@@ -56,6 +56,10 @@ import type {
56
56
SchemaExtensionNode ,
57
57
SchemaDefinitionNode ,
58
58
} from '../language/ast' ;
59
+ import {
60
+ isTypeDefinitionNode ,
61
+ isTypeExtensionNode ,
62
+ } from '../language/predicates' ;
59
63
60
64
type Options = { |
61
65
...GraphQLSchemaValidationOptions ,
@@ -125,67 +129,51 @@ export function extendSchema(
125
129
126
130
for ( let i = 0 ; i < documentAST . definitions . length ; i ++ ) {
127
131
const def = documentAST . definitions [ i ] ;
128
- switch ( def . kind ) {
129
- case Kind . SCHEMA_DEFINITION :
130
- schemaDef = def ;
131
- break ;
132
- case Kind . SCHEMA_EXTENSION :
133
- schemaExtensions . push ( def ) ;
134
- break ;
135
- case Kind . OBJECT_TYPE_DEFINITION :
136
- case Kind . INTERFACE_TYPE_DEFINITION :
137
- case Kind . ENUM_TYPE_DEFINITION :
138
- case Kind . UNION_TYPE_DEFINITION :
139
- case Kind . SCALAR_TYPE_DEFINITION :
140
- case Kind . INPUT_OBJECT_TYPE_DEFINITION :
141
- // Sanity check that none of the defined types conflict with the
142
- // schema's existing types.
143
- const typeName = def . name . value ;
144
- if ( schema . getType ( typeName ) ) {
145
- throw new GraphQLError (
146
- `Type "${ typeName } " already exists in the schema. It cannot also ` +
147
- 'be defined in this type definition.' ,
148
- [ def ] ,
149
- ) ;
150
- }
151
- typeDefinitionMap [ typeName ] = def ;
152
- break ;
153
- case Kind . SCALAR_TYPE_EXTENSION :
154
- case Kind . OBJECT_TYPE_EXTENSION :
155
- case Kind . INTERFACE_TYPE_EXTENSION :
156
- case Kind . ENUM_TYPE_EXTENSION :
157
- case Kind . INPUT_OBJECT_TYPE_EXTENSION :
158
- case Kind . UNION_TYPE_EXTENSION :
159
- // Sanity check that this type extension exists within the
160
- // schema's existing types.
161
- const extendedTypeName = def . name . value ;
162
- const existingType = schema . getType ( extendedTypeName ) ;
163
- if ( ! existingType ) {
164
- throw new GraphQLError (
165
- `Cannot extend type "${ extendedTypeName } " because it does not ` +
166
- 'exist in the existing schema.' ,
167
- [ def ] ,
168
- ) ;
169
- }
170
- checkExtensionNode ( existingType , def ) ;
171
-
172
- const existingTypeExtensions = typeExtensionsMap [ extendedTypeName ] ;
173
- typeExtensionsMap [ extendedTypeName ] = existingTypeExtensions
174
- ? existingTypeExtensions . concat ( [ def ] )
175
- : [ def ] ;
176
- break ;
177
- case Kind . DIRECTIVE_DEFINITION :
178
- const directiveName = def . name . value ;
179
- const existingDirective = schema . getDirective ( directiveName ) ;
180
- if ( existingDirective ) {
181
- throw new GraphQLError (
182
- `Directive "${ directiveName } " already exists in the schema. It ` +
183
- 'cannot be redefined.' ,
184
- [ def ] ,
185
- ) ;
186
- }
187
- directiveDefinitions . push ( def ) ;
188
- break ;
132
+ if ( def . kind === Kind . SCHEMA_DEFINITION ) {
133
+ schemaDef = def ;
134
+ } else if ( def . kind === Kind . SCHEMA_EXTENSION ) {
135
+ schemaExtensions . push ( def ) ;
136
+ } else if ( isTypeDefinitionNode ( def ) ) {
137
+ // Sanity check that none of the defined types conflict with the
138
+ // schema's existing types.
139
+ const typeName = def . name . value ;
140
+ if ( schema . getType ( typeName ) ) {
141
+ throw new GraphQLError (
142
+ `Type "${ typeName } " already exists in the schema. It cannot also ` +
143
+ 'be defined in this type definition.' ,
144
+ [ def ] ,
145
+ ) ;
146
+ }
147
+ typeDefinitionMap [ typeName ] = def ;
148
+ } else if ( isTypeExtensionNode ( def ) ) {
149
+ // Sanity check that this type extension exists within the
150
+ // schema's existing types.
151
+ const extendedTypeName = def . name . value ;
152
+ const existingType = schema . getType ( extendedTypeName ) ;
153
+ if ( ! existingType ) {
154
+ throw new GraphQLError (
155
+ `Cannot extend type "${ extendedTypeName } " because it does not ` +
156
+ 'exist in the existing schema.' ,
157
+ [ def ] ,
158
+ ) ;
159
+ }
160
+ checkExtensionNode ( existingType , def ) ;
161
+
162
+ const existingTypeExtensions = typeExtensionsMap [ extendedTypeName ] ;
163
+ typeExtensionsMap [ extendedTypeName ] = existingTypeExtensions
164
+ ? existingTypeExtensions . concat ( [ def ] )
165
+ : [ def ] ;
166
+ } else if ( def . kind === Kind . DIRECTIVE_DEFINITION ) {
167
+ const directiveName = def . name . value ;
168
+ const existingDirective = schema . getDirective ( directiveName ) ;
169
+ if ( existingDirective ) {
170
+ throw new GraphQLError (
171
+ `Directive "${ directiveName } " already exists in the schema. It ` +
172
+ 'cannot be redefined.' ,
173
+ [ def ] ,
174
+ ) ;
175
+ }
176
+ directiveDefinitions . push ( def ) ;
189
177
}
190
178
}
191
179
0 commit comments