Skip to content
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

SWAPI example: construct schema from SDL #1384

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

IvanGoncharov
Copy link
Member

Motivation #1379

episodeEnum.getValue('JEDI').value = 6;
for (const enumValue of episodeEnum.getValues()) {
episodeEnum._valueLookup.set(enumValue.value, enumValue);
}
Copy link
Member Author

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 },
        },
      },
    },
  },
});

Copy link
Contributor

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

Copy link
Contributor

@langpavel langpavel Jun 21, 2019

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:

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'] },
});
});

Copy link
Contributor

@leebyron leebyron left a 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(`
Copy link
Contributor

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

Copy link
Member Author

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;
Copy link
Contributor

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

Copy link
Contributor

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';

@IvanGoncharov
Copy link
Member Author

IvanGoncharov commented Jun 16, 2018

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.

@leebyron Agree 👍 I got carried away with shiny SDL syntax, GraphQL*Type is still a backbone of this library. At the same time, we need a more complex example of SDL defined schema than this one: https://graphql.org/graphql-js/object-types/
How about adding separate starWarsSDLSchema.js alongside with starWarsSchema.js?

P.S. This PR is not a part of 14.0.0 so I will postpone this PR until we ship release.

@guxima
Copy link

guxima commented Feb 21, 2019

so these codes for v15??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants