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

Fix extend schema from #1314 #1323

Merged
merged 4 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,14 +987,25 @@ describe('extendSchema', () => {
hearSomething: String
}
`);
const schema = extendSchema(testSchema, ast);
expect(schema.extensionASTNodes.map(print).join('\n')).to.equal(dedent`
let schema = extendSchema(testSchema, ast);

const secondAST = parse(`
extend schema @foo

directive @foo on SCHEMA
`);
schema = extendSchema(schema, secondAST);

const nodes = schema.extensionASTNodes;
expect(nodes.map(n => print(n) + '\n').join('')).to.equal(dedent`
extend schema {
mutation: Mutation
}
extend schema {
subscription: Subscription
}`);
}
extend schema @foo
`);
});

it('does not allow redefining an existing root type', () => {
Expand Down
31 changes: 19 additions & 12 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,10 @@ export function extendSchema(
const extendTypeCache = Object.create(null);

// Get the extended root operation types.
const existingQueryType = schema.getQueryType();
const existingMutationType = schema.getMutationType();
const existingSubscriptionType = schema.getSubscriptionType();
const operationTypes = {
query: existingQueryType ? getExtendedType(existingQueryType) : null,
mutation: existingMutationType
? getExtendedType(existingMutationType)
: null,
subscription: existingSubscriptionType
? getExtendedType(existingSubscriptionType)
: null,
query: getExtendedMaybeType(schema.getQueryType()),
mutation: getExtendedMaybeType(schema.getMutationType()),
subscription: getExtendedMaybeType(schema.getSubscriptionType()),
};

// Then, incorporate all schema extensions.
Expand All @@ -220,11 +213,21 @@ export function extendSchema(
if (operationTypes[operation]) {
throw new Error(`Must provide only one ${operation} type in schema.`);
}
operationTypes[operation] = astBuilder.buildType(operationType.type);
const typeRef = operationType.type;
// Note: While this could make early assertions to get the correctly
// typed values, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
operationTypes[operation] = (astBuilder.buildType(typeRef): any);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how it worked previously since astBuilder.buildType returns GraphQLNamedType but new GraphQLSchema expects query/mutation/subscription to be GraphQLObjectType.
Looks like Flow bug 😞

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that the more declarative operationTypes definition has firmer types which flow can now report on where previously the error was hidden

});
}
});

const schemaExtensionASTNodes = schemaExtensions
? schema.extensionASTNodes
? schema.extensionASTNodes.concat(schemaExtensions)
: schemaExtensions
: schema.extensionASTNodes;

const types = [
// Iterate through all types, getting the type definition for each, ensuring
// that any type not directly referenced by a field will get created.
Expand All @@ -249,7 +252,7 @@ export function extendSchema(
types,
directives: getMergedDirectives(),
astNode: schema.astNode,
extensionASTNodes: schemaExtensions,
extensionASTNodes: schemaExtensionASTNodes,
allowedLegacyNames,
});

Expand All @@ -265,6 +268,10 @@ export function extendSchema(
);
}

function getExtendedMaybeType<T: GraphQLNamedType>(type: ?T): ?T {
return type ? getExtendedType(type) : null;
}

function getExtendedType<T: GraphQLNamedType>(type: T): T {
if (!extendTypeCache[type.name]) {
extendTypeCache[type.name] = extendType(type);
Expand Down