-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Allows to add schema definition missing in the original schema #1441
Merged
IvanGoncharov
merged 2 commits into
graphql:master
from
IvanGoncharov:definedSchemaInExtend
Aug 1, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ import type { | |
DocumentNode, | ||
DirectiveDefinitionNode, | ||
SchemaExtensionNode, | ||
SchemaDefinitionNode, | ||
} from '../language/ast'; | ||
|
||
type Options = {| | ||
|
@@ -106,18 +107,28 @@ export function extendSchema( | |
// have the same name. For example, a type named "skip". | ||
const directiveDefinitions: Array<DirectiveDefinitionNode> = []; | ||
|
||
let schemaDef: ?SchemaDefinitionNode; | ||
// Schema extensions are collected which may add additional operation types. | ||
const schemaExtensions: Array<SchemaExtensionNode> = []; | ||
|
||
for (let i = 0; i < documentAST.definitions.length; i++) { | ||
const def = documentAST.definitions[i]; | ||
switch (def.kind) { | ||
case Kind.SCHEMA_DEFINITION: | ||
// Sanity check that a schema extension is not defining a new schema | ||
throw new GraphQLError( | ||
'Cannot define a new schema within a schema extension.', | ||
[def], | ||
); | ||
// Sanity check that a schema extension is not overriding the schema | ||
if ( | ||
schema.astNode || | ||
schema.getQueryType() || | ||
schema.getMutationType() || | ||
schema.getSubscriptionType() | ||
) { | ||
throw new GraphQLError( | ||
'Cannot define a new schema within a schema extension.', | ||
[def], | ||
); | ||
} | ||
schemaDef = def; | ||
break; | ||
case Kind.SCHEMA_EXTENSION: | ||
schemaExtensions.push(def); | ||
break; | ||
|
@@ -184,7 +195,8 @@ export function extendSchema( | |
Object.keys(typeExtensionsMap).length === 0 && | ||
Object.keys(typeDefinitionMap).length === 0 && | ||
directiveDefinitions.length === 0 && | ||
schemaExtensions.length === 0 | ||
schemaExtensions.length === 0 && | ||
!schemaDef | ||
) { | ||
return schema; | ||
} | ||
|
@@ -216,19 +228,28 @@ export function extendSchema( | |
subscription: extendMaybeNamedType(schema.getSubscriptionType()), | ||
}; | ||
|
||
// Then, incorporate all schema extensions. | ||
if (schemaDef) { | ||
for (const { operation, type } of schemaDef.operationTypes) { | ||
if (operationTypes[operation]) { | ||
throw new Error(`Must provide only one ${operation} type in schema.`); | ||
} | ||
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. After #1435 I will move this check into SDL validation. |
||
// 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(type): any); | ||
} | ||
} | ||
// Then, incorporate schema definition and all schema extensions. | ||
for (const schemaExtension of schemaExtensions) { | ||
if (schemaExtension.operationTypes) { | ||
for (const operationType of schemaExtension.operationTypes) { | ||
const operation = operationType.operation; | ||
for (const { operation, type } of schemaExtension.operationTypes) { | ||
if (operationTypes[operation]) { | ||
throw new Error(`Must provide only one ${operation} type in schema.`); | ||
} | ||
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); | ||
operationTypes[operation] = (astBuilder.buildType(type): any); | ||
} | ||
} | ||
} | ||
|
@@ -254,9 +275,7 @@ export function extendSchema( | |
|
||
// Then produce and return a Schema with these types. | ||
return new GraphQLSchema({ | ||
query: operationTypes.query, | ||
mutation: operationTypes.mutation, | ||
subscription: operationTypes.subscription, | ||
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. tiny refactoring 😜 |
||
...operationTypes, | ||
types, | ||
directives: getMergedDirectives(), | ||
astNode: schema.astNode, | ||
|
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.
I initially forgot to add this check, so I also change tests to cover it.