graphql 14.0.0 → 14.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/README.md +4 -4
  2. package/error/GraphQLError.js +1 -1
  3. package/error/GraphQLError.js.flow +3 -3
  4. package/error/GraphQLError.mjs +1 -1
  5. package/error/formatError.js +1 -1
  6. package/error/formatError.mjs +1 -1
  7. package/error/index.mjs +1 -1
  8. package/error/locatedError.js +1 -1
  9. package/error/locatedError.mjs +1 -1
  10. package/error/printError.js +1 -1
  11. package/error/printError.mjs +1 -1
  12. package/error/syntaxError.js +1 -1
  13. package/error/syntaxError.mjs +1 -1
  14. package/execution/execute.js +4 -4
  15. package/execution/execute.js.flow +4 -6
  16. package/execution/execute.mjs +5 -5
  17. package/execution/index.mjs +1 -1
  18. package/execution/values.js +4 -4
  19. package/execution/values.js.flow +3 -3
  20. package/execution/values.mjs +3 -3
  21. package/graphql.js +1 -1
  22. package/graphql.mjs +1 -1
  23. package/index.js +12 -0
  24. package/index.js.flow +4 -0
  25. package/index.mjs +4 -4
  26. package/jsutils/dedent.js +2 -2
  27. package/jsutils/dedent.js.flow +1 -1
  28. package/jsutils/dedent.mjs +2 -2
  29. package/jsutils/defineToJSON.js +17 -6
  30. package/jsutils/defineToJSON.js.flow +14 -5
  31. package/jsutils/defineToJSON.mjs +14 -6
  32. package/jsutils/defineToStringTag.js +5 -5
  33. package/jsutils/defineToStringTag.js.flow +3 -3
  34. package/jsutils/defineToStringTag.mjs +4 -4
  35. package/jsutils/inspect.js +46 -12
  36. package/jsutils/inspect.js.flow +42 -15
  37. package/jsutils/inspect.mjs +45 -5
  38. package/jsutils/instanceOf.js +1 -1
  39. package/jsutils/instanceOf.mjs +1 -1
  40. package/jsutils/invariant.js +1 -1
  41. package/jsutils/invariant.mjs +1 -1
  42. package/jsutils/isInvalid.js +1 -1
  43. package/jsutils/isInvalid.mjs +1 -1
  44. package/jsutils/isNullish.js +1 -1
  45. package/jsutils/isNullish.mjs +1 -1
  46. package/jsutils/isPromise.js +1 -1
  47. package/jsutils/isPromise.mjs +1 -1
  48. package/jsutils/keyMap.js +1 -1
  49. package/jsutils/keyMap.mjs +1 -1
  50. package/jsutils/keyValMap.js +1 -1
  51. package/jsutils/keyValMap.mjs +1 -1
  52. package/jsutils/mapValue.js +54 -0
  53. package/jsutils/mapValue.js.flow +27 -0
  54. package/jsutils/mapValue.mjs +44 -0
  55. package/jsutils/memoize3.js +1 -1
  56. package/jsutils/memoize3.mjs +1 -1
  57. package/jsutils/nodejsCustomInspectSymbol.js +18 -0
  58. package/jsutils/nodejsCustomInspectSymbol.js.flow +15 -0
  59. package/jsutils/nodejsCustomInspectSymbol.mjs +10 -0
  60. package/jsutils/orList.js +17 -4
  61. package/jsutils/orList.js.flow +13 -7
  62. package/jsutils/orList.mjs +14 -4
  63. package/jsutils/promiseForObject.js +1 -1
  64. package/jsutils/promiseForObject.mjs +1 -1
  65. package/jsutils/promiseReduce.js +1 -1
  66. package/jsutils/promiseReduce.mjs +1 -1
  67. package/jsutils/quotedOrList.js +1 -1
  68. package/jsutils/quotedOrList.mjs +1 -1
  69. package/jsutils/suggestionList.js +1 -1
  70. package/jsutils/suggestionList.mjs +1 -1
  71. package/language/blockStringValue.js +2 -2
  72. package/language/blockStringValue.js.flow +1 -1
  73. package/language/blockStringValue.mjs +2 -2
  74. package/language/directiveLocation.js +1 -1
  75. package/language/directiveLocation.mjs +1 -1
  76. package/language/index.mjs +1 -1
  77. package/language/kinds.js +1 -1
  78. package/language/kinds.mjs +1 -1
  79. package/language/lexer.js +29 -13
  80. package/language/lexer.js.flow +34 -20
  81. package/language/lexer.mjs +28 -13
  82. package/language/location.js +1 -1
  83. package/language/location.mjs +1 -1
  84. package/language/parser.js +31 -38
  85. package/language/parser.js.flow +32 -48
  86. package/language/parser.mjs +30 -38
  87. package/language/predicates.js +1 -1
  88. package/language/predicates.mjs +1 -1
  89. package/language/printer.js +13 -7
  90. package/language/printer.js.flow +19 -9
  91. package/language/printer.mjs +13 -7
  92. package/language/source.js +8 -7
  93. package/language/source.mjs +1 -9
  94. package/language/visitor.js +11 -20
  95. package/language/visitor.js.flow +4 -5
  96. package/language/visitor.mjs +8 -20
  97. package/package.json +10 -3
  98. package/polyfills/find.js +31 -0
  99. package/polyfills/find.js.flow +29 -0
  100. package/polyfills/find.mjs +23 -0
  101. package/{jsutils → polyfills}/isFinite.js +1 -1
  102. package/{jsutils → polyfills}/isFinite.js.flow +0 -0
  103. package/{jsutils → polyfills}/isFinite.mjs +1 -1
  104. package/{jsutils → polyfills}/isInteger.js +1 -1
  105. package/{jsutils → polyfills}/isInteger.js.flow +0 -0
  106. package/{jsutils → polyfills}/isInteger.mjs +1 -1
  107. package/polyfills/objectEntries.js +26 -0
  108. package/polyfills/objectEntries.js.flow +19 -0
  109. package/polyfills/objectEntries.mjs +18 -0
  110. package/{jsutils → polyfills}/objectValues.js +1 -1
  111. package/{jsutils → polyfills}/objectValues.js.flow +1 -1
  112. package/{jsutils → polyfills}/objectValues.mjs +1 -1
  113. package/subscription/asyncIteratorReject.mjs +1 -1
  114. package/subscription/index.mjs +1 -1
  115. package/subscription/mapAsyncIterator.mjs +1 -1
  116. package/subscription/subscribe.js +1 -1
  117. package/subscription/subscribe.js.flow +1 -1
  118. package/subscription/subscribe.mjs +2 -2
  119. package/type/definition.js +31 -130
  120. package/type/definition.js.flow +35 -47
  121. package/type/definition.mjs +30 -131
  122. package/type/directives.js +25 -29
  123. package/type/directives.js.flow +24 -20
  124. package/type/directives.mjs +22 -30
  125. package/type/index.js +12 -0
  126. package/type/index.js.flow +4 -0
  127. package/type/index.mjs +5 -3
  128. package/type/introspection.js +3 -3
  129. package/type/introspection.js.flow +1 -1
  130. package/type/introspection.mjs +2 -2
  131. package/type/scalars.js +45 -34
  132. package/type/scalars.js.flow +40 -26
  133. package/type/scalars.mjs +44 -26
  134. package/type/schema.js +82 -87
  135. package/type/schema.js.flow +17 -9
  136. package/type/schema.mjs +79 -86
  137. package/type/validate.js +168 -175
  138. package/type/validate.js.flow +8 -49
  139. package/type/validate.mjs +159 -175
  140. package/utilities/TypeInfo.js +10 -23
  141. package/utilities/TypeInfo.js.flow +1 -1
  142. package/utilities/TypeInfo.mjs +2 -24
  143. package/utilities/assertValidName.js +1 -1
  144. package/utilities/assertValidName.mjs +1 -1
  145. package/utilities/astFromValue.js +2 -2
  146. package/utilities/astFromValue.js.flow +1 -1
  147. package/utilities/astFromValue.mjs +2 -2
  148. package/utilities/buildASTSchema.js +169 -152
  149. package/utilities/buildASTSchema.js.flow +117 -113
  150. package/utilities/buildASTSchema.mjs +160 -152
  151. package/utilities/buildClientSchema.js +24 -14
  152. package/utilities/buildClientSchema.js.flow +24 -14
  153. package/utilities/buildClientSchema.mjs +23 -14
  154. package/utilities/coerceValue.js +38 -19
  155. package/utilities/coerceValue.js.flow +41 -47
  156. package/utilities/coerceValue.mjs +38 -20
  157. package/utilities/concatAST.js +1 -1
  158. package/utilities/concatAST.mjs +1 -1
  159. package/utilities/extendSchema.js +228 -312
  160. package/utilities/extendSchema.js.flow +27 -152
  161. package/utilities/extendSchema.mjs +229 -312
  162. package/utilities/findBreakingChanges.js +7 -5
  163. package/utilities/findBreakingChanges.js.flow +3 -2
  164. package/utilities/findBreakingChanges.mjs +6 -5
  165. package/utilities/findDeprecatedUsages.js +1 -1
  166. package/utilities/findDeprecatedUsages.mjs +1 -1
  167. package/utilities/getOperationAST.js +1 -1
  168. package/utilities/getOperationAST.mjs +1 -1
  169. package/utilities/getOperationRootType.js +1 -1
  170. package/utilities/getOperationRootType.mjs +1 -1
  171. package/utilities/index.mjs +1 -1
  172. package/utilities/introspectionFromSchema.js +1 -1
  173. package/utilities/introspectionFromSchema.mjs +1 -1
  174. package/utilities/introspectionQuery.js +1 -1
  175. package/utilities/introspectionQuery.mjs +1 -1
  176. package/utilities/isValidJSValue.js +1 -1
  177. package/utilities/isValidJSValue.mjs +1 -1
  178. package/utilities/isValidLiteralValue.js +1 -1
  179. package/utilities/isValidLiteralValue.mjs +1 -1
  180. package/utilities/lexicographicSortSchema.js +2 -2
  181. package/utilities/lexicographicSortSchema.js.flow +1 -1
  182. package/utilities/lexicographicSortSchema.mjs +2 -2
  183. package/utilities/schemaPrinter.js +3 -3
  184. package/utilities/schemaPrinter.js.flow +1 -1
  185. package/utilities/schemaPrinter.mjs +2 -2
  186. package/utilities/separateOperations.js +1 -1
  187. package/utilities/separateOperations.mjs +1 -1
  188. package/utilities/typeComparators.js +1 -1
  189. package/utilities/typeComparators.mjs +1 -1
  190. package/utilities/typeFromAST.js +1 -1
  191. package/utilities/typeFromAST.mjs +1 -1
  192. package/utilities/valueFromAST.js +3 -3
  193. package/utilities/valueFromAST.js.flow +1 -1
  194. package/utilities/valueFromAST.mjs +2 -2
  195. package/utilities/valueFromASTUntyped.js +1 -1
  196. package/utilities/valueFromASTUntyped.mjs +1 -1
  197. package/validation/ValidationContext.js +57 -82
  198. package/validation/ValidationContext.js.flow +52 -49
  199. package/validation/ValidationContext.mjs +52 -77
  200. package/validation/index.js.flow +1 -0
  201. package/validation/index.mjs +1 -1
  202. package/validation/rules/ExecutableDefinitions.js +1 -1
  203. package/validation/rules/ExecutableDefinitions.js.flow +1 -1
  204. package/validation/rules/ExecutableDefinitions.mjs +1 -1
  205. package/validation/rules/FieldsOnCorrectType.js +1 -1
  206. package/validation/rules/FieldsOnCorrectType.mjs +1 -1
  207. package/validation/rules/FragmentsOnCompositeTypes.js +1 -1
  208. package/validation/rules/FragmentsOnCompositeTypes.mjs +1 -1
  209. package/validation/rules/KnownArgumentNames.mjs +1 -1
  210. package/validation/rules/KnownDirectives.js +1 -1
  211. package/validation/rules/KnownDirectives.mjs +1 -1
  212. package/validation/rules/KnownFragmentNames.js +1 -1
  213. package/validation/rules/KnownFragmentNames.mjs +1 -1
  214. package/validation/rules/KnownTypeNames.js +59 -21
  215. package/validation/rules/KnownTypeNames.js.flow +54 -20
  216. package/validation/rules/KnownTypeNames.mjs +56 -21
  217. package/validation/rules/LoneAnonymousOperation.js +1 -1
  218. package/validation/rules/LoneAnonymousOperation.mjs +1 -1
  219. package/validation/rules/LoneSchemaDefinition.js +1 -1
  220. package/validation/rules/LoneSchemaDefinition.mjs +1 -1
  221. package/validation/rules/NoFragmentCycles.js +1 -1
  222. package/validation/rules/NoFragmentCycles.js.flow +2 -2
  223. package/validation/rules/NoFragmentCycles.mjs +1 -1
  224. package/validation/rules/NoUndefinedVariables.js +1 -1
  225. package/validation/rules/NoUndefinedVariables.mjs +1 -1
  226. package/validation/rules/NoUnusedFragments.js +1 -1
  227. package/validation/rules/NoUnusedFragments.js.flow +2 -2
  228. package/validation/rules/NoUnusedFragments.mjs +1 -1
  229. package/validation/rules/NoUnusedVariables.js +1 -1
  230. package/validation/rules/NoUnusedVariables.mjs +1 -1
  231. package/validation/rules/OverlappingFieldsCanBeMerged.js +57 -33
  232. package/validation/rules/OverlappingFieldsCanBeMerged.js.flow +5 -6
  233. package/validation/rules/OverlappingFieldsCanBeMerged.mjs +48 -33
  234. package/validation/rules/PossibleFragmentSpreads.js +1 -1
  235. package/validation/rules/PossibleFragmentSpreads.mjs +1 -1
  236. package/validation/rules/PossibleTypeExtensions.js +158 -0
  237. package/validation/rules/PossibleTypeExtensions.js.flow +160 -0
  238. package/validation/rules/PossibleTypeExtensions.mjs +146 -0
  239. package/validation/rules/ProvidedRequiredArguments.mjs +1 -1
  240. package/validation/rules/ScalarLeafs.js +1 -1
  241. package/validation/rules/ScalarLeafs.mjs +1 -1
  242. package/validation/rules/SingleFieldSubscriptions.js +1 -1
  243. package/validation/rules/SingleFieldSubscriptions.mjs +1 -1
  244. package/validation/rules/UniqueArgumentNames.js +1 -1
  245. package/validation/rules/UniqueArgumentNames.mjs +1 -1
  246. package/validation/rules/UniqueDirectiveNames.js +55 -0
  247. package/validation/rules/UniqueDirectiveNames.js.flow +64 -0
  248. package/validation/rules/UniqueDirectiveNames.mjs +43 -0
  249. package/validation/rules/UniqueDirectivesPerLocation.js +1 -1
  250. package/validation/rules/UniqueDirectivesPerLocation.mjs +1 -1
  251. package/validation/rules/UniqueEnumValueNames.js +94 -0
  252. package/validation/rules/UniqueEnumValueNames.js.flow +87 -0
  253. package/validation/rules/UniqueEnumValueNames.mjs +81 -0
  254. package/validation/rules/UniqueFieldDefinitionNames.js +105 -0
  255. package/validation/rules/UniqueFieldDefinitionNames.js.flow +101 -0
  256. package/validation/rules/UniqueFieldDefinitionNames.mjs +92 -0
  257. package/validation/rules/UniqueFragmentNames.js +1 -1
  258. package/validation/rules/UniqueFragmentNames.mjs +1 -1
  259. package/validation/rules/UniqueInputFieldNames.js +1 -1
  260. package/validation/rules/UniqueInputFieldNames.mjs +1 -1
  261. package/validation/rules/UniqueOperationNames.js +1 -1
  262. package/validation/rules/UniqueOperationNames.mjs +1 -1
  263. package/validation/rules/UniqueOperationTypes.js +85 -0
  264. package/validation/rules/UniqueOperationTypes.js.flow +76 -0
  265. package/validation/rules/UniqueOperationTypes.mjs +73 -0
  266. package/validation/rules/UniqueTypeNames.js +62 -0
  267. package/validation/rules/UniqueTypeNames.js.flow +67 -0
  268. package/validation/rules/UniqueTypeNames.mjs +50 -0
  269. package/validation/rules/UniqueVariableNames.js +1 -1
  270. package/validation/rules/UniqueVariableNames.mjs +1 -1
  271. package/validation/rules/ValuesOfCorrectType.js +29 -13
  272. package/validation/rules/ValuesOfCorrectType.js.flow +4 -5
  273. package/validation/rules/ValuesOfCorrectType.mjs +28 -13
  274. package/validation/rules/VariablesAreInputTypes.js +1 -1
  275. package/validation/rules/VariablesAreInputTypes.mjs +1 -1
  276. package/validation/rules/VariablesInAllowedPosition.js +4 -4
  277. package/validation/rules/VariablesInAllowedPosition.mjs +4 -4
  278. package/validation/specifiedRules.js +14 -2
  279. package/validation/specifiedRules.js.flow +13 -0
  280. package/validation/specifiedRules.mjs +8 -2
  281. package/validation/validate.js +1 -1
  282. package/validation/validate.mjs +1 -1
  283. package/jsutils/find.js +0 -22
  284. package/jsutils/find.js.flow +0 -19
  285. package/jsutils/find.mjs +0 -15
@@ -7,6 +7,7 @@
7
7
  * @flow strict
8
8
  */
9
9
 
10
+ import objectValues from '../polyfills/objectValues';
10
11
  import invariant from '../jsutils/invariant';
11
12
  import keyMap from '../jsutils/keyMap';
12
13
  import keyValMap from '../jsutils/keyValMap';
@@ -23,6 +24,7 @@ import { Kind } from '../language/kinds';
23
24
 
24
25
  import type {
25
26
  DocumentNode,
27
+ NameNode,
26
28
  TypeNode,
27
29
  NamedTypeNode,
28
30
  SchemaDefinitionNode,
@@ -48,8 +50,9 @@ import type {
48
50
  GraphQLType,
49
51
  GraphQLNamedType,
50
52
  GraphQLFieldConfig,
53
+ GraphQLArgumentConfig,
51
54
  GraphQLEnumValueConfig,
52
- GraphQLInputField,
55
+ GraphQLInputFieldConfig,
53
56
  } from '../type/definition';
54
57
 
55
58
  import {
@@ -128,21 +131,14 @@ export function buildASTSchema(
128
131
  }
129
132
 
130
133
  let schemaDef: ?SchemaDefinitionNode;
131
-
132
- const typeDefs: Array<TypeDefinitionNode> = [];
133
134
  const nodeMap: ObjMap<TypeDefinitionNode> = Object.create(null);
134
135
  const directiveDefs: Array<DirectiveDefinitionNode> = [];
135
- for (let i = 0; i < documentAST.definitions.length; i++) {
136
- const def = documentAST.definitions[i];
136
+
137
+ for (const def of documentAST.definitions) {
137
138
  if (def.kind === Kind.SCHEMA_DEFINITION) {
138
139
  schemaDef = def;
139
140
  } else if (isTypeDefinitionNode(def)) {
140
- const typeName = def.name.value;
141
- if (nodeMap[typeName]) {
142
- throw new Error(`Type "${typeName}" was defined more than once.`);
143
- }
144
- typeDefs.push(def);
145
- nodeMap[typeName] = def;
141
+ nodeMap[def.name.value] = def;
146
142
  } else if (def.kind === Kind.DIRECTIVE_DEFINITION) {
147
143
  directiveDefs.push(def);
148
144
  }
@@ -159,8 +155,8 @@ export function buildASTSchema(
159
155
  const definitionBuilder = new ASTDefinitionBuilder(
160
156
  nodeMap,
161
157
  options,
162
- typeRef => {
163
- throw new Error(`Type "${typeRef.name.value}" not found in document.`);
158
+ typeName => {
159
+ throw new Error(`Type "${typeName}" not found in document.`);
164
160
  },
165
161
  );
166
162
 
@@ -194,7 +190,7 @@ export function buildASTSchema(
194
190
  subscription: operationTypes.subscription
195
191
  ? (definitionBuilder.buildType(operationTypes.subscription): any)
196
192
  : null,
197
- types: typeDefs.map(node => definitionBuilder.buildType(node)),
193
+ types: objectValues(nodeMap).map(node => definitionBuilder.buildType(node)),
198
194
  directives,
199
195
  astNode: schemaDef,
200
196
  assumeValid: options && options.assumeValid,
@@ -204,24 +200,14 @@ export function buildASTSchema(
204
200
  function getOperationTypes(schema: SchemaDefinitionNode) {
205
201
  const opTypes = {};
206
202
  for (const operationType of schema.operationTypes) {
207
- const typeName = operationType.type.name.value;
208
- const operation = operationType.operation;
209
- if (opTypes[operation]) {
210
- throw new Error(`Must provide only one ${operation} type in schema.`);
211
- }
212
- if (!nodeMap[typeName]) {
213
- throw new Error(
214
- `Specified ${operation} type "${typeName}" not found in document.`,
215
- );
216
- }
217
- opTypes[operation] = operationType.type;
203
+ opTypes[operationType.operation] = operationType.type;
218
204
  }
219
205
  return opTypes;
220
206
  }
221
207
  }
222
208
 
223
209
  type TypeDefinitionsMap = ObjMap<TypeDefinitionNode>;
224
- type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType;
210
+ type TypeResolver = (typeName: string) => GraphQLNamedType;
225
211
 
226
212
  export class ASTDefinitionBuilder {
227
213
  _typeDefinitionsMap: TypeDefinitionsMap;
@@ -251,7 +237,7 @@ export class ASTDefinitionBuilder {
251
237
  const defNode = this._typeDefinitionsMap[typeName];
252
238
  this._cache[typeName] = defNode
253
239
  ? this._makeSchemaDef(defNode)
254
- : this._resolveType(node);
240
+ : this._resolveType(node.name.value);
255
241
  } else {
256
242
  this._cache[typeName] = this._makeSchemaDef(node);
257
243
  }
@@ -272,40 +258,52 @@ export class ASTDefinitionBuilder {
272
258
  return this.buildType(typeNode);
273
259
  }
274
260
 
275
- buildDirective(directiveNode: DirectiveDefinitionNode): GraphQLDirective {
261
+ buildDirective(directive: DirectiveDefinitionNode): GraphQLDirective {
262
+ const locations = directive.locations.map(
263
+ ({ value }) => ((value: any): DirectiveLocationEnum),
264
+ );
265
+
276
266
  return new GraphQLDirective({
277
- name: directiveNode.name.value,
278
- description: getDescription(directiveNode, this._options),
279
- locations: directiveNode.locations.map(
280
- node => ((node.value: any): DirectiveLocationEnum),
281
- ),
282
- args:
283
- directiveNode.arguments &&
284
- this._makeInputValues(directiveNode.arguments),
285
- astNode: directiveNode,
267
+ name: directive.name.value,
268
+ description: getDescription(directive, this._options),
269
+ locations,
270
+ args: keyByNameNode(directive.arguments || [], arg => this.buildArg(arg)),
271
+ astNode: directive,
286
272
  });
287
273
  }
288
274
 
289
- buildField(field: FieldDefinitionNode): GraphQLFieldConfig<*, *> {
275
+ buildField(field: FieldDefinitionNode): GraphQLFieldConfig<mixed, mixed> {
290
276
  return {
291
277
  // Note: While this could make assertions to get the correctly typed
292
278
  // value, that would throw immediately while type system validation
293
279
  // with validateSchema() will produce more actionable results.
294
280
  type: (this._buildWrappedType(field.type): any),
295
281
  description: getDescription(field, this._options),
296
- args: field.arguments && this._makeInputValues(field.arguments),
282
+ args: keyByNameNode(field.arguments || [], arg => this.buildArg(arg)),
297
283
  deprecationReason: getDeprecationReason(field),
298
284
  astNode: field,
299
285
  };
300
286
  }
301
287
 
302
- buildInputField(value: InputValueDefinitionNode): GraphQLInputField {
288
+ buildArg(value: InputValueDefinitionNode): GraphQLArgumentConfig {
289
+ // Note: While this could make assertions to get the correctly typed
290
+ // value, that would throw immediately while type system validation
291
+ // with validateSchema() will produce more actionable results.
292
+ const type: any = this._buildWrappedType(value.type);
293
+ return {
294
+ type,
295
+ description: getDescription(value, this._options),
296
+ defaultValue: valueFromAST(value.defaultValue, type),
297
+ astNode: value,
298
+ };
299
+ }
300
+
301
+ buildInputField(value: InputValueDefinitionNode): GraphQLInputFieldConfig {
303
302
  // Note: While this could make assertions to get the correctly typed
304
303
  // value, that would throw immediately while type system validation
305
304
  // with validateSchema() will produce more actionable results.
306
305
  const type: any = this._buildWrappedType(value.type);
307
306
  return {
308
- name: value.name.value,
309
307
  type,
310
308
  description: getDescription(value, this._options),
311
309
  defaultValue: valueFromAST(value.defaultValue, type),
@@ -321,121 +319,127 @@ export class ASTDefinitionBuilder {
321
319
  };
322
320
  }
323
321
 
324
- _makeSchemaDef(def: TypeDefinitionNode): GraphQLNamedType {
325
- switch (def.kind) {
322
+ _makeSchemaDef(astNode: TypeDefinitionNode): GraphQLNamedType {
323
+ switch (astNode.kind) {
326
324
  case Kind.OBJECT_TYPE_DEFINITION:
327
- return this._makeTypeDef(def);
325
+ return this._makeTypeDef(astNode);
328
326
  case Kind.INTERFACE_TYPE_DEFINITION:
329
- return this._makeInterfaceDef(def);
327
+ return this._makeInterfaceDef(astNode);
330
328
  case Kind.ENUM_TYPE_DEFINITION:
331
- return this._makeEnumDef(def);
329
+ return this._makeEnumDef(astNode);
332
330
  case Kind.UNION_TYPE_DEFINITION:
333
- return this._makeUnionDef(def);
331
+ return this._makeUnionDef(astNode);
334
332
  case Kind.SCALAR_TYPE_DEFINITION:
335
- return this._makeScalarDef(def);
333
+ return this._makeScalarDef(astNode);
336
334
  case Kind.INPUT_OBJECT_TYPE_DEFINITION:
337
- return this._makeInputObjectDef(def);
335
+ return this._makeInputObjectDef(astNode);
338
336
  default:
339
- throw new Error(`Type kind "${def.kind}" not supported.`);
337
+ throw new Error(`Type kind "${astNode.kind}" not supported.`);
340
338
  }
341
339
  }
342
340
 
343
- _makeTypeDef(def: ObjectTypeDefinitionNode) {
344
- const interfaces: ?$ReadOnlyArray<NamedTypeNode> = def.interfaces;
341
+ _makeTypeDef(astNode: ObjectTypeDefinitionNode) {
342
+ const interfaceNodes = astNode.interfaces;
343
+ const fieldNodes = astNode.fields;
344
+
345
+ // Note: While this could make assertions to get the correctly typed
346
+ // values below, that would throw immediately while type system
347
+ // validation with validateSchema() will produce more actionable results.
348
+ const interfaces =
349
+ interfaceNodes && interfaceNodes.length > 0
350
+ ? () => interfaceNodes.map(ref => (this.buildType(ref): any))
351
+ : [];
352
+
353
+ const fields =
354
+ fieldNodes && fieldNodes.length > 0
355
+ ? () => keyByNameNode(fieldNodes, field => this.buildField(field))
356
+ : Object.create(null);
357
+
345
358
  return new GraphQLObjectType({
346
- name: def.name.value,
347
- description: getDescription(def, this._options),
348
- fields: () => this._makeFieldDefMap(def),
349
- // Note: While this could make early assertions to get the correctly
350
- // typed values, that would throw immediately while type system
351
- // validation with validateSchema() will produce more actionable results.
352
- interfaces: interfaces
353
- ? () => interfaces.map(ref => (this.buildType(ref): any))
354
- : [],
355
- astNode: def,
359
+ name: astNode.name.value,
360
+ description: getDescription(astNode, this._options),
361
+ interfaces,
362
+ fields,
363
+ astNode,
356
364
  });
357
365
  }
358
366
 
359
- _makeFieldDefMap(
360
- def: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
361
- ) {
362
- return def.fields
363
- ? keyValMap(
364
- def.fields,
365
- field => field.name.value,
366
- field => this.buildField(field),
367
- )
368
- : {};
369
- }
367
+ _makeInterfaceDef(astNode: InterfaceTypeDefinitionNode) {
368
+ const fieldNodes = astNode.fields;
370
369
 
371
- _makeInputValues(values: $ReadOnlyArray<InputValueDefinitionNode>) {
372
- return keyValMap(
373
- values,
374
- value => value.name.value,
375
- value => this.buildInputField(value),
376
- );
377
- }
370
+ const fields =
371
+ fieldNodes && fieldNodes.length > 0
372
+ ? () => keyByNameNode(fieldNodes, field => this.buildField(field))
373
+ : Object.create(null);
378
374
 
379
- _makeInterfaceDef(def: InterfaceTypeDefinitionNode) {
380
375
  return new GraphQLInterfaceType({
381
- name: def.name.value,
382
- description: getDescription(def, this._options),
383
- fields: () => this._makeFieldDefMap(def),
384
- astNode: def,
376
+ name: astNode.name.value,
377
+ description: getDescription(astNode, this._options),
378
+ fields,
379
+ astNode,
385
380
  });
386
381
  }
387
382
 
388
- _makeEnumDef(def: EnumTypeDefinitionNode) {
383
+ _makeEnumDef(astNode: EnumTypeDefinitionNode) {
384
+ const valueNodes = astNode.values || [];
385
+
389
386
  return new GraphQLEnumType({
390
- name: def.name.value,
391
- description: getDescription(def, this._options),
392
- values: this._makeValueDefMap(def),
393
- astNode: def,
387
+ name: astNode.name.value,
388
+ description: getDescription(astNode, this._options),
389
+ values: keyByNameNode(valueNodes, value => this.buildEnumValue(value)),
390
+ astNode,
394
391
  });
395
392
  }
396
393
 
397
- _makeValueDefMap(def: EnumTypeDefinitionNode) {
398
- return def.values
399
- ? keyValMap(
400
- def.values,
401
- enumValue => enumValue.name.value,
402
- enumValue => this.buildEnumValue(enumValue),
403
- )
404
- : {};
405
- }
394
+ _makeUnionDef(astNode: UnionTypeDefinitionNode) {
395
+ const typeNodes = astNode.types;
396
+
397
+ // Note: While this could make assertions to get the correctly typed
398
+ // values below, that would throw immediately while type system
399
+ // validation with validateSchema() will produce more actionable results.
400
+ const types =
401
+ typeNodes && typeNodes.length > 0
402
+ ? () => typeNodes.map(ref => (this.buildType(ref): any))
403
+ : [];
406
404
 
407
- _makeUnionDef(def: UnionTypeDefinitionNode) {
408
- const types: ?$ReadOnlyArray<NamedTypeNode> = def.types;
409
405
  return new GraphQLUnionType({
410
- name: def.name.value,
411
- description: getDescription(def, this._options),
412
- // Note: While this could make assertions to get the correctly typed
413
- // values below, that would throw immediately while type system
414
- // validation with validateSchema() will produce more actionable results.
415
- types: types ? () => types.map(ref => (this.buildType(ref): any)) : [],
416
- astNode: def,
406
+ name: astNode.name.value,
407
+ description: getDescription(astNode, this._options),
408
+ types,
409
+ astNode,
417
410
  });
418
411
  }
419
412
 
420
- _makeScalarDef(def: ScalarTypeDefinitionNode) {
413
+ _makeScalarDef(astNode: ScalarTypeDefinitionNode) {
421
414
  return new GraphQLScalarType({
422
- name: def.name.value,
423
- description: getDescription(def, this._options),
424
- astNode: def,
415
+ name: astNode.name.value,
416
+ description: getDescription(astNode, this._options),
417
+ astNode,
425
418
  serialize: value => value,
426
419
  });
427
420
  }
428
421
 
429
422
  _makeInputObjectDef(def: InputObjectTypeDefinitionNode) {
423
+ const { fields } = def;
424
+
430
425
  return new GraphQLInputObjectType({
431
426
  name: def.name.value,
432
427
  description: getDescription(def, this._options),
433
- fields: () => (def.fields ? this._makeInputValues(def.fields) : {}),
428
+ fields: fields
429
+ ? () => keyByNameNode(fields, field => this.buildInputField(field))
430
+ : Object.create(null),
434
431
  astNode: def,
435
432
  });
436
433
  }
437
434
  }
438
435
 
436
+ function keyByNameNode<T: { +name: NameNode }, V>(
437
+ list: $ReadOnlyArray<T>,
438
+ valFn: (item: T) => V,
439
+ ): ObjMap<V> {
440
+ return keyValMap(list, ({ name }) => name.value, valFn);
441
+ }
442
+
439
443
  /**
440
444
  * Given a field or enum value node, returns the string value for the
441
445
  * deprecation reason.