Skip to content

SWAPI example: construct schema from SDL#1384

Closed
IvanGoncharov wants to merge 3 commits into
graphql:mainfrom
IvanGoncharov:sdlSWAPI
Closed

SWAPI example: construct schema from SDL#1384
IvanGoncharov wants to merge 3 commits into
graphql:mainfrom
IvanGoncharov:sdlSWAPI

Conversation

@IvanGoncharov

Copy link
Copy Markdown
Member

Motivation #1379

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

@IvanGoncharov IvanGoncharov Jun 11, 2018

Copy link
Copy Markdown
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
Copy Markdown
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

@langpavel langpavel Jun 21, 2019

Copy link
Copy Markdown
Contributor

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

@leebyron leebyron left a comment

Copy link
Copy Markdown
Contributor

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.

it('Allows us to query for Luke Skywalker directly, using his ID', async () => {
const query = `
query FetchLukeQuery {
const result = await executeQuery(`

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

IvanGoncharov commented Jun 16, 2018

Copy link
Copy Markdown
Member Author

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

guxima commented Feb 21, 2019

Copy link
Copy Markdown

so these codes for v15??

Base automatically changed from master to main January 27, 2021 11:10
@IvanGoncharov IvanGoncharov added this to the post-16.0.0 milestone Aug 11, 2021
@yaacovCR

yaacovCR commented Oct 1, 2024

Copy link
Copy Markdown
Contributor

Closing this old PR, feel free to reopen!

We can track efforts to build a makeExecutableSchema() type approach in #3047

@yaacovCR yaacovCR closed this Oct 1, 2024
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Jun 9, 2026
Add `supplementalConfig` to `buildSchema`, `buildASTSchema`, and `extendSchema` so SDL-defined schema elements can receive constructor-only configuration that SDL cannot express. The option is validated by default and can be bypassed with `assumeValidSupplementalConfig` for config that has already been checked.

The schema-level config is keyed by schema element kind and GraphQL name:

- `scalarTypes`: `serialize`, `parseValue`, `parseLiteral`, `coerceOutputValue`, `coerceInputValue`, `coerceInputLiteral`, `valueToLiteral`, and `extensions`.

- `objectTypes`: `fields`, `isTypeOf`, and `extensions`.

- `interfaceTypes`: `fields`, `resolveType`, and `extensions`.

- `unionTypes`: `resolveType` and `extensions`.

- `enumTypes`: `values` and `extensions`; enum values can define an internal `value` and `extensions`.

- `inputObjectTypes`: `fields` and `extensions`.

- `directives`: `args` and `extensions`.

- `extensions`: schema extensions.

Field supplements can be a resolver function or an object with `resolve`, `subscribe`, `args`, and `extensions`. Argument, input field, directive argument, enum value, and type supplements apply only to schema elements declared or extended by the SDL document, and unmatched supplemental config is rejected by default.

This lets SDL own the type-system shape while JavaScript supplies runtime behavior and host-language metadata: field resolvers, subscription functions, abstract type resolution, object type predicates, custom scalar coercion and literal conversion, enum runtime values, and extensions.

Related issues: graphql#497, graphql#499, graphql#516, graphql#525, graphql#604, graphql#622, graphql#1379, graphql#1858.

Related prior PRs: graphql#469, graphql#947, graphql#1384, graphql#1987.
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Jun 10, 2026
Add `supplementalConfig` to `buildSchema`, `buildASTSchema`, and `extendSchema` so SDL-defined schema elements can receive constructor-only configuration that SDL cannot express. The option is validated by default and can be bypassed with `assumeValidSupplementalConfig` for config that has already been checked.

The schema-level config is keyed by schema element kind and GraphQL name:

- `scalarTypes`: `serialize`, `parseValue`, `parseLiteral`, `coerceOutputValue`, `coerceInputValue`, `coerceInputLiteral`, `valueToLiteral`, and `extensions`.

- `objectTypes`: `fields`, `isTypeOf`, and `extensions`.

- `interfaceTypes`: `fields`, `resolveType`, and `extensions`.

- `unionTypes`: `resolveType` and `extensions`.

- `enumTypes`: `values` and `extensions`; enum values can define an internal `value` and `extensions`.

- `inputObjectTypes`: `fields` and `extensions`.

- `directives`: `args` and `extensions`.

- `extensions`: schema extensions.

Field supplements can be a resolver function or an object with `resolve`, `subscribe`, `args`, and `extensions`. Argument, input field, directive argument, enum value, and type supplements apply only to schema elements declared or extended by the SDL document, and unmatched supplemental config is rejected by default.

This lets SDL own the type-system shape while JavaScript supplies runtime behavior and host-language metadata: field resolvers, subscription functions, abstract type resolution, object type predicates, custom scalar coercion and literal conversion, enum runtime values, and extensions.

Related issues: graphql#497, graphql#499, graphql#516, graphql#525, graphql#604, graphql#622, graphql#1379, graphql#1858.

Related prior PRs: graphql#469, graphql#947, graphql#1384, graphql#1987.
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Jun 21, 2026
Add `supplementalConfig` to `buildSchema`, `buildASTSchema`, and `extendSchema` so SDL-defined schema elements can receive constructor-only configuration that SDL cannot express. The option is validated by default and can be bypassed with `assumeValidSupplementalConfig` for config that has already been checked.

The schema-level config is keyed by schema element kind and GraphQL name:

- `scalarTypes`: `serialize`, `parseValue`, `parseLiteral`, `coerceOutputValue`, `coerceInputValue`, `coerceInputLiteral`, `valueToLiteral`, and `extensions`.

- `objectTypes`: `fields`, `isTypeOf`, and `extensions`.

- `interfaceTypes`: `fields`, `resolveType`, and `extensions`.

- `unionTypes`: `resolveType` and `extensions`.

- `enumTypes`: `values` and `extensions`; enum values can define an internal `value` and `extensions`.

- `inputObjectTypes`: `fields` and `extensions`.

- `directives`: `args` and `extensions`.

- `extensions`: schema extensions.

Field supplements can be a resolver function or an object with `resolve`, `subscribe`, `args`, and `extensions`. Argument, input field, directive argument, enum value, and type supplements apply only to schema elements declared or extended by the SDL document, and unmatched supplemental config is rejected by default.

This lets SDL own the type-system shape while JavaScript supplies runtime behavior and host-language metadata: field resolvers, subscription functions, abstract type resolution, object type predicates, custom scalar coercion and literal conversion, enum runtime values, and extensions.

Related issues: graphql#497, graphql#499, graphql#516, graphql#525, graphql#604, graphql#622, graphql#1379, graphql#1858.

Related prior PRs: graphql#469, graphql#947, graphql#1384, graphql#1987.
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.

6 participants