-
Notifications
You must be signed in to change notification settings - Fork 2k
Unify extending of type system #1261
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -34,12 +34,7 @@ import { GraphQLDirective } from '../type/directives'; | |
import { Kind } from '../language/kinds'; | ||
|
||
import type { GraphQLType, GraphQLNamedType } from '../type/definition'; | ||
|
||
import type { | ||
DocumentNode, | ||
DirectiveDefinitionNode, | ||
TypeExtensionNode, | ||
} from '../language/ast'; | ||
import type { DocumentNode, DirectiveDefinitionNode } from '../language/ast'; | ||
|
||
type Options = {| | ||
...GraphQLSchemaValidationOptions, | ||
|
@@ -114,6 +109,7 @@ export function extendSchema( | |
typeDefinitionMap[typeName] = def; | ||
break; | ||
case Kind.OBJECT_TYPE_EXTENSION: | ||
case Kind.INTERFACE_TYPE_EXTENSION: | ||
// Sanity check that this type extension exists within the | ||
// schema's existing types. | ||
const extendedTypeName = def.name.value; | ||
|
@@ -125,39 +121,12 @@ export function extendSchema( | |
[def], | ||
); | ||
} | ||
if (!isObjectType(existingType)) { | ||
throw new GraphQLError( | ||
`Cannot extend non-object type "${extendedTypeName}".`, | ||
[def], | ||
); | ||
} | ||
typeExtensionsMap[extendedTypeName] = appendExtensionToTypeExtensions( | ||
def, | ||
typeExtensionsMap[extendedTypeName], | ||
); | ||
break; | ||
case Kind.INTERFACE_TYPE_EXTENSION: | ||
const extendedInterfaceTypeName = def.name.value; | ||
const existingInterfaceType = schema.getType(extendedInterfaceTypeName); | ||
if (!existingInterfaceType) { | ||
throw new GraphQLError( | ||
`Cannot extend interface "${extendedInterfaceTypeName}" because ` + | ||
'it does not exist in the existing schema.', | ||
[def], | ||
); | ||
} | ||
if (!isInterfaceType(existingInterfaceType)) { | ||
throw new GraphQLError( | ||
`Cannot extend non-interface type "${extendedInterfaceTypeName}".`, | ||
[def], | ||
); | ||
} | ||
typeExtensionsMap[ | ||
extendedInterfaceTypeName | ||
] = appendExtensionToTypeExtensions( | ||
def, | ||
typeExtensionsMap[extendedInterfaceTypeName], | ||
); | ||
checkExtensionNode(existingType, def); | ||
|
||
const existingTypeExtensions = typeExtensionsMap[extendedTypeName]; | ||
typeExtensionsMap[extendedTypeName] = existingTypeExtensions | ||
? existingTypeExtensions.concat([def]) | ||
: [def]; | ||
break; | ||
case Kind.DIRECTIVE_DEFINITION: | ||
const directiveName = def.name.value; | ||
|
@@ -212,9 +181,6 @@ export function extendSchema( | |
const extendTypeCache = Object.create(null); | ||
|
||
// Get the root Query, Mutation, and Subscription object types. | ||
// Note: While this could make early assertions to get the correctly | ||
// typed values below, that would throw immediately while type system | ||
// validation with validateSchema() will produce more actionable results. | ||
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. This comment is not relevant anymore since they're no |
||
const existingQueryType = schema.getQueryType(); | ||
const queryType = existingQueryType | ||
? getExtendedType(existingQueryType) | ||
|
@@ -235,7 +201,7 @@ export function extendSchema( | |
// that any type not directly referenced by a field will get created. | ||
...objectValues(schema.getTypeMap()).map(type => getExtendedType(type)), | ||
// Do the same with new types. | ||
...objectValues(typeDefinitionMap).map(type => astBuilder.buildType(type)), | ||
...astBuilder.buildTypes(objectValues(typeDefinitionMap)), | ||
]; | ||
|
||
// Support both original legacy names and extended legacy names. | ||
|
@@ -257,17 +223,6 @@ export function extendSchema( | |
allowedLegacyNames, | ||
}); | ||
|
||
function appendExtensionToTypeExtensions( | ||
extension: TypeExtensionNode, | ||
existingTypeExtensions: ?Array<TypeExtensionNode>, | ||
): Array<TypeExtensionNode> { | ||
if (!existingTypeExtensions) { | ||
return [extension]; | ||
} | ||
existingTypeExtensions.push(extension); | ||
return existingTypeExtensions; | ||
} | ||
|
||
// Below are functions used for producing this schema that have closed over | ||
// this scope and have access to the schema, cache, and newly defined types. | ||
|
||
|
@@ -420,3 +375,24 @@ export function extendSchema( | |
return getExtendedType(typeDef); | ||
} | ||
} | ||
|
||
function checkExtensionNode(type, node) { | ||
switch (node.kind) { | ||
case Kind.OBJECT_TYPE_EXTENSION: | ||
if (!isObjectType(type)) { | ||
throw new GraphQLError( | ||
`Cannot extend non-object type "${type.name}".`, | ||
[node], | ||
); | ||
} | ||
break; | ||
case Kind.INTERFACE_TYPE_EXTENSION: | ||
if (!isInterfaceType(type)) { | ||
throw new GraphQLError( | ||
`Cannot extend non-interface type "${type.name}".`, | ||
[node], | ||
); | ||
} | ||
break; | ||
} | ||
} |
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.
Nice to avoid the lambda allocation if the AST defines no interfaces