-
Notifications
You must be signed in to change notification settings - Fork 2k
SWAPI example: construct schema from SDL #1384
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
Conversation
episodeEnum.getValue('JEDI').value = 6; | ||
for (const enumValue of episodeEnum.getValues()) { | ||
episodeEnum._valueLookup.set(enumValue.value, enumValue); | ||
} |
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.
There no way to assign enum values after schema constructed.
@leebyron What do you this about adding one more option to buidlSchema
/extendSchema
?
const schema = buildSchema(sdl, {
extraConfig: {
types: {
Episode: {
values: {
NEWHOPE: { value: 4 },
EMPIRE: { value: 5 },
JEDI: { value: 6 },
},
},
},
},
});
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.
Likewise buildSchema does not provide resolvers or resolveType functions - I think this is probably more appropriate for the graphql-tools which provides additional runtime behavior atop buildSchema
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.
I'm implementing this in #1987, it may look like:
const schema = buildSchema(sdl, {
resolvers: {
Episode: {
NEWHOPE: 4,
EMPIRE: 5,
JEDI: 6,
},
},
});
if merged of course :-)
See this test:
graphql-js/src/utilities/__tests__/buildASTSchema-test.js
Lines 98 to 123 in 84adacf
it('can lookup enum values', () => { | |
const schema = buildSchema( | |
` | |
enum Color { RED, GREEN, BLUE } | |
type Query { | |
colors: [Color!]! | |
} | |
`, | |
{ | |
resolvers: { | |
Query: { | |
colors: () => [4, 2, 1], | |
}, | |
Color: { | |
RED: 1, | |
GREEN: 2, | |
BLUE: 4, | |
}, | |
}, | |
}, | |
); | |
expect(graphqlSync(schema, '{ colors }', null)).to.deep.equal({ | |
data: { colors: ['BLUE', 'GREEN', 'RED'] }, | |
}); | |
}); |
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.
I'm not sure this is something we want to do - this is supposed to act as both an example of and full integration test of the schema definition API and query path.
@@ -170,71 +139,65 @@ describe('Star Wars Query Tests', () => { | |||
|
|||
describe('Using IDs and query parameters to refetch objects', () => { | |||
it('Allows us to query for Luke Skywalker directly, using his ID', async () => { | |||
const query = ` | |||
query FetchLukeQuery { | |||
const result = await executeQuery(` |
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.
These are intended to be full integration tests, so they intentionally were running the full graphql function instead of just the execute step
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.
@leebyron it's a naming issue executeQuery
is defined like this:
function executeQuery(query, variableValues) {
return graphql(StarWarsSchema, query, new Query(), null, variableValues);
}
Since now in majority of tests you need to supply 3 args instead of 2:
const result = graphql(StarWarsSchema, query, new Query());
I decided to move it into utility function but now I see that it needs a better name. Do you have any suggestions?
friends: Array<string>, | ||
appearsIn: Array<number>, | ||
}; | ||
export type CharacterData = HumanData | DroidData; |
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.
I prefer without the Data
suffix for readability
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.
No please, because of:
import type { CharacterData, HumanData, DroidData } from './starWarsData'; |
@leebyron Agree 👍 I got carried away with shiny SDL syntax, P.S. This PR is not a part of |
so these codes for v15?? |
Closing this old PR, feel free to reopen! We can track efforts to build a |
Motivation #1379