UNPKG

30.6 kBJavaScriptView Raw
1function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
2
3function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
5function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
7import objectEntries from '../polyfills/objectEntries';
8import inspect from '../jsutils/inspect';
9import keyMap from '../jsutils/keyMap';
10import mapValue from '../jsutils/mapValue';
11import toObjMap from '../jsutils/toObjMap';
12import devAssert from '../jsutils/devAssert';
13import keyValMap from '../jsutils/keyValMap';
14import instanceOf from '../jsutils/instanceOf';
15import isObjectLike from '../jsutils/isObjectLike';
16import identityFunc from '../jsutils/identityFunc';
17import defineToJSON from '../jsutils/defineToJSON';
18import defineToStringTag from '../jsutils/defineToStringTag';
19import { Kind } from '../language/kinds';
20import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped';
21export function isType(type) {
22 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type) || isListType(type) || isNonNullType(type);
23}
24export function assertType(type) {
25 if (!isType(type)) {
26 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL type."));
27 }
28
29 return type;
30}
31/**
32 * There are predicates for each kind of GraphQL type.
33 */
34
35// eslint-disable-next-line no-redeclare
36export function isScalarType(type) {
37 return instanceOf(type, GraphQLScalarType);
38}
39export function assertScalarType(type) {
40 if (!isScalarType(type)) {
41 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Scalar type."));
42 }
43
44 return type;
45}
46// eslint-disable-next-line no-redeclare
47export function isObjectType(type) {
48 return instanceOf(type, GraphQLObjectType);
49}
50export function assertObjectType(type) {
51 if (!isObjectType(type)) {
52 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Object type."));
53 }
54
55 return type;
56}
57// eslint-disable-next-line no-redeclare
58export function isInterfaceType(type) {
59 return instanceOf(type, GraphQLInterfaceType);
60}
61export function assertInterfaceType(type) {
62 if (!isInterfaceType(type)) {
63 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Interface type."));
64 }
65
66 return type;
67}
68// eslint-disable-next-line no-redeclare
69export function isUnionType(type) {
70 return instanceOf(type, GraphQLUnionType);
71}
72export function assertUnionType(type) {
73 if (!isUnionType(type)) {
74 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Union type."));
75 }
76
77 return type;
78}
79// eslint-disable-next-line no-redeclare
80export function isEnumType(type) {
81 return instanceOf(type, GraphQLEnumType);
82}
83export function assertEnumType(type) {
84 if (!isEnumType(type)) {
85 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Enum type."));
86 }
87
88 return type;
89}
90// eslint-disable-next-line no-redeclare
91export function isInputObjectType(type) {
92 return instanceOf(type, GraphQLInputObjectType);
93}
94export function assertInputObjectType(type) {
95 if (!isInputObjectType(type)) {
96 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Input Object type."));
97 }
98
99 return type;
100}
101// eslint-disable-next-line no-redeclare
102export function isListType(type) {
103 return instanceOf(type, GraphQLList);
104}
105export function assertListType(type) {
106 if (!isListType(type)) {
107 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL List type."));
108 }
109
110 return type;
111}
112// eslint-disable-next-line no-redeclare
113export function isNonNullType(type) {
114 return instanceOf(type, GraphQLNonNull);
115}
116export function assertNonNullType(type) {
117 if (!isNonNullType(type)) {
118 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL Non-Null type."));
119 }
120
121 return type;
122}
123/**
124 * These types may be used as input types for arguments and directives.
125 */
126
127export function isInputType(type) {
128 return isScalarType(type) || isEnumType(type) || isInputObjectType(type) || isWrappingType(type) && isInputType(type.ofType);
129}
130export function assertInputType(type) {
131 if (!isInputType(type)) {
132 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL input type."));
133 }
134
135 return type;
136}
137/**
138 * These types may be used as output types as the result of fields.
139 */
140
141export function isOutputType(type) {
142 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isWrappingType(type) && isOutputType(type.ofType);
143}
144export function assertOutputType(type) {
145 if (!isOutputType(type)) {
146 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL output type."));
147 }
148
149 return type;
150}
151/**
152 * These types may describe types which may be leaf values.
153 */
154
155export function isLeafType(type) {
156 return isScalarType(type) || isEnumType(type);
157}
158export function assertLeafType(type) {
159 if (!isLeafType(type)) {
160 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL leaf type."));
161 }
162
163 return type;
164}
165/**
166 * These types may describe the parent context of a selection set.
167 */
168
169export function isCompositeType(type) {
170 return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
171}
172export function assertCompositeType(type) {
173 if (!isCompositeType(type)) {
174 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL composite type."));
175 }
176
177 return type;
178}
179/**
180 * These types may describe the parent context of a selection set.
181 */
182
183export function isAbstractType(type) {
184 return isInterfaceType(type) || isUnionType(type);
185}
186export function assertAbstractType(type) {
187 if (!isAbstractType(type)) {
188 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL abstract type."));
189 }
190
191 return type;
192}
193/**
194 * List Type Wrapper
195 *
196 * A list is a wrapping type which points to another type.
197 * Lists are often created within the context of defining the fields of
198 * an object type.
199 *
200 * Example:
201 *
202 * const PersonType = new GraphQLObjectType({
203 * name: 'Person',
204 * fields: () => ({
205 * parents: { type: GraphQLList(PersonType) },
206 * children: { type: GraphQLList(PersonType) },
207 * })
208 * })
209 *
210 */
211
212// eslint-disable-next-line no-redeclare
213export function GraphQLList(ofType) {
214 if (this instanceof GraphQLList) {
215 this.ofType = assertType(ofType);
216 } else {
217 return new GraphQLList(ofType);
218 }
219} // Need to cast through any to alter the prototype.
220
221GraphQLList.prototype.toString = function toString() {
222 return '[' + String(this.ofType) + ']';
223}; // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
224
225
226defineToStringTag(GraphQLList);
227defineToJSON(GraphQLList);
228/**
229 * Non-Null Type Wrapper
230 *
231 * A non-null is a wrapping type which points to another type.
232 * Non-null types enforce that their values are never null and can ensure
233 * an error is raised if this ever occurs during a request. It is useful for
234 * fields which you can make a strong guarantee on non-nullability, for example
235 * usually the id field of a database row will never be null.
236 *
237 * Example:
238 *
239 * const RowType = new GraphQLObjectType({
240 * name: 'Row',
241 * fields: () => ({
242 * id: { type: GraphQLNonNull(GraphQLString) },
243 * })
244 * })
245 *
246 * Note: the enforcement of non-nullability occurs within the executor.
247 */
248
249// eslint-disable-next-line no-redeclare
250export function GraphQLNonNull(ofType) {
251 if (this instanceof GraphQLNonNull) {
252 this.ofType = assertNullableType(ofType);
253 } else {
254 return new GraphQLNonNull(ofType);
255 }
256} // Need to cast through any to alter the prototype.
257
258GraphQLNonNull.prototype.toString = function toString() {
259 return String(this.ofType) + '!';
260}; // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
261
262
263defineToStringTag(GraphQLNonNull);
264defineToJSON(GraphQLNonNull);
265/**
266 * These types wrap and modify other types
267 */
268
269export function isWrappingType(type) {
270 return isListType(type) || isNonNullType(type);
271}
272export function assertWrappingType(type) {
273 if (!isWrappingType(type)) {
274 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL wrapping type."));
275 }
276
277 return type;
278}
279/**
280 * These types can all accept null as a value.
281 */
282
283export function isNullableType(type) {
284 return isType(type) && !isNonNullType(type);
285}
286export function assertNullableType(type) {
287 if (!isNullableType(type)) {
288 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL nullable type."));
289 }
290
291 return type;
292}
293/* eslint-disable no-redeclare */
294
295export function getNullableType(type) {
296 /* eslint-enable no-redeclare */
297 if (type) {
298 return isNonNullType(type) ? type.ofType : type;
299 }
300}
301/**
302 * These named types do not include modifiers like List or NonNull.
303 */
304
305export function isNamedType(type) {
306 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type);
307}
308export function assertNamedType(type) {
309 if (!isNamedType(type)) {
310 throw new Error("Expected ".concat(inspect(type), " to be a GraphQL named type."));
311 }
312
313 return type;
314}
315/* eslint-disable no-redeclare */
316
317export function getNamedType(type) {
318 /* eslint-enable no-redeclare */
319 if (type) {
320 var unwrappedType = type;
321
322 while (isWrappingType(unwrappedType)) {
323 unwrappedType = unwrappedType.ofType;
324 }
325
326 return unwrappedType;
327 }
328}
329/**
330 * Used while defining GraphQL types to allow for circular references in
331 * otherwise immutable type definitions.
332 */
333
334function resolveThunk(thunk) {
335 // $FlowFixMe(>=0.90.0)
336 return typeof thunk === 'function' ? thunk() : thunk;
337}
338
339function undefineIfEmpty(arr) {
340 return arr && arr.length > 0 ? arr : undefined;
341}
342/**
343 * Scalar Type Definition
344 *
345 * The leaf values of any request and input values to arguments are
346 * Scalars (or Enums) and are defined with a name and a series of functions
347 * used to parse input from ast or variables and to ensure validity.
348 *
349 * If a type's serialize function does not return a value (i.e. it returns
350 * `undefined`) then an error will be raised and a `null` value will be returned
351 * in the response. If the serialize function returns `null`, then no error will
352 * be included in the response.
353 *
354 * Example:
355 *
356 * const OddType = new GraphQLScalarType({
357 * name: 'Odd',
358 * serialize(value) {
359 * if (value % 2 === 1) {
360 * return value;
361 * }
362 * }
363 * });
364 *
365 */
366
367
368export var GraphQLScalarType =
369/*#__PURE__*/
370function () {
371 function GraphQLScalarType(config) {
372 var parseValue = config.parseValue || identityFunc;
373 this.name = config.name;
374 this.description = config.description;
375 this.serialize = config.serialize || identityFunc;
376 this.parseValue = parseValue;
377
378 this.parseLiteral = config.parseLiteral || function (node) {
379 return parseValue(valueFromASTUntyped(node));
380 };
381
382 this.extensions = config.extensions && toObjMap(config.extensions);
383 this.astNode = config.astNode;
384 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
385 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
386 config.serialize == null || typeof config.serialize === 'function' || devAssert(0, "".concat(this.name, " must provide \"serialize\" function. If this custom Scalar is also used as an input type, ensure \"parseValue\" and \"parseLiteral\" functions are also provided."));
387
388 if (config.parseLiteral) {
389 typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function' || devAssert(0, "".concat(this.name, " must provide both \"parseValue\" and \"parseLiteral\" functions."));
390 }
391 }
392
393 var _proto = GraphQLScalarType.prototype;
394
395 _proto.toConfig = function toConfig() {
396 return {
397 name: this.name,
398 description: this.description,
399 serialize: this.serialize,
400 parseValue: this.parseValue,
401 parseLiteral: this.parseLiteral,
402 extensions: this.extensions,
403 astNode: this.astNode,
404 extensionASTNodes: this.extensionASTNodes || []
405 };
406 };
407
408 _proto.toString = function toString() {
409 return this.name;
410 };
411
412 return GraphQLScalarType;
413}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
414
415defineToStringTag(GraphQLScalarType);
416defineToJSON(GraphQLScalarType);
417
418/**
419 * Object Type Definition
420 *
421 * Almost all of the GraphQL types you define will be object types. Object types
422 * have a name, but most importantly describe their fields.
423 *
424 * Example:
425 *
426 * const AddressType = new GraphQLObjectType({
427 * name: 'Address',
428 * fields: {
429 * street: { type: GraphQLString },
430 * number: { type: GraphQLInt },
431 * formatted: {
432 * type: GraphQLString,
433 * resolve(obj) {
434 * return obj.number + ' ' + obj.street
435 * }
436 * }
437 * }
438 * });
439 *
440 * When two types need to refer to each other, or a type needs to refer to
441 * itself in a field, you can use a function expression (aka a closure or a
442 * thunk) to supply the fields lazily.
443 *
444 * Example:
445 *
446 * const PersonType = new GraphQLObjectType({
447 * name: 'Person',
448 * fields: () => ({
449 * name: { type: GraphQLString },
450 * bestFriend: { type: PersonType },
451 * })
452 * });
453 *
454 */
455export var GraphQLObjectType =
456/*#__PURE__*/
457function () {
458 function GraphQLObjectType(config) {
459 this.name = config.name;
460 this.description = config.description;
461 this.isTypeOf = config.isTypeOf;
462 this.extensions = config.extensions && toObjMap(config.extensions);
463 this.astNode = config.astNode;
464 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
465 this._fields = defineFieldMap.bind(undefined, config);
466 this._interfaces = defineInterfaces.bind(undefined, config);
467 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
468 config.isTypeOf == null || typeof config.isTypeOf === 'function' || devAssert(0, "".concat(this.name, " must provide \"isTypeOf\" as a function, ") + "but got: ".concat(inspect(config.isTypeOf), "."));
469 }
470
471 var _proto2 = GraphQLObjectType.prototype;
472
473 _proto2.getFields = function getFields() {
474 if (typeof this._fields === 'function') {
475 this._fields = this._fields();
476 }
477
478 return this._fields;
479 };
480
481 _proto2.getInterfaces = function getInterfaces() {
482 if (typeof this._interfaces === 'function') {
483 this._interfaces = this._interfaces();
484 }
485
486 return this._interfaces;
487 };
488
489 _proto2.toConfig = function toConfig() {
490 return {
491 name: this.name,
492 description: this.description,
493 interfaces: this.getInterfaces(),
494 fields: fieldsToFieldsConfig(this.getFields()),
495 isTypeOf: this.isTypeOf,
496 extensions: this.extensions,
497 astNode: this.astNode,
498 extensionASTNodes: this.extensionASTNodes || []
499 };
500 };
501
502 _proto2.toString = function toString() {
503 return this.name;
504 };
505
506 return GraphQLObjectType;
507}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
508
509defineToStringTag(GraphQLObjectType);
510defineToJSON(GraphQLObjectType);
511
512function defineInterfaces(config) {
513 var interfaces = resolveThunk(config.interfaces) || [];
514 Array.isArray(interfaces) || devAssert(0, "".concat(config.name, " interfaces must be an Array or a function which returns an Array."));
515 return interfaces;
516}
517
518function defineFieldMap(config) {
519 var fieldMap = resolveThunk(config.fields) || {};
520 isPlainObj(fieldMap) || devAssert(0, "".concat(config.name, " fields must be an object with field names as keys or a function which returns such an object."));
521 return mapValue(fieldMap, function (fieldConfig, fieldName) {
522 isPlainObj(fieldConfig) || devAssert(0, "".concat(config.name, ".").concat(fieldName, " field config must be an object"));
523 !('isDeprecated' in fieldConfig) || devAssert(0, "".concat(config.name, ".").concat(fieldName, " should provide \"deprecationReason\" instead of \"isDeprecated\"."));
524 fieldConfig.resolve == null || typeof fieldConfig.resolve === 'function' || devAssert(0, "".concat(config.name, ".").concat(fieldName, " field resolver must be a function if ") + "provided, but got: ".concat(inspect(fieldConfig.resolve), "."));
525 var argsConfig = fieldConfig.args || {};
526 isPlainObj(argsConfig) || devAssert(0, "".concat(config.name, ".").concat(fieldName, " args must be an object with argument names as keys."));
527 var args = objectEntries(argsConfig).map(function (_ref) {
528 var argName = _ref[0],
529 arg = _ref[1];
530 return {
531 name: argName,
532 description: arg.description === undefined ? null : arg.description,
533 type: arg.type,
534 defaultValue: arg.defaultValue,
535 extensions: arg.extensions && toObjMap(arg.extensions),
536 astNode: arg.astNode
537 };
538 });
539 return _objectSpread({}, fieldConfig, {
540 name: fieldName,
541 description: fieldConfig.description,
542 type: fieldConfig.type,
543 args: args,
544 resolve: fieldConfig.resolve,
545 subscribe: fieldConfig.subscribe,
546 isDeprecated: Boolean(fieldConfig.deprecationReason),
547 deprecationReason: fieldConfig.deprecationReason,
548 extensions: fieldConfig.extensions && toObjMap(fieldConfig.extensions),
549 astNode: fieldConfig.astNode
550 });
551 });
552}
553
554function isPlainObj(obj) {
555 return isObjectLike(obj) && !Array.isArray(obj);
556}
557
558function fieldsToFieldsConfig(fields) {
559 return mapValue(fields, function (field) {
560 return {
561 description: field.description,
562 type: field.type,
563 args: argsToArgsConfig(field.args),
564 resolve: field.resolve,
565 subscribe: field.subscribe,
566 deprecationReason: field.deprecationReason,
567 extensions: field.extensions,
568 astNode: field.astNode
569 };
570 });
571}
572
573export function argsToArgsConfig(args) {
574 return keyValMap(args, function (arg) {
575 return arg.name;
576 }, function (arg) {
577 return {
578 description: arg.description,
579 type: arg.type,
580 defaultValue: arg.defaultValue,
581 extensions: arg.extensions,
582 astNode: arg.astNode
583 };
584 });
585}
586export function isRequiredArgument(arg) {
587 return isNonNullType(arg.type) && arg.defaultValue === undefined;
588}
589
590/**
591 * Interface Type Definition
592 *
593 * When a field can return one of a heterogeneous set of types, a Interface type
594 * is used to describe what types are possible, what fields are in common across
595 * all types, as well as a function to determine which type is actually used
596 * when the field is resolved.
597 *
598 * Example:
599 *
600 * const EntityType = new GraphQLInterfaceType({
601 * name: 'Entity',
602 * fields: {
603 * name: { type: GraphQLString }
604 * }
605 * });
606 *
607 */
608export var GraphQLInterfaceType =
609/*#__PURE__*/
610function () {
611 function GraphQLInterfaceType(config) {
612 this.name = config.name;
613 this.description = config.description;
614 this.resolveType = config.resolveType;
615 this.extensions = config.extensions && toObjMap(config.extensions);
616 this.astNode = config.astNode;
617 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
618 this._fields = defineFieldMap.bind(undefined, config);
619 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
620 config.resolveType == null || typeof config.resolveType === 'function' || devAssert(0, "".concat(this.name, " must provide \"resolveType\" as a function, ") + "but got: ".concat(inspect(config.resolveType), "."));
621 }
622
623 var _proto3 = GraphQLInterfaceType.prototype;
624
625 _proto3.getFields = function getFields() {
626 if (typeof this._fields === 'function') {
627 this._fields = this._fields();
628 }
629
630 return this._fields;
631 };
632
633 _proto3.toConfig = function toConfig() {
634 return {
635 name: this.name,
636 description: this.description,
637 fields: fieldsToFieldsConfig(this.getFields()),
638 resolveType: this.resolveType,
639 extensions: this.extensions,
640 astNode: this.astNode,
641 extensionASTNodes: this.extensionASTNodes || []
642 };
643 };
644
645 _proto3.toString = function toString() {
646 return this.name;
647 };
648
649 return GraphQLInterfaceType;
650}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
651
652defineToStringTag(GraphQLInterfaceType);
653defineToJSON(GraphQLInterfaceType);
654
655/**
656 * Union Type Definition
657 *
658 * When a field can return one of a heterogeneous set of types, a Union type
659 * is used to describe what types are possible as well as providing a function
660 * to determine which type is actually used when the field is resolved.
661 *
662 * Example:
663 *
664 * const PetType = new GraphQLUnionType({
665 * name: 'Pet',
666 * types: [ DogType, CatType ],
667 * resolveType(value) {
668 * if (value instanceof Dog) {
669 * return DogType;
670 * }
671 * if (value instanceof Cat) {
672 * return CatType;
673 * }
674 * }
675 * });
676 *
677 */
678export var GraphQLUnionType =
679/*#__PURE__*/
680function () {
681 function GraphQLUnionType(config) {
682 this.name = config.name;
683 this.description = config.description;
684 this.resolveType = config.resolveType;
685 this.extensions = config.extensions && toObjMap(config.extensions);
686 this.astNode = config.astNode;
687 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
688 this._types = defineTypes.bind(undefined, config);
689 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
690 config.resolveType == null || typeof config.resolveType === 'function' || devAssert(0, "".concat(this.name, " must provide \"resolveType\" as a function, ") + "but got: ".concat(inspect(config.resolveType), "."));
691 }
692
693 var _proto4 = GraphQLUnionType.prototype;
694
695 _proto4.getTypes = function getTypes() {
696 if (typeof this._types === 'function') {
697 this._types = this._types();
698 }
699
700 return this._types;
701 };
702
703 _proto4.toConfig = function toConfig() {
704 return {
705 name: this.name,
706 description: this.description,
707 types: this.getTypes(),
708 resolveType: this.resolveType,
709 extensions: this.extensions,
710 astNode: this.astNode,
711 extensionASTNodes: this.extensionASTNodes || []
712 };
713 };
714
715 _proto4.toString = function toString() {
716 return this.name;
717 };
718
719 return GraphQLUnionType;
720}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
721
722defineToStringTag(GraphQLUnionType);
723defineToJSON(GraphQLUnionType);
724
725function defineTypes(config) {
726 var types = resolveThunk(config.types) || [];
727 Array.isArray(types) || devAssert(0, "Must provide Array of types or a function which returns such an array for Union ".concat(config.name, "."));
728 return types;
729}
730
731/**
732 * Enum Type Definition
733 *
734 * Some leaf values of requests and input values are Enums. GraphQL serializes
735 * Enum values as strings, however internally Enums can be represented by any
736 * kind of type, often integers.
737 *
738 * Example:
739 *
740 * const RGBType = new GraphQLEnumType({
741 * name: 'RGB',
742 * values: {
743 * RED: { value: 0 },
744 * GREEN: { value: 1 },
745 * BLUE: { value: 2 }
746 * }
747 * });
748 *
749 * Note: If a value is not provided in a definition, the name of the enum value
750 * will be used as its internal value.
751 */
752export var GraphQLEnumType
753/* <T> */
754=
755/*#__PURE__*/
756function () {
757 function GraphQLEnumType(config) {
758 this.name = config.name;
759 this.description = config.description;
760 this.extensions = config.extensions && toObjMap(config.extensions);
761 this.astNode = config.astNode;
762 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
763 this._values = defineEnumValues(this.name, config.values);
764 this._valueLookup = new Map(this._values.map(function (enumValue) {
765 return [enumValue.value, enumValue];
766 }));
767 this._nameLookup = keyMap(this._values, function (value) {
768 return value.name;
769 });
770 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
771 }
772
773 var _proto5 = GraphQLEnumType.prototype;
774
775 _proto5.getValues = function getValues() {
776 return this._values;
777 };
778
779 _proto5.getValue = function getValue(name) {
780 return this._nameLookup[name];
781 };
782
783 _proto5.serialize = function serialize(value) {
784 var enumValue = this._valueLookup.get(value);
785
786 if (enumValue) {
787 return enumValue.name;
788 }
789 };
790
791 _proto5.parseValue = function parseValue(value)
792 /* T */
793 {
794 if (typeof value === 'string') {
795 var enumValue = this.getValue(value);
796
797 if (enumValue) {
798 return enumValue.value;
799 }
800 }
801 };
802
803 _proto5.parseLiteral = function parseLiteral(valueNode, _variables)
804 /* T */
805 {
806 // Note: variables will be resolved to a value before calling this function.
807 if (valueNode.kind === Kind.ENUM) {
808 var enumValue = this.getValue(valueNode.value);
809
810 if (enumValue) {
811 return enumValue.value;
812 }
813 }
814 };
815
816 _proto5.toConfig = function toConfig() {
817 var values = keyValMap(this.getValues(), function (value) {
818 return value.name;
819 }, function (value) {
820 return {
821 description: value.description,
822 value: value.value,
823 deprecationReason: value.deprecationReason,
824 extensions: value.extensions,
825 astNode: value.astNode
826 };
827 });
828 return {
829 name: this.name,
830 description: this.description,
831 values: values,
832 extensions: this.extensions,
833 astNode: this.astNode,
834 extensionASTNodes: this.extensionASTNodes || []
835 };
836 };
837
838 _proto5.toString = function toString() {
839 return this.name;
840 };
841
842 return GraphQLEnumType;
843}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
844
845defineToStringTag(GraphQLEnumType);
846defineToJSON(GraphQLEnumType);
847
848function defineEnumValues(typeName, valueMap) {
849 isPlainObj(valueMap) || devAssert(0, "".concat(typeName, " values must be an object with value names as keys."));
850 return objectEntries(valueMap).map(function (_ref2) {
851 var valueName = _ref2[0],
852 value = _ref2[1];
853 isPlainObj(value) || devAssert(0, "".concat(typeName, ".").concat(valueName, " must refer to an object with a \"value\" key ") + "representing an internal value but got: ".concat(inspect(value), "."));
854 !('isDeprecated' in value) || devAssert(0, "".concat(typeName, ".").concat(valueName, " should provide \"deprecationReason\" instead of \"isDeprecated\"."));
855 return {
856 name: valueName,
857 description: value.description,
858 value: 'value' in value ? value.value : valueName,
859 isDeprecated: Boolean(value.deprecationReason),
860 deprecationReason: value.deprecationReason,
861 extensions: value.extensions && toObjMap(value.extensions),
862 astNode: value.astNode
863 };
864 });
865}
866
867/**
868 * Input Object Type Definition
869 *
870 * An input object defines a structured collection of fields which may be
871 * supplied to a field argument.
872 *
873 * Using `NonNull` will ensure that a value must be provided by the query
874 *
875 * Example:
876 *
877 * const GeoPoint = new GraphQLInputObjectType({
878 * name: 'GeoPoint',
879 * fields: {
880 * lat: { type: GraphQLNonNull(GraphQLFloat) },
881 * lon: { type: GraphQLNonNull(GraphQLFloat) },
882 * alt: { type: GraphQLFloat, defaultValue: 0 },
883 * }
884 * });
885 *
886 */
887export var GraphQLInputObjectType =
888/*#__PURE__*/
889function () {
890 function GraphQLInputObjectType(config) {
891 this.name = config.name;
892 this.description = config.description;
893 this.extensions = config.extensions && toObjMap(config.extensions);
894 this.astNode = config.astNode;
895 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
896 this._fields = defineInputFieldMap.bind(undefined, config);
897 typeof config.name === 'string' || devAssert(0, 'Must provide name.');
898 }
899
900 var _proto6 = GraphQLInputObjectType.prototype;
901
902 _proto6.getFields = function getFields() {
903 if (typeof this._fields === 'function') {
904 this._fields = this._fields();
905 }
906
907 return this._fields;
908 };
909
910 _proto6.toConfig = function toConfig() {
911 var fields = mapValue(this.getFields(), function (field) {
912 return {
913 description: field.description,
914 type: field.type,
915 defaultValue: field.defaultValue,
916 extensions: field.extensions,
917 astNode: field.astNode
918 };
919 });
920 return {
921 name: this.name,
922 description: this.description,
923 fields: fields,
924 extensions: this.extensions,
925 astNode: this.astNode,
926 extensionASTNodes: this.extensionASTNodes || []
927 };
928 };
929
930 _proto6.toString = function toString() {
931 return this.name;
932 };
933
934 return GraphQLInputObjectType;
935}(); // Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
936
937defineToStringTag(GraphQLInputObjectType);
938defineToJSON(GraphQLInputObjectType);
939
940function defineInputFieldMap(config) {
941 var fieldMap = resolveThunk(config.fields) || {};
942 isPlainObj(fieldMap) || devAssert(0, "".concat(config.name, " fields must be an object with field names as keys or a function which returns such an object."));
943 return mapValue(fieldMap, function (fieldConfig, fieldName) {
944 !('resolve' in fieldConfig) || devAssert(0, "".concat(config.name, ".").concat(fieldName, " field has a resolve property, but Input Types cannot define resolvers."));
945 return _objectSpread({}, fieldConfig, {
946 name: fieldName,
947 description: fieldConfig.description,
948 type: fieldConfig.type,
949 defaultValue: fieldConfig.defaultValue,
950 extensions: fieldConfig.extensions && toObjMap(fieldConfig.extensions),
951 astNode: fieldConfig.astNode
952 });
953 });
954}
955
956export function isRequiredInputField(field) {
957 return isNonNullType(field.type) && field.defaultValue === undefined;
958}