-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add union type #134
Open
Elendev
wants to merge
13
commits into
graphql:master
Choose a base branch
from
Elendev:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add union type #134
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
04d3114
Add an Union type
Elendev 85be99f
Small fix : re-use the constant
Elendev 5eec722
Add union support to query all vehicles and ships
Elendev 4325d05
Fix unnecessarily quoted properties
Elendev 71031da
Remove useless else...
Elendev 6fd8041
Prettify
Elendev 6048896
Fix code style and flow
Elendev 294c488
Add unit tests
Elendev 3e1f059
Prettify machine test cases
Elendev 50ab10f
prettify....
Elendev 73041b3
Add 'Person' in the 'Machines' type to represent all machines in the …
Elendev 56b27b5
Remove unused import ^^"
Elendev d1ffec2
Merge branch 'master' of https://github.com/graphql/swapi-graphql
Elendev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
allMachines { | ||
machines { | ||
... on Vehicle { | ||
id, | ||
name, | ||
vehicleClass | ||
}, | ||
... on Starship { | ||
id, | ||
name, | ||
starshipClass | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE-examples file in the root directory of this source tree. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import { swapi } from './swapi'; | ||
|
||
// 80+ char lines are useful in describe/it, so ignore in this file. | ||
/* eslint-disable max-len */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both lines not needed after recent changes in |
||
|
||
function getDocument(query) { | ||
return `${query} | ||
fragment AllStarshipProperties on Starship { | ||
MGLT | ||
starshipCargoCapacity: cargoCapacity | ||
consumables | ||
starshipCostInCredits: costInCredits | ||
crew | ||
hyperdriveRating | ||
length | ||
manufacturers | ||
maxAtmospheringSpeed | ||
model | ||
name | ||
passengers | ||
starshipClass | ||
filmConnection(first:1) { edges { node { title } } } | ||
pilotConnection(first:1) { edges { node { name } } } | ||
} | ||
|
||
fragment AllVehicleProperties on Vehicle { | ||
vehicleCargoCapacity: cargoCapacity | ||
consumables | ||
vehicleCostInCredits: costInCredits | ||
crew | ||
length | ||
manufacturers | ||
maxAtmospheringSpeed | ||
model | ||
name | ||
passengers | ||
vehicleClass | ||
filmConnection(first:1) { edges { node { title } } } | ||
pilotConnection(first:1) { edges { node { name } } } | ||
} | ||
|
||
fragment AllPersonProperties on Person { | ||
birthYear | ||
eyeColor | ||
gender | ||
hairColor | ||
height | ||
homeworld { name } | ||
mass | ||
name | ||
skinColor | ||
species { name } | ||
filmConnection(first:1) { edges { node { title } } } | ||
starshipConnection(first:1) { edges { node { name } } } | ||
vehicleConnection(first:1) { edges { node { name } } } | ||
} | ||
`; | ||
} | ||
|
||
describe('Machine type', async () => { | ||
it('Gets an object by global ID', async () => { | ||
const query = '{ starship(starshipID: 5) { id, name } }'; | ||
const result = await swapi(query); | ||
const nextQuery = ` | ||
{ | ||
machine(id: "${result.data.starship.id}") { | ||
... on Vehicle { id, name }, | ||
... on Starship { id, name }, | ||
... on Person { id, name } | ||
} | ||
} | ||
`; | ||
const nextResult = await swapi(nextQuery); | ||
expect(result.data.starship.name).to.equal('Sentinel-class landing craft'); | ||
expect(nextResult.data.machine.name).to.equal( | ||
'Sentinel-class landing craft', | ||
); | ||
expect(result.data.starship.id).to.equal(nextResult.data.machine.id); | ||
}); | ||
|
||
it('Gets all properties', async () => { | ||
const query = '{ starship(starshipID: 5) { id, name } }'; | ||
const idResult = await swapi(query); | ||
|
||
const nextQuery = getDocument( | ||
`{ | ||
machine(id: "${idResult.data.starship.id}") { | ||
... on Starship { | ||
...AllStarshipProperties | ||
} | ||
... on Vehicle { | ||
...AllVehicleProperties | ||
} | ||
... on Person { | ||
...AllPersonProperties | ||
} | ||
} | ||
}`, | ||
); | ||
const result = await swapi(nextQuery); | ||
const expected = { | ||
MGLT: 70, | ||
starshipCargoCapacity: 180000, | ||
consumables: '1 month', | ||
starshipCostInCredits: 240000, | ||
crew: '5', | ||
filmConnection: { edges: [{ node: { title: 'A New Hope' } }] }, | ||
hyperdriveRating: 1, | ||
length: 38, | ||
manufacturers: ['Sienar Fleet Systems', 'Cyngus Spaceworks'], | ||
maxAtmospheringSpeed: 1000, | ||
model: 'Sentinel-class landing craft', | ||
name: 'Sentinel-class landing craft', | ||
passengers: '75', | ||
pilotConnection: { edges: [] }, | ||
starshipClass: 'landing craft', | ||
}; | ||
expect(result.data.machine).to.deep.equal(expected); | ||
}); | ||
|
||
it('All objects query', async () => { | ||
const query = getDocument( | ||
`{ | ||
allMachines { | ||
edges { | ||
cursor, | ||
node { | ||
... on Starship { ...AllStarshipProperties }, | ||
... on Vehicle { ...AllVehicleProperties }, | ||
... on Person { ... AllPersonProperties } | ||
} | ||
} | ||
} | ||
}`, | ||
); | ||
const result = await swapi(query); | ||
expect(result.data.allMachines.edges.length).to.equal(81); | ||
}); | ||
|
||
it('Pagination query', async () => { | ||
const query = `{ | ||
allMachines(first: 2) { | ||
edges { | ||
cursor, | ||
node { | ||
... on Vehicle { name }, | ||
... on Starship { name }, | ||
... on Person { name } | ||
} | ||
} | ||
} | ||
}`; | ||
const result = await swapi(query); | ||
expect(result.data.allMachines.edges.map(e => e.node.name)).to.deep.equal([ | ||
'Sand Crawler', | ||
'T-16 skyhopper', | ||
]); | ||
const nextCursor = result.data.allMachines.edges[1].cursor; | ||
|
||
const nextQuery = `{ allMachines(first: 2, after:"${nextCursor}") { | ||
edges { cursor, node { ... on Vehicle { name }, ... on Starship { name }, ... on Person { name } } } } | ||
}`; | ||
const nextResult = await swapi(nextQuery); | ||
expect( | ||
nextResult.data.allMachines.edges.map(e => e.node.name), | ||
).to.deep.equal(['X-34 landspeeder', 'TIE/LN starfighter']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (c) 2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE-examples file in the root directory of this source tree. | ||
*/ | ||
|
||
import { GraphQLUnionType } from 'graphql'; | ||
import { GraphQLUnionTypeConfig } from 'graphql/type/definition'; | ||
|
||
/** | ||
* GraphQLUnionType with a 'filter' method that allow to filter the | ||
* elements of the union | ||
*/ | ||
export default class GraphQLFilteredUnionType extends GraphQLUnionType { | ||
constructor(config: GraphQLUnionTypeConfig<*, *>): void { | ||
super(config); | ||
|
||
if ('filter' in config) { | ||
this.filter = config.filter; | ||
} else { | ||
this.filter = (type, objects) => objects; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please copy updated header from
master