UNPKG

40 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isType = isType;
7exports.assertType = assertType;
8exports.isScalarType = isScalarType;
9exports.assertScalarType = assertScalarType;
10exports.isObjectType = isObjectType;
11exports.assertObjectType = assertObjectType;
12exports.isInterfaceType = isInterfaceType;
13exports.assertInterfaceType = assertInterfaceType;
14exports.isUnionType = isUnionType;
15exports.assertUnionType = assertUnionType;
16exports.isEnumType = isEnumType;
17exports.assertEnumType = assertEnumType;
18exports.isInputObjectType = isInputObjectType;
19exports.assertInputObjectType = assertInputObjectType;
20exports.isListType = isListType;
21exports.assertListType = assertListType;
22exports.isNonNullType = isNonNullType;
23exports.assertNonNullType = assertNonNullType;
24exports.isInputType = isInputType;
25exports.assertInputType = assertInputType;
26exports.isOutputType = isOutputType;
27exports.assertOutputType = assertOutputType;
28exports.isLeafType = isLeafType;
29exports.assertLeafType = assertLeafType;
30exports.isCompositeType = isCompositeType;
31exports.assertCompositeType = assertCompositeType;
32exports.isAbstractType = isAbstractType;
33exports.assertAbstractType = assertAbstractType;
34exports.GraphQLList = GraphQLList;
35exports.GraphQLNonNull = GraphQLNonNull;
36exports.isWrappingType = isWrappingType;
37exports.assertWrappingType = assertWrappingType;
38exports.isNullableType = isNullableType;
39exports.assertNullableType = assertNullableType;
40exports.getNullableType = getNullableType;
41exports.isNamedType = isNamedType;
42exports.assertNamedType = assertNamedType;
43exports.getNamedType = getNamedType;
44exports.argsToArgsConfig = argsToArgsConfig;
45exports.isRequiredArgument = isRequiredArgument;
46exports.isRequiredInputField = isRequiredInputField;
47exports.GraphQLInputObjectType = exports.GraphQLEnumType = exports.GraphQLUnionType = exports.GraphQLInterfaceType = exports.GraphQLObjectType = exports.GraphQLScalarType = void 0;
48
49var _objectEntries = _interopRequireDefault(require("../polyfills/objectEntries.js"));
50
51var _symbols = require("../polyfills/symbols.js");
52
53var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
54
55var _keyMap = _interopRequireDefault(require("../jsutils/keyMap.js"));
56
57var _mapValue = _interopRequireDefault(require("../jsutils/mapValue.js"));
58
59var _toObjMap = _interopRequireDefault(require("../jsutils/toObjMap.js"));
60
61var _devAssert = _interopRequireDefault(require("../jsutils/devAssert.js"));
62
63var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap.js"));
64
65var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf.js"));
66
67var _didYouMean = _interopRequireDefault(require("../jsutils/didYouMean.js"));
68
69var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
70
71var _identityFunc = _interopRequireDefault(require("../jsutils/identityFunc.js"));
72
73var _defineInspect = _interopRequireDefault(require("../jsutils/defineInspect.js"));
74
75var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList.js"));
76
77var _GraphQLError = require("../error/GraphQLError.js");
78
79var _kinds = require("../language/kinds.js");
80
81var _printer = require("../language/printer.js");
82
83var _valueFromASTUntyped = require("../utilities/valueFromASTUntyped.js");
84
85function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
86
87function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
88
89function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
90
91function isType(type) {
92 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type) || isListType(type) || isNonNullType(type);
93}
94
95function assertType(type) {
96 if (!isType(type)) {
97 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL type."));
98 }
99
100 return type;
101}
102/**
103 * There are predicates for each kind of GraphQL type.
104 */
105
106
107// eslint-disable-next-line no-redeclare
108function isScalarType(type) {
109 return (0, _instanceOf.default)(type, GraphQLScalarType);
110}
111
112function assertScalarType(type) {
113 if (!isScalarType(type)) {
114 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Scalar type."));
115 }
116
117 return type;
118}
119
120// eslint-disable-next-line no-redeclare
121function isObjectType(type) {
122 return (0, _instanceOf.default)(type, GraphQLObjectType);
123}
124
125function assertObjectType(type) {
126 if (!isObjectType(type)) {
127 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Object type."));
128 }
129
130 return type;
131}
132
133// eslint-disable-next-line no-redeclare
134function isInterfaceType(type) {
135 return (0, _instanceOf.default)(type, GraphQLInterfaceType);
136}
137
138function assertInterfaceType(type) {
139 if (!isInterfaceType(type)) {
140 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Interface type."));
141 }
142
143 return type;
144}
145
146// eslint-disable-next-line no-redeclare
147function isUnionType(type) {
148 return (0, _instanceOf.default)(type, GraphQLUnionType);
149}
150
151function assertUnionType(type) {
152 if (!isUnionType(type)) {
153 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Union type."));
154 }
155
156 return type;
157}
158
159// eslint-disable-next-line no-redeclare
160function isEnumType(type) {
161 return (0, _instanceOf.default)(type, GraphQLEnumType);
162}
163
164function assertEnumType(type) {
165 if (!isEnumType(type)) {
166 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Enum type."));
167 }
168
169 return type;
170}
171
172// eslint-disable-next-line no-redeclare
173function isInputObjectType(type) {
174 return (0, _instanceOf.default)(type, GraphQLInputObjectType);
175}
176
177function assertInputObjectType(type) {
178 if (!isInputObjectType(type)) {
179 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Input Object type."));
180 }
181
182 return type;
183}
184
185// eslint-disable-next-line no-redeclare
186function isListType(type) {
187 return (0, _instanceOf.default)(type, GraphQLList);
188}
189
190function assertListType(type) {
191 if (!isListType(type)) {
192 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL List type."));
193 }
194
195 return type;
196}
197
198// eslint-disable-next-line no-redeclare
199function isNonNullType(type) {
200 return (0, _instanceOf.default)(type, GraphQLNonNull);
201}
202
203function assertNonNullType(type) {
204 if (!isNonNullType(type)) {
205 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL Non-Null type."));
206 }
207
208 return type;
209}
210/**
211 * These types may be used as input types for arguments and directives.
212 */
213
214
215function isInputType(type) {
216 return isScalarType(type) || isEnumType(type) || isInputObjectType(type) || isWrappingType(type) && isInputType(type.ofType);
217}
218
219function assertInputType(type) {
220 if (!isInputType(type)) {
221 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL input type."));
222 }
223
224 return type;
225}
226/**
227 * These types may be used as output types as the result of fields.
228 */
229
230
231function isOutputType(type) {
232 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isWrappingType(type) && isOutputType(type.ofType);
233}
234
235function assertOutputType(type) {
236 if (!isOutputType(type)) {
237 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL output type."));
238 }
239
240 return type;
241}
242/**
243 * These types may describe types which may be leaf values.
244 */
245
246
247function isLeafType(type) {
248 return isScalarType(type) || isEnumType(type);
249}
250
251function assertLeafType(type) {
252 if (!isLeafType(type)) {
253 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL leaf type."));
254 }
255
256 return type;
257}
258/**
259 * These types may describe the parent context of a selection set.
260 */
261
262
263function isCompositeType(type) {
264 return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
265}
266
267function assertCompositeType(type) {
268 if (!isCompositeType(type)) {
269 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL composite type."));
270 }
271
272 return type;
273}
274/**
275 * These types may describe the parent context of a selection set.
276 */
277
278
279function isAbstractType(type) {
280 return isInterfaceType(type) || isUnionType(type);
281}
282
283function assertAbstractType(type) {
284 if (!isAbstractType(type)) {
285 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL abstract type."));
286 }
287
288 return type;
289}
290/**
291 * List Type Wrapper
292 *
293 * A list is a wrapping type which points to another type.
294 * Lists are often created within the context of defining the fields of
295 * an object type.
296 *
297 * Example:
298 *
299 * const PersonType = new GraphQLObjectType({
300 * name: 'Person',
301 * fields: () => ({
302 * parents: { type: new GraphQLList(PersonType) },
303 * children: { type: new GraphQLList(PersonType) },
304 * })
305 * })
306 *
307 */
308// FIXME: workaround to fix issue with Babel parser
309
310/* ::
311declare class GraphQLList<+T: GraphQLType> {
312 +ofType: T;
313 static <T>(ofType: T): GraphQLList<T>;
314 // Note: constructors cannot be used for covariant types. Drop the "new".
315 constructor(ofType: GraphQLType): void;
316}
317*/
318
319
320function GraphQLList(ofType) {
321 // istanbul ignore else (to be removed in v16.0.0)
322 if (this instanceof GraphQLList) {
323 this.ofType = assertType(ofType);
324 } else {
325 return new GraphQLList(ofType);
326 }
327} // Need to cast through any to alter the prototype.
328
329
330GraphQLList.prototype.toString = function toString() {
331 return '[' + String(this.ofType) + ']';
332};
333
334GraphQLList.prototype.toJSON = function toJSON() {
335 return this.toString();
336};
337
338Object.defineProperty(GraphQLList.prototype, _symbols.SYMBOL_TO_STRING_TAG, {
339 get: function get() {
340 return 'GraphQLList';
341 }
342}); // Print a simplified form when appearing in `inspect` and `util.inspect`.
343
344(0, _defineInspect.default)(GraphQLList);
345/**
346 * Non-Null Type Wrapper
347 *
348 * A non-null is a wrapping type which points to another type.
349 * Non-null types enforce that their values are never null and can ensure
350 * an error is raised if this ever occurs during a request. It is useful for
351 * fields which you can make a strong guarantee on non-nullability, for example
352 * usually the id field of a database row will never be null.
353 *
354 * Example:
355 *
356 * const RowType = new GraphQLObjectType({
357 * name: 'Row',
358 * fields: () => ({
359 * id: { type: new GraphQLNonNull(GraphQLString) },
360 * })
361 * })
362 *
363 * Note: the enforcement of non-nullability occurs within the executor.
364 */
365// FIXME: workaround to fix issue with Babel parser
366
367/* ::
368declare class GraphQLNonNull<+T: GraphQLNullableType> {
369 +ofType: T;
370 static <T>(ofType: T): GraphQLNonNull<T>;
371 // Note: constructors cannot be used for covariant types. Drop the "new".
372 constructor(ofType: GraphQLType): void;
373}
374*/
375
376function GraphQLNonNull(ofType) {
377 // istanbul ignore else (to be removed in v16.0.0)
378 if (this instanceof GraphQLNonNull) {
379 this.ofType = assertNullableType(ofType);
380 } else {
381 return new GraphQLNonNull(ofType);
382 }
383} // Need to cast through any to alter the prototype.
384
385
386GraphQLNonNull.prototype.toString = function toString() {
387 return String(this.ofType) + '!';
388};
389
390GraphQLNonNull.prototype.toJSON = function toJSON() {
391 return this.toString();
392};
393
394Object.defineProperty(GraphQLNonNull.prototype, _symbols.SYMBOL_TO_STRING_TAG, {
395 get: function get() {
396 return 'GraphQLNonNull';
397 }
398}); // Print a simplified form when appearing in `inspect` and `util.inspect`.
399
400(0, _defineInspect.default)(GraphQLNonNull);
401/**
402 * These types wrap and modify other types
403 */
404
405function isWrappingType(type) {
406 return isListType(type) || isNonNullType(type);
407}
408
409function assertWrappingType(type) {
410 if (!isWrappingType(type)) {
411 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL wrapping type."));
412 }
413
414 return type;
415}
416/**
417 * These types can all accept null as a value.
418 */
419
420
421function isNullableType(type) {
422 return isType(type) && !isNonNullType(type);
423}
424
425function assertNullableType(type) {
426 if (!isNullableType(type)) {
427 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL nullable type."));
428 }
429
430 return type;
431}
432/* eslint-disable no-redeclare */
433
434
435function getNullableType(type) {
436 /* eslint-enable no-redeclare */
437 if (type) {
438 return isNonNullType(type) ? type.ofType : type;
439 }
440}
441/**
442 * These named types do not include modifiers like List or NonNull.
443 */
444
445
446function isNamedType(type) {
447 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type);
448}
449
450function assertNamedType(type) {
451 if (!isNamedType(type)) {
452 throw new Error("Expected ".concat((0, _inspect.default)(type), " to be a GraphQL named type."));
453 }
454
455 return type;
456}
457/* eslint-disable no-redeclare */
458
459
460function getNamedType(type) {
461 /* eslint-enable no-redeclare */
462 if (type) {
463 var unwrappedType = type;
464
465 while (isWrappingType(unwrappedType)) {
466 unwrappedType = unwrappedType.ofType;
467 }
468
469 return unwrappedType;
470 }
471}
472/**
473 * Used while defining GraphQL types to allow for circular references in
474 * otherwise immutable type definitions.
475 */
476
477
478function resolveThunk(thunk) {
479 // $FlowFixMe[incompatible-use]
480 return typeof thunk === 'function' ? thunk() : thunk;
481}
482
483function undefineIfEmpty(arr) {
484 return arr && arr.length > 0 ? arr : undefined;
485}
486/**
487 * Scalar Type Definition
488 *
489 * The leaf values of any request and input values to arguments are
490 * Scalars (or Enums) and are defined with a name and a series of functions
491 * used to parse input from ast or variables and to ensure validity.
492 *
493 * If a type's serialize function does not return a value (i.e. it returns
494 * `undefined`) then an error will be raised and a `null` value will be returned
495 * in the response. If the serialize function returns `null`, then no error will
496 * be included in the response.
497 *
498 * Example:
499 *
500 * const OddType = new GraphQLScalarType({
501 * name: 'Odd',
502 * serialize(value) {
503 * if (value % 2 === 1) {
504 * return value;
505 * }
506 * }
507 * });
508 *
509 */
510
511
512var GraphQLScalarType = /*#__PURE__*/function () {
513 function GraphQLScalarType(config) {
514 var _config$parseValue, _config$serialize, _config$parseLiteral;
515
516 var parseValue = (_config$parseValue = config.parseValue) !== null && _config$parseValue !== void 0 ? _config$parseValue : _identityFunc.default;
517 this.name = config.name;
518 this.description = config.description;
519 this.specifiedByUrl = config.specifiedByUrl;
520 this.serialize = (_config$serialize = config.serialize) !== null && _config$serialize !== void 0 ? _config$serialize : _identityFunc.default;
521 this.parseValue = parseValue;
522 this.parseLiteral = (_config$parseLiteral = config.parseLiteral) !== null && _config$parseLiteral !== void 0 ? _config$parseLiteral : function (node, variables) {
523 return parseValue((0, _valueFromASTUntyped.valueFromASTUntyped)(node, variables));
524 };
525 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
526 this.astNode = config.astNode;
527 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
528 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
529 config.specifiedByUrl == null || typeof config.specifiedByUrl === 'string' || (0, _devAssert.default)(0, "".concat(this.name, " must provide \"specifiedByUrl\" as a string, ") + "but got: ".concat((0, _inspect.default)(config.specifiedByUrl), "."));
530 config.serialize == null || typeof config.serialize === 'function' || (0, _devAssert.default)(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."));
531
532 if (config.parseLiteral) {
533 typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function' || (0, _devAssert.default)(0, "".concat(this.name, " must provide both \"parseValue\" and \"parseLiteral\" functions."));
534 }
535 }
536
537 var _proto = GraphQLScalarType.prototype;
538
539 _proto.toConfig = function toConfig() {
540 var _this$extensionASTNod;
541
542 return {
543 name: this.name,
544 description: this.description,
545 specifiedByUrl: this.specifiedByUrl,
546 serialize: this.serialize,
547 parseValue: this.parseValue,
548 parseLiteral: this.parseLiteral,
549 extensions: this.extensions,
550 astNode: this.astNode,
551 extensionASTNodes: (_this$extensionASTNod = this.extensionASTNodes) !== null && _this$extensionASTNod !== void 0 ? _this$extensionASTNod : []
552 };
553 };
554
555 _proto.toString = function toString() {
556 return this.name;
557 };
558
559 _proto.toJSON = function toJSON() {
560 return this.toString();
561 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
562 ;
563
564 _createClass(GraphQLScalarType, [{
565 key: _symbols.SYMBOL_TO_STRING_TAG,
566 get: function get() {
567 return 'GraphQLScalarType';
568 }
569 }]);
570
571 return GraphQLScalarType;
572}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
573
574
575exports.GraphQLScalarType = GraphQLScalarType;
576(0, _defineInspect.default)(GraphQLScalarType);
577
578/**
579 * Object Type Definition
580 *
581 * Almost all of the GraphQL types you define will be object types. Object types
582 * have a name, but most importantly describe their fields.
583 *
584 * Example:
585 *
586 * const AddressType = new GraphQLObjectType({
587 * name: 'Address',
588 * fields: {
589 * street: { type: GraphQLString },
590 * number: { type: GraphQLInt },
591 * formatted: {
592 * type: GraphQLString,
593 * resolve(obj) {
594 * return obj.number + ' ' + obj.street
595 * }
596 * }
597 * }
598 * });
599 *
600 * When two types need to refer to each other, or a type needs to refer to
601 * itself in a field, you can use a function expression (aka a closure or a
602 * thunk) to supply the fields lazily.
603 *
604 * Example:
605 *
606 * const PersonType = new GraphQLObjectType({
607 * name: 'Person',
608 * fields: () => ({
609 * name: { type: GraphQLString },
610 * bestFriend: { type: PersonType },
611 * })
612 * });
613 *
614 */
615var GraphQLObjectType = /*#__PURE__*/function () {
616 function GraphQLObjectType(config) {
617 this.name = config.name;
618 this.description = config.description;
619 this.isTypeOf = config.isTypeOf;
620 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
621 this.astNode = config.astNode;
622 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
623 this._fields = defineFieldMap.bind(undefined, config);
624 this._interfaces = defineInterfaces.bind(undefined, config);
625 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
626 config.isTypeOf == null || typeof config.isTypeOf === 'function' || (0, _devAssert.default)(0, "".concat(this.name, " must provide \"isTypeOf\" as a function, ") + "but got: ".concat((0, _inspect.default)(config.isTypeOf), "."));
627 }
628
629 var _proto2 = GraphQLObjectType.prototype;
630
631 _proto2.getFields = function getFields() {
632 if (typeof this._fields === 'function') {
633 this._fields = this._fields();
634 }
635
636 return this._fields;
637 };
638
639 _proto2.getInterfaces = function getInterfaces() {
640 if (typeof this._interfaces === 'function') {
641 this._interfaces = this._interfaces();
642 }
643
644 return this._interfaces;
645 };
646
647 _proto2.toConfig = function toConfig() {
648 return {
649 name: this.name,
650 description: this.description,
651 interfaces: this.getInterfaces(),
652 fields: fieldsToFieldsConfig(this.getFields()),
653 isTypeOf: this.isTypeOf,
654 extensions: this.extensions,
655 astNode: this.astNode,
656 extensionASTNodes: this.extensionASTNodes || []
657 };
658 };
659
660 _proto2.toString = function toString() {
661 return this.name;
662 };
663
664 _proto2.toJSON = function toJSON() {
665 return this.toString();
666 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
667 ;
668
669 _createClass(GraphQLObjectType, [{
670 key: _symbols.SYMBOL_TO_STRING_TAG,
671 get: function get() {
672 return 'GraphQLObjectType';
673 }
674 }]);
675
676 return GraphQLObjectType;
677}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
678
679
680exports.GraphQLObjectType = GraphQLObjectType;
681(0, _defineInspect.default)(GraphQLObjectType);
682
683function defineInterfaces(config) {
684 var _resolveThunk;
685
686 var interfaces = (_resolveThunk = resolveThunk(config.interfaces)) !== null && _resolveThunk !== void 0 ? _resolveThunk : [];
687 Array.isArray(interfaces) || (0, _devAssert.default)(0, "".concat(config.name, " interfaces must be an Array or a function which returns an Array."));
688 return interfaces;
689}
690
691function defineFieldMap(config) {
692 var fieldMap = resolveThunk(config.fields);
693 isPlainObj(fieldMap) || (0, _devAssert.default)(0, "".concat(config.name, " fields must be an object with field names as keys or a function which returns such an object."));
694 return (0, _mapValue.default)(fieldMap, function (fieldConfig, fieldName) {
695 var _fieldConfig$args;
696
697 isPlainObj(fieldConfig) || (0, _devAssert.default)(0, "".concat(config.name, ".").concat(fieldName, " field config must be an object."));
698 !('isDeprecated' in fieldConfig) || (0, _devAssert.default)(0, "".concat(config.name, ".").concat(fieldName, " should provide \"deprecationReason\" instead of \"isDeprecated\"."));
699 fieldConfig.resolve == null || typeof fieldConfig.resolve === 'function' || (0, _devAssert.default)(0, "".concat(config.name, ".").concat(fieldName, " field resolver must be a function if ") + "provided, but got: ".concat((0, _inspect.default)(fieldConfig.resolve), "."));
700 var argsConfig = (_fieldConfig$args = fieldConfig.args) !== null && _fieldConfig$args !== void 0 ? _fieldConfig$args : {};
701 isPlainObj(argsConfig) || (0, _devAssert.default)(0, "".concat(config.name, ".").concat(fieldName, " args must be an object with argument names as keys."));
702 var args = (0, _objectEntries.default)(argsConfig).map(function (_ref) {
703 var argName = _ref[0],
704 argConfig = _ref[1];
705 return {
706 name: argName,
707 description: argConfig.description,
708 type: argConfig.type,
709 defaultValue: argConfig.defaultValue,
710 deprecationReason: argConfig.deprecationReason,
711 extensions: argConfig.extensions && (0, _toObjMap.default)(argConfig.extensions),
712 astNode: argConfig.astNode
713 };
714 });
715 return {
716 name: fieldName,
717 description: fieldConfig.description,
718 type: fieldConfig.type,
719 args: args,
720 resolve: fieldConfig.resolve,
721 subscribe: fieldConfig.subscribe,
722 isDeprecated: fieldConfig.deprecationReason != null,
723 deprecationReason: fieldConfig.deprecationReason,
724 extensions: fieldConfig.extensions && (0, _toObjMap.default)(fieldConfig.extensions),
725 astNode: fieldConfig.astNode
726 };
727 });
728}
729
730function isPlainObj(obj) {
731 return (0, _isObjectLike.default)(obj) && !Array.isArray(obj);
732}
733
734function fieldsToFieldsConfig(fields) {
735 return (0, _mapValue.default)(fields, function (field) {
736 return {
737 description: field.description,
738 type: field.type,
739 args: argsToArgsConfig(field.args),
740 resolve: field.resolve,
741 subscribe: field.subscribe,
742 deprecationReason: field.deprecationReason,
743 extensions: field.extensions,
744 astNode: field.astNode
745 };
746 });
747}
748/**
749 * @internal
750 */
751
752
753function argsToArgsConfig(args) {
754 return (0, _keyValMap.default)(args, function (arg) {
755 return arg.name;
756 }, function (arg) {
757 return {
758 description: arg.description,
759 type: arg.type,
760 defaultValue: arg.defaultValue,
761 deprecationReason: arg.deprecationReason,
762 extensions: arg.extensions,
763 astNode: arg.astNode
764 };
765 });
766}
767
768function isRequiredArgument(arg) {
769 return isNonNullType(arg.type) && arg.defaultValue === undefined;
770}
771
772/**
773 * Interface Type Definition
774 *
775 * When a field can return one of a heterogeneous set of types, a Interface type
776 * is used to describe what types are possible, what fields are in common across
777 * all types, as well as a function to determine which type is actually used
778 * when the field is resolved.
779 *
780 * Example:
781 *
782 * const EntityType = new GraphQLInterfaceType({
783 * name: 'Entity',
784 * fields: {
785 * name: { type: GraphQLString }
786 * }
787 * });
788 *
789 */
790var GraphQLInterfaceType = /*#__PURE__*/function () {
791 function GraphQLInterfaceType(config) {
792 this.name = config.name;
793 this.description = config.description;
794 this.resolveType = config.resolveType;
795 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
796 this.astNode = config.astNode;
797 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
798 this._fields = defineFieldMap.bind(undefined, config);
799 this._interfaces = defineInterfaces.bind(undefined, config);
800 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
801 config.resolveType == null || typeof config.resolveType === 'function' || (0, _devAssert.default)(0, "".concat(this.name, " must provide \"resolveType\" as a function, ") + "but got: ".concat((0, _inspect.default)(config.resolveType), "."));
802 }
803
804 var _proto3 = GraphQLInterfaceType.prototype;
805
806 _proto3.getFields = function getFields() {
807 if (typeof this._fields === 'function') {
808 this._fields = this._fields();
809 }
810
811 return this._fields;
812 };
813
814 _proto3.getInterfaces = function getInterfaces() {
815 if (typeof this._interfaces === 'function') {
816 this._interfaces = this._interfaces();
817 }
818
819 return this._interfaces;
820 };
821
822 _proto3.toConfig = function toConfig() {
823 var _this$extensionASTNod2;
824
825 return {
826 name: this.name,
827 description: this.description,
828 interfaces: this.getInterfaces(),
829 fields: fieldsToFieldsConfig(this.getFields()),
830 resolveType: this.resolveType,
831 extensions: this.extensions,
832 astNode: this.astNode,
833 extensionASTNodes: (_this$extensionASTNod2 = this.extensionASTNodes) !== null && _this$extensionASTNod2 !== void 0 ? _this$extensionASTNod2 : []
834 };
835 };
836
837 _proto3.toString = function toString() {
838 return this.name;
839 };
840
841 _proto3.toJSON = function toJSON() {
842 return this.toString();
843 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
844 ;
845
846 _createClass(GraphQLInterfaceType, [{
847 key: _symbols.SYMBOL_TO_STRING_TAG,
848 get: function get() {
849 return 'GraphQLInterfaceType';
850 }
851 }]);
852
853 return GraphQLInterfaceType;
854}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
855
856
857exports.GraphQLInterfaceType = GraphQLInterfaceType;
858(0, _defineInspect.default)(GraphQLInterfaceType);
859
860/**
861 * Union Type Definition
862 *
863 * When a field can return one of a heterogeneous set of types, a Union type
864 * is used to describe what types are possible as well as providing a function
865 * to determine which type is actually used when the field is resolved.
866 *
867 * Example:
868 *
869 * const PetType = new GraphQLUnionType({
870 * name: 'Pet',
871 * types: [ DogType, CatType ],
872 * resolveType(value) {
873 * if (value instanceof Dog) {
874 * return DogType;
875 * }
876 * if (value instanceof Cat) {
877 * return CatType;
878 * }
879 * }
880 * });
881 *
882 */
883var GraphQLUnionType = /*#__PURE__*/function () {
884 function GraphQLUnionType(config) {
885 this.name = config.name;
886 this.description = config.description;
887 this.resolveType = config.resolveType;
888 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
889 this.astNode = config.astNode;
890 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
891 this._types = defineTypes.bind(undefined, config);
892 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
893 config.resolveType == null || typeof config.resolveType === 'function' || (0, _devAssert.default)(0, "".concat(this.name, " must provide \"resolveType\" as a function, ") + "but got: ".concat((0, _inspect.default)(config.resolveType), "."));
894 }
895
896 var _proto4 = GraphQLUnionType.prototype;
897
898 _proto4.getTypes = function getTypes() {
899 if (typeof this._types === 'function') {
900 this._types = this._types();
901 }
902
903 return this._types;
904 };
905
906 _proto4.toConfig = function toConfig() {
907 var _this$extensionASTNod3;
908
909 return {
910 name: this.name,
911 description: this.description,
912 types: this.getTypes(),
913 resolveType: this.resolveType,
914 extensions: this.extensions,
915 astNode: this.astNode,
916 extensionASTNodes: (_this$extensionASTNod3 = this.extensionASTNodes) !== null && _this$extensionASTNod3 !== void 0 ? _this$extensionASTNod3 : []
917 };
918 };
919
920 _proto4.toString = function toString() {
921 return this.name;
922 };
923
924 _proto4.toJSON = function toJSON() {
925 return this.toString();
926 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
927 ;
928
929 _createClass(GraphQLUnionType, [{
930 key: _symbols.SYMBOL_TO_STRING_TAG,
931 get: function get() {
932 return 'GraphQLUnionType';
933 }
934 }]);
935
936 return GraphQLUnionType;
937}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
938
939
940exports.GraphQLUnionType = GraphQLUnionType;
941(0, _defineInspect.default)(GraphQLUnionType);
942
943function defineTypes(config) {
944 var types = resolveThunk(config.types);
945 Array.isArray(types) || (0, _devAssert.default)(0, "Must provide Array of types or a function which returns such an array for Union ".concat(config.name, "."));
946 return types;
947}
948
949/**
950 * Enum Type Definition
951 *
952 * Some leaf values of requests and input values are Enums. GraphQL serializes
953 * Enum values as strings, however internally Enums can be represented by any
954 * kind of type, often integers.
955 *
956 * Example:
957 *
958 * const RGBType = new GraphQLEnumType({
959 * name: 'RGB',
960 * values: {
961 * RED: { value: 0 },
962 * GREEN: { value: 1 },
963 * BLUE: { value: 2 }
964 * }
965 * });
966 *
967 * Note: If a value is not provided in a definition, the name of the enum value
968 * will be used as its internal value.
969 */
970var GraphQLEnumType
971/* <T> */
972= /*#__PURE__*/function () {
973 function GraphQLEnumType(config) {
974 this.name = config.name;
975 this.description = config.description;
976 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
977 this.astNode = config.astNode;
978 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
979 this._values = defineEnumValues(this.name, config.values);
980 this._valueLookup = new Map(this._values.map(function (enumValue) {
981 return [enumValue.value, enumValue];
982 }));
983 this._nameLookup = (0, _keyMap.default)(this._values, function (value) {
984 return value.name;
985 });
986 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
987 }
988
989 var _proto5 = GraphQLEnumType.prototype;
990
991 _proto5.getValues = function getValues() {
992 return this._values;
993 };
994
995 _proto5.getValue = function getValue(name) {
996 return this._nameLookup[name];
997 };
998
999 _proto5.serialize = function serialize(outputValue) {
1000 var enumValue = this._valueLookup.get(outputValue);
1001
1002 if (enumValue === undefined) {
1003 throw new _GraphQLError.GraphQLError("Enum \"".concat(this.name, "\" cannot represent value: ").concat((0, _inspect.default)(outputValue)));
1004 }
1005
1006 return enumValue.name;
1007 };
1008
1009 _proto5.parseValue = function parseValue(inputValue)
1010 /* T */
1011 {
1012 if (typeof inputValue !== 'string') {
1013 var valueStr = (0, _inspect.default)(inputValue);
1014 throw new _GraphQLError.GraphQLError("Enum \"".concat(this.name, "\" cannot represent non-string value: ").concat(valueStr, ".") + didYouMeanEnumValue(this, valueStr));
1015 }
1016
1017 var enumValue = this.getValue(inputValue);
1018
1019 if (enumValue == null) {
1020 throw new _GraphQLError.GraphQLError("Value \"".concat(inputValue, "\" does not exist in \"").concat(this.name, "\" enum.") + didYouMeanEnumValue(this, inputValue));
1021 }
1022
1023 return enumValue.value;
1024 };
1025
1026 _proto5.parseLiteral = function parseLiteral(valueNode, _variables)
1027 /* T */
1028 {
1029 // Note: variables will be resolved to a value before calling this function.
1030 if (valueNode.kind !== _kinds.Kind.ENUM) {
1031 var valueStr = (0, _printer.print)(valueNode);
1032 throw new _GraphQLError.GraphQLError("Enum \"".concat(this.name, "\" cannot represent non-enum value: ").concat(valueStr, ".") + didYouMeanEnumValue(this, valueStr), valueNode);
1033 }
1034
1035 var enumValue = this.getValue(valueNode.value);
1036
1037 if (enumValue == null) {
1038 var _valueStr = (0, _printer.print)(valueNode);
1039
1040 throw new _GraphQLError.GraphQLError("Value \"".concat(_valueStr, "\" does not exist in \"").concat(this.name, "\" enum.") + didYouMeanEnumValue(this, _valueStr), valueNode);
1041 }
1042
1043 return enumValue.value;
1044 };
1045
1046 _proto5.toConfig = function toConfig() {
1047 var _this$extensionASTNod4;
1048
1049 var values = (0, _keyValMap.default)(this.getValues(), function (value) {
1050 return value.name;
1051 }, function (value) {
1052 return {
1053 description: value.description,
1054 value: value.value,
1055 deprecationReason: value.deprecationReason,
1056 extensions: value.extensions,
1057 astNode: value.astNode
1058 };
1059 });
1060 return {
1061 name: this.name,
1062 description: this.description,
1063 values: values,
1064 extensions: this.extensions,
1065 astNode: this.astNode,
1066 extensionASTNodes: (_this$extensionASTNod4 = this.extensionASTNodes) !== null && _this$extensionASTNod4 !== void 0 ? _this$extensionASTNod4 : []
1067 };
1068 };
1069
1070 _proto5.toString = function toString() {
1071 return this.name;
1072 };
1073
1074 _proto5.toJSON = function toJSON() {
1075 return this.toString();
1076 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
1077 ;
1078
1079 _createClass(GraphQLEnumType, [{
1080 key: _symbols.SYMBOL_TO_STRING_TAG,
1081 get: function get() {
1082 return 'GraphQLEnumType';
1083 }
1084 }]);
1085
1086 return GraphQLEnumType;
1087}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
1088
1089
1090exports.GraphQLEnumType = GraphQLEnumType;
1091(0, _defineInspect.default)(GraphQLEnumType);
1092
1093function didYouMeanEnumValue(enumType, unknownValueStr) {
1094 var allNames = enumType.getValues().map(function (value) {
1095 return value.name;
1096 });
1097 var suggestedValues = (0, _suggestionList.default)(unknownValueStr, allNames);
1098 return (0, _didYouMean.default)('the enum value', suggestedValues);
1099}
1100
1101function defineEnumValues(typeName, valueMap) {
1102 isPlainObj(valueMap) || (0, _devAssert.default)(0, "".concat(typeName, " values must be an object with value names as keys."));
1103 return (0, _objectEntries.default)(valueMap).map(function (_ref2) {
1104 var valueName = _ref2[0],
1105 valueConfig = _ref2[1];
1106 isPlainObj(valueConfig) || (0, _devAssert.default)(0, "".concat(typeName, ".").concat(valueName, " must refer to an object with a \"value\" key ") + "representing an internal value but got: ".concat((0, _inspect.default)(valueConfig), "."));
1107 !('isDeprecated' in valueConfig) || (0, _devAssert.default)(0, "".concat(typeName, ".").concat(valueName, " should provide \"deprecationReason\" instead of \"isDeprecated\"."));
1108 return {
1109 name: valueName,
1110 description: valueConfig.description,
1111 value: valueConfig.value !== undefined ? valueConfig.value : valueName,
1112 isDeprecated: valueConfig.deprecationReason != null,
1113 deprecationReason: valueConfig.deprecationReason,
1114 extensions: valueConfig.extensions && (0, _toObjMap.default)(valueConfig.extensions),
1115 astNode: valueConfig.astNode
1116 };
1117 });
1118}
1119
1120/**
1121 * Input Object Type Definition
1122 *
1123 * An input object defines a structured collection of fields which may be
1124 * supplied to a field argument.
1125 *
1126 * Using `NonNull` will ensure that a value must be provided by the query
1127 *
1128 * Example:
1129 *
1130 * const GeoPoint = new GraphQLInputObjectType({
1131 * name: 'GeoPoint',
1132 * fields: {
1133 * lat: { type: new GraphQLNonNull(GraphQLFloat) },
1134 * lon: { type: new GraphQLNonNull(GraphQLFloat) },
1135 * alt: { type: GraphQLFloat, defaultValue: 0 },
1136 * }
1137 * });
1138 *
1139 */
1140var GraphQLInputObjectType = /*#__PURE__*/function () {
1141 function GraphQLInputObjectType(config) {
1142 this.name = config.name;
1143 this.description = config.description;
1144 this.extensions = config.extensions && (0, _toObjMap.default)(config.extensions);
1145 this.astNode = config.astNode;
1146 this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
1147 this._fields = defineInputFieldMap.bind(undefined, config);
1148 typeof config.name === 'string' || (0, _devAssert.default)(0, 'Must provide name.');
1149 }
1150
1151 var _proto6 = GraphQLInputObjectType.prototype;
1152
1153 _proto6.getFields = function getFields() {
1154 if (typeof this._fields === 'function') {
1155 this._fields = this._fields();
1156 }
1157
1158 return this._fields;
1159 };
1160
1161 _proto6.toConfig = function toConfig() {
1162 var _this$extensionASTNod5;
1163
1164 var fields = (0, _mapValue.default)(this.getFields(), function (field) {
1165 return {
1166 description: field.description,
1167 type: field.type,
1168 defaultValue: field.defaultValue,
1169 extensions: field.extensions,
1170 astNode: field.astNode
1171 };
1172 });
1173 return {
1174 name: this.name,
1175 description: this.description,
1176 fields: fields,
1177 extensions: this.extensions,
1178 astNode: this.astNode,
1179 extensionASTNodes: (_this$extensionASTNod5 = this.extensionASTNodes) !== null && _this$extensionASTNod5 !== void 0 ? _this$extensionASTNod5 : []
1180 };
1181 };
1182
1183 _proto6.toString = function toString() {
1184 return this.name;
1185 };
1186
1187 _proto6.toJSON = function toJSON() {
1188 return this.toString();
1189 } // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
1190 ;
1191
1192 _createClass(GraphQLInputObjectType, [{
1193 key: _symbols.SYMBOL_TO_STRING_TAG,
1194 get: function get() {
1195 return 'GraphQLInputObjectType';
1196 }
1197 }]);
1198
1199 return GraphQLInputObjectType;
1200}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
1201
1202
1203exports.GraphQLInputObjectType = GraphQLInputObjectType;
1204(0, _defineInspect.default)(GraphQLInputObjectType);
1205
1206function defineInputFieldMap(config) {
1207 var fieldMap = resolveThunk(config.fields);
1208 isPlainObj(fieldMap) || (0, _devAssert.default)(0, "".concat(config.name, " fields must be an object with field names as keys or a function which returns such an object."));
1209 return (0, _mapValue.default)(fieldMap, function (fieldConfig, fieldName) {
1210 !('resolve' in fieldConfig) || (0, _devAssert.default)(0, "".concat(config.name, ".").concat(fieldName, " field has a resolve property, but Input Types cannot define resolvers."));
1211 return {
1212 name: fieldName,
1213 description: fieldConfig.description,
1214 type: fieldConfig.type,
1215 defaultValue: fieldConfig.defaultValue,
1216 deprecationReason: fieldConfig.deprecationReason,
1217 extensions: fieldConfig.extensions && (0, _toObjMap.default)(fieldConfig.extensions),
1218 astNode: fieldConfig.astNode
1219 };
1220 });
1221}
1222
1223function isRequiredInputField(field) {
1224 return isNonNullType(field.type) && field.defaultValue === undefined;
1225}