UNPKG

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