UNPKG

28.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.GraphQLInputObjectType = exports.GraphQLEnumType = exports.GraphQLUnionType = exports.GraphQLInterfaceType = exports.GraphQLObjectType = exports.GraphQLScalarType = undefined;
7
8var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
10var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
11
12exports.isType = isType;
13exports.assertType = assertType;
14exports.isScalarType = isScalarType;
15exports.assertScalarType = assertScalarType;
16exports.isObjectType = isObjectType;
17exports.assertObjectType = assertObjectType;
18exports.isInterfaceType = isInterfaceType;
19exports.assertInterfaceType = assertInterfaceType;
20exports.isUnionType = isUnionType;
21exports.assertUnionType = assertUnionType;
22exports.isEnumType = isEnumType;
23exports.assertEnumType = assertEnumType;
24exports.isInputObjectType = isInputObjectType;
25exports.assertInputObjectType = assertInputObjectType;
26exports.isListType = isListType;
27exports.assertListType = assertListType;
28exports.isNonNullType = isNonNullType;
29exports.assertNonNullType = assertNonNullType;
30exports.isInputType = isInputType;
31exports.assertInputType = assertInputType;
32exports.isOutputType = isOutputType;
33exports.assertOutputType = assertOutputType;
34exports.isLeafType = isLeafType;
35exports.assertLeafType = assertLeafType;
36exports.isCompositeType = isCompositeType;
37exports.assertCompositeType = assertCompositeType;
38exports.isAbstractType = isAbstractType;
39exports.assertAbstractType = assertAbstractType;
40exports.isWrappingType = isWrappingType;
41exports.assertWrappingType = assertWrappingType;
42exports.isNullableType = isNullableType;
43exports.assertNullableType = assertNullableType;
44exports.getNullableType = getNullableType;
45exports.isNamedType = isNamedType;
46exports.assertNamedType = assertNamedType;
47exports.getNamedType = getNamedType;
48
49var _instanceOf = require('../jsutils/instanceOf');
50
51var _instanceOf2 = _interopRequireDefault(_instanceOf);
52
53var _invariant = require('../jsutils/invariant');
54
55var _invariant2 = _interopRequireDefault(_invariant);
56
57var _isInvalid = require('../jsutils/isInvalid');
58
59var _isInvalid2 = _interopRequireDefault(_isInvalid);
60
61var _kinds = require('../language/kinds');
62
63var Kind = _interopRequireWildcard(_kinds);
64
65var _valueFromASTUntyped = require('../utilities/valueFromASTUntyped');
66
67var _wrappers = require('./wrappers');
68
69function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
70
71function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
72
73function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /**
74 * Copyright (c) 2015-present, Facebook, Inc.
75 *
76 * This source code is licensed under the MIT license found in the
77 * LICENSE file in the root directory of this source tree.
78 *
79 *
80 */
81
82// Predicates & Assertions
83
84/**
85 * These are all of the possible kinds of types.
86 */
87function isType(type) {
88 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type) || isListType(type) || isNonNullType(type);
89}
90
91function assertType(type) {
92 !isType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL type.') : void 0;
93 return type;
94}
95
96/**
97 * There are predicates for each kind of GraphQL type.
98 */
99
100// eslint-disable-next-line no-redeclare
101function isScalarType(type) {
102 return (0, _instanceOf2.default)(type, GraphQLScalarType);
103}
104
105function assertScalarType(type) {
106 !isScalarType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Scalar type.') : void 0;
107 return type;
108}
109
110// eslint-disable-next-line no-redeclare
111function isObjectType(type) {
112 return (0, _instanceOf2.default)(type, GraphQLObjectType);
113}
114
115function assertObjectType(type) {
116 !isObjectType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Object type.') : void 0;
117 return type;
118}
119
120// eslint-disable-next-line no-redeclare
121function isInterfaceType(type) {
122 return (0, _instanceOf2.default)(type, GraphQLInterfaceType);
123}
124
125function assertInterfaceType(type) {
126 !isInterfaceType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Interface type.') : void 0;
127 return type;
128}
129
130// eslint-disable-next-line no-redeclare
131function isUnionType(type) {
132 return (0, _instanceOf2.default)(type, GraphQLUnionType);
133}
134
135function assertUnionType(type) {
136 !isUnionType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Union type.') : void 0;
137 return type;
138}
139
140// eslint-disable-next-line no-redeclare
141function isEnumType(type) {
142 return (0, _instanceOf2.default)(type, GraphQLEnumType);
143}
144
145function assertEnumType(type) {
146 !isEnumType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Enum type.') : void 0;
147 return type;
148}
149
150// eslint-disable-next-line no-redeclare
151function isInputObjectType(type) {
152 return (0, _instanceOf2.default)(type, GraphQLInputObjectType);
153}
154
155function assertInputObjectType(type) {
156 !isInputObjectType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Input Object type.') : void 0;
157 return type;
158}
159
160// eslint-disable-next-line no-redeclare
161function isListType(type) {
162 return (0, _instanceOf2.default)(type, _wrappers.GraphQLList);
163}
164
165function assertListType(type) {
166 !isListType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL List type.') : void 0;
167 return type;
168}
169
170// eslint-disable-next-line no-redeclare
171function isNonNullType(type) {
172 return (0, _instanceOf2.default)(type, _wrappers.GraphQLNonNull);
173}
174
175function assertNonNullType(type) {
176 !isNonNullType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL Non-Null type.') : void 0;
177 return type;
178}
179
180/**
181 * These types may be used as input types for arguments and directives.
182 */
183function isInputType(type) {
184 return isScalarType(type) || isEnumType(type) || isInputObjectType(type) || isWrappingType(type) && isInputType(type.ofType);
185}
186
187function assertInputType(type) {
188 !isInputType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL input type.') : void 0;
189 return type;
190}
191
192/**
193 * These types may be used as output types as the result of fields.
194 */
195function isOutputType(type) {
196 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isWrappingType(type) && isOutputType(type.ofType);
197}
198
199function assertOutputType(type) {
200 !isOutputType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL output type.') : void 0;
201 return type;
202}
203
204/**
205 * These types may describe types which may be leaf values.
206 */
207function isLeafType(type) {
208 return isScalarType(type) || isEnumType(type);
209}
210
211function assertLeafType(type) {
212 !isLeafType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL leaf type.') : void 0;
213 return type;
214}
215
216/**
217 * These types may describe the parent context of a selection set.
218 */
219function isCompositeType(type) {
220 return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
221}
222
223function assertCompositeType(type) {
224 !isCompositeType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL composite type.') : void 0;
225 return type;
226}
227
228/**
229 * These types may describe the parent context of a selection set.
230 */
231function isAbstractType(type) {
232 return isInterfaceType(type) || isUnionType(type);
233}
234
235function assertAbstractType(type) {
236 !isAbstractType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL abstract type.') : void 0;
237 return type;
238}
239
240/**
241 * These types wrap and modify other types
242 */
243
244function isWrappingType(type) {
245 return isListType(type) || isNonNullType(type);
246}
247
248function assertWrappingType(type) {
249 !isWrappingType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL wrapping type.') : void 0;
250 return type;
251}
252
253/**
254 * These types can all accept null as a value.
255 */
256function isNullableType(type) {
257 return isType(type) && !isNonNullType(type);
258}
259
260function assertNullableType(type) {
261 !isNullableType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL nullable type.') : void 0;
262 return type;
263}
264
265/* eslint-disable no-redeclare */
266function getNullableType(type) {
267 /* eslint-enable no-redeclare */
268 if (type) {
269 return isNonNullType(type) ? type.ofType : type;
270 }
271}
272
273/**
274 * These named types do not include modifiers like List or NonNull.
275 */
276function isNamedType(type) {
277 return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type);
278}
279
280function assertNamedType(type) {
281 !isNamedType(type) ? (0, _invariant2.default)(0, 'Expected ' + String(type) + ' to be a GraphQL named type.') : void 0;
282 return type;
283}
284
285/* eslint-disable no-redeclare */
286function getNamedType(type) {
287 /* eslint-enable no-redeclare */
288 if (type) {
289 var unwrappedType = type;
290 while (isWrappingType(unwrappedType)) {
291 unwrappedType = unwrappedType.ofType;
292 }
293 return unwrappedType;
294 }
295}
296
297/**
298 * Used while defining GraphQL types to allow for circular references in
299 * otherwise immutable type definitions.
300 */
301
302
303function resolveThunk(thunk) {
304 return typeof thunk === 'function' ? thunk() : thunk;
305}
306
307/**
308 * Scalar Type Definition
309 *
310 * The leaf values of any request and input values to arguments are
311 * Scalars (or Enums) and are defined with a name and a series of functions
312 * used to parse input from ast or variables and to ensure validity.
313 *
314 * If a type's serialize function does not return a value (i.e. it returns
315 * `undefined`) then an error will be raised and a `null` value will be returned
316 * in the response. If the serialize function returns `null`, then no error will
317 * be included in the response.
318 *
319 * Example:
320 *
321 * const OddType = new GraphQLScalarType({
322 * name: 'Odd',
323 * serialize(value) {
324 * if (value % 2 === 1) {
325 * return value;
326 * }
327 * }
328 * });
329 *
330 */
331
332var GraphQLScalarType = exports.GraphQLScalarType = function () {
333 function GraphQLScalarType(config) {
334 _classCallCheck(this, GraphQLScalarType);
335
336 this.name = config.name;
337 this.description = config.description;
338 this.astNode = config.astNode;
339 this._scalarConfig = config;
340 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
341 !(typeof config.serialize === 'function') ? (0, _invariant2.default)(0, 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.') : void 0;
342 if (config.parseValue || config.parseLiteral) {
343 !(typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function') ? (0, _invariant2.default)(0, this.name + ' must provide both "parseValue" and "parseLiteral" ' + 'functions.') : void 0;
344 }
345 }
346
347 // Serializes an internal value to include in a response.
348
349
350 GraphQLScalarType.prototype.serialize = function serialize(value) {
351 var serializer = this._scalarConfig.serialize;
352 return serializer(value);
353 };
354
355 // Parses an externally provided value to use as an input.
356
357
358 GraphQLScalarType.prototype.parseValue = function parseValue(value) {
359 var parser = this._scalarConfig.parseValue;
360 if ((0, _isInvalid2.default)(value)) {
361 return undefined;
362 }
363 return parser ? parser(value) : value;
364 };
365
366 // Parses an externally provided literal value to use as an input.
367
368
369 GraphQLScalarType.prototype.parseLiteral = function parseLiteral(valueNode, variables) {
370 var parser = this._scalarConfig.parseLiteral;
371 return parser ? parser(valueNode, variables) : (0, _valueFromASTUntyped.valueFromASTUntyped)(valueNode, variables);
372 };
373
374 GraphQLScalarType.prototype.toString = function toString() {
375 return this.name;
376 };
377
378 return GraphQLScalarType;
379}();
380
381// Also provide toJSON and inspect aliases for toString.
382
383
384GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect = GraphQLScalarType.prototype.toString;
385
386/**
387 * Object Type Definition
388 *
389 * Almost all of the GraphQL types you define will be object types. Object types
390 * have a name, but most importantly describe their fields.
391 *
392 * Example:
393 *
394 * const AddressType = new GraphQLObjectType({
395 * name: 'Address',
396 * fields: {
397 * street: { type: GraphQLString },
398 * number: { type: GraphQLInt },
399 * formatted: {
400 * type: GraphQLString,
401 * resolve(obj) {
402 * return obj.number + ' ' + obj.street
403 * }
404 * }
405 * }
406 * });
407 *
408 * When two types need to refer to each other, or a type needs to refer to
409 * itself in a field, you can use a function expression (aka a closure or a
410 * thunk) to supply the fields lazily.
411 *
412 * Example:
413 *
414 * const PersonType = new GraphQLObjectType({
415 * name: 'Person',
416 * fields: () => ({
417 * name: { type: GraphQLString },
418 * bestFriend: { type: PersonType },
419 * })
420 * });
421 *
422 */
423var GraphQLObjectType = exports.GraphQLObjectType = function () {
424 function GraphQLObjectType(config) {
425 _classCallCheck(this, GraphQLObjectType);
426
427 this.name = config.name;
428 this.description = config.description;
429 this.astNode = config.astNode;
430 this.extensionASTNodes = config.extensionASTNodes;
431 this.isTypeOf = config.isTypeOf;
432 this._typeConfig = config;
433 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
434 if (config.isTypeOf) {
435 !(typeof config.isTypeOf === 'function') ? (0, _invariant2.default)(0, this.name + ' must provide "isTypeOf" as a function.') : void 0;
436 }
437 }
438
439 GraphQLObjectType.prototype.getFields = function getFields() {
440 return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields));
441 };
442
443 GraphQLObjectType.prototype.getInterfaces = function getInterfaces() {
444 return this._interfaces || (this._interfaces = defineInterfaces(this, this._typeConfig.interfaces));
445 };
446
447 GraphQLObjectType.prototype.toString = function toString() {
448 return this.name;
449 };
450
451 return GraphQLObjectType;
452}();
453
454// Also provide toJSON and inspect aliases for toString.
455
456
457GraphQLObjectType.prototype.toJSON = GraphQLObjectType.prototype.inspect = GraphQLObjectType.prototype.toString;
458
459function defineInterfaces(type, interfacesThunk) {
460 var interfaces = resolveThunk(interfacesThunk) || [];
461 !Array.isArray(interfaces) ? (0, _invariant2.default)(0, type.name + ' interfaces must be an Array or a function which returns ' + 'an Array.') : void 0;
462 return interfaces;
463}
464
465function defineFieldMap(type, fieldsThunk) {
466 var fieldMap = resolveThunk(fieldsThunk) || {};
467 !isPlainObj(fieldMap) ? (0, _invariant2.default)(0, type.name + ' fields must be an object with field names as keys or a ' + 'function which returns such an object.') : void 0;
468
469 var resultFieldMap = Object.create(null);
470 Object.keys(fieldMap).forEach(function (fieldName) {
471 var fieldConfig = fieldMap[fieldName];
472 !isPlainObj(fieldConfig) ? (0, _invariant2.default)(0, type.name + '.' + fieldName + ' field config must be an object') : void 0;
473 !!fieldConfig.hasOwnProperty('isDeprecated') ? (0, _invariant2.default)(0, type.name + '.' + fieldName + ' should provide "deprecationReason" instead ' + 'of "isDeprecated".') : void 0;
474 var field = _extends({}, fieldConfig, {
475 isDeprecated: Boolean(fieldConfig.deprecationReason),
476 name: fieldName
477 });
478 !isValidResolver(field.resolve) ? (0, _invariant2.default)(0, type.name + '.' + fieldName + ' field resolver must be a function if ' + ('provided, but got: ' + String(field.resolve) + '.')) : void 0;
479 var argsConfig = fieldConfig.args;
480 if (!argsConfig) {
481 field.args = [];
482 } else {
483 !isPlainObj(argsConfig) ? (0, _invariant2.default)(0, type.name + '.' + fieldName + ' args must be an object with argument ' + 'names as keys.') : void 0;
484 field.args = Object.keys(argsConfig).map(function (argName) {
485 var arg = argsConfig[argName];
486 return {
487 name: argName,
488 description: arg.description === undefined ? null : arg.description,
489 type: arg.type,
490 defaultValue: arg.defaultValue,
491 astNode: arg.astNode
492 };
493 });
494 }
495 resultFieldMap[fieldName] = field;
496 });
497 return resultFieldMap;
498}
499
500function isPlainObj(obj) {
501 return obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && !Array.isArray(obj);
502}
503
504// If a resolver is defined, it must be a function.
505function isValidResolver(resolver) {
506 return resolver == null || typeof resolver === 'function';
507}
508
509/**
510 * Interface Type Definition
511 *
512 * When a field can return one of a heterogeneous set of types, a Interface type
513 * is used to describe what types are possible, what fields are in common across
514 * all types, as well as a function to determine which type is actually used
515 * when the field is resolved.
516 *
517 * Example:
518 *
519 * const EntityType = new GraphQLInterfaceType({
520 * name: 'Entity',
521 * fields: {
522 * name: { type: GraphQLString }
523 * }
524 * });
525 *
526 */
527var GraphQLInterfaceType = exports.GraphQLInterfaceType = function () {
528 function GraphQLInterfaceType(config) {
529 _classCallCheck(this, GraphQLInterfaceType);
530
531 this.name = config.name;
532 this.description = config.description;
533 this.astNode = config.astNode;
534 this.extensionASTNodes = config.extensionASTNodes;
535 this.resolveType = config.resolveType;
536 this._typeConfig = config;
537 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
538 if (config.resolveType) {
539 !(typeof config.resolveType === 'function') ? (0, _invariant2.default)(0, this.name + ' must provide "resolveType" as a function.') : void 0;
540 }
541 }
542
543 GraphQLInterfaceType.prototype.getFields = function getFields() {
544 return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields));
545 };
546
547 GraphQLInterfaceType.prototype.toString = function toString() {
548 return this.name;
549 };
550
551 return GraphQLInterfaceType;
552}();
553
554// Also provide toJSON and inspect aliases for toString.
555
556
557GraphQLInterfaceType.prototype.toJSON = GraphQLInterfaceType.prototype.inspect = GraphQLInterfaceType.prototype.toString;
558
559/**
560 * Union Type Definition
561 *
562 * When a field can return one of a heterogeneous set of types, a Union type
563 * is used to describe what types are possible as well as providing a function
564 * to determine which type is actually used when the field is resolved.
565 *
566 * Example:
567 *
568 * const PetType = new GraphQLUnionType({
569 * name: 'Pet',
570 * types: [ DogType, CatType ],
571 * resolveType(value) {
572 * if (value instanceof Dog) {
573 * return DogType;
574 * }
575 * if (value instanceof Cat) {
576 * return CatType;
577 * }
578 * }
579 * });
580 *
581 */
582var GraphQLUnionType = exports.GraphQLUnionType = function () {
583 function GraphQLUnionType(config) {
584 _classCallCheck(this, GraphQLUnionType);
585
586 this.name = config.name;
587 this.description = config.description;
588 this.astNode = config.astNode;
589 this.resolveType = config.resolveType;
590 this._typeConfig = config;
591 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
592 if (config.resolveType) {
593 !(typeof config.resolveType === 'function') ? (0, _invariant2.default)(0, this.name + ' must provide "resolveType" as a function.') : void 0;
594 }
595 }
596
597 GraphQLUnionType.prototype.getTypes = function getTypes() {
598 return this._types || (this._types = defineTypes(this, this._typeConfig.types));
599 };
600
601 GraphQLUnionType.prototype.toString = function toString() {
602 return this.name;
603 };
604
605 return GraphQLUnionType;
606}();
607
608// Also provide toJSON and inspect aliases for toString.
609
610
611GraphQLUnionType.prototype.toJSON = GraphQLUnionType.prototype.inspect = GraphQLUnionType.prototype.toString;
612
613function defineTypes(unionType, typesThunk) {
614 var types = resolveThunk(typesThunk) || [];
615 !Array.isArray(types) ? (0, _invariant2.default)(0, 'Must provide Array of types or a function which returns ' + ('such an array for Union ' + unionType.name + '.')) : void 0;
616 return types;
617}
618
619/**
620 * Enum Type Definition
621 *
622 * Some leaf values of requests and input values are Enums. GraphQL serializes
623 * Enum values as strings, however internally Enums can be represented by any
624 * kind of type, often integers.
625 *
626 * Example:
627 *
628 * const RGBType = new GraphQLEnumType({
629 * name: 'RGB',
630 * values: {
631 * RED: { value: 0 },
632 * GREEN: { value: 1 },
633 * BLUE: { value: 2 }
634 * }
635 * });
636 *
637 * Note: If a value is not provided in a definition, the name of the enum value
638 * will be used as its internal value.
639 */
640var GraphQLEnumType /* <T> */ = exports.GraphQLEnumType = function () {
641 function GraphQLEnumType(config /* <T> */) {
642 _classCallCheck(this, GraphQLEnumType);
643
644 this.name = config.name;
645 this.description = config.description;
646 this.astNode = config.astNode;
647 this._enumConfig = config;
648 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
649 }
650
651 GraphQLEnumType.prototype.getValues = function getValues() {
652 return this._values || (this._values = defineEnumValues(this, this._enumConfig.values));
653 };
654
655 GraphQLEnumType.prototype.getValue = function getValue(name) {
656 return this._getNameLookup()[name];
657 };
658
659 GraphQLEnumType.prototype.serialize = function serialize(value /* T */) {
660 var enumValue = this._getValueLookup().get(value);
661 if (enumValue) {
662 return enumValue.name;
663 }
664 };
665
666 GraphQLEnumType.prototype.parseValue = function parseValue(value) /* T */{
667 if (typeof value === 'string') {
668 var enumValue = this._getNameLookup()[value];
669 if (enumValue) {
670 return enumValue.value;
671 }
672 }
673 };
674
675 GraphQLEnumType.prototype.parseLiteral = function parseLiteral(valueNode, _variables) /* T */{
676 // Note: variables will be resolved to a value before calling this function.
677 if (valueNode.kind === Kind.ENUM) {
678 var enumValue = this._getNameLookup()[valueNode.value];
679 if (enumValue) {
680 return enumValue.value;
681 }
682 }
683 };
684
685 GraphQLEnumType.prototype._getValueLookup = function _getValueLookup() {
686 if (!this._valueLookup) {
687 var lookup = new Map();
688 this.getValues().forEach(function (value) {
689 lookup.set(value.value, value);
690 });
691 this._valueLookup = lookup;
692 }
693 return this._valueLookup;
694 };
695
696 GraphQLEnumType.prototype._getNameLookup = function _getNameLookup() {
697 if (!this._nameLookup) {
698 var lookup = Object.create(null);
699 this.getValues().forEach(function (value) {
700 lookup[value.name] = value;
701 });
702 this._nameLookup = lookup;
703 }
704 return this._nameLookup;
705 };
706
707 GraphQLEnumType.prototype.toString = function toString() {
708 return this.name;
709 };
710
711 return GraphQLEnumType;
712}();
713
714// Also provide toJSON and inspect aliases for toString.
715
716
717GraphQLEnumType.prototype.toJSON = GraphQLEnumType.prototype.inspect = GraphQLEnumType.prototype.toString;
718
719function defineEnumValues(type, valueMap /* <T> */
720) {
721 !isPlainObj(valueMap) ? (0, _invariant2.default)(0, type.name + ' values must be an object with value names as keys.') : void 0;
722 return Object.keys(valueMap).map(function (valueName) {
723 var value = valueMap[valueName];
724 !isPlainObj(value) ? (0, _invariant2.default)(0, type.name + '.' + valueName + ' must refer to an object with a "value" key ' + ('representing an internal value but got: ' + String(value) + '.')) : void 0;
725 !!value.hasOwnProperty('isDeprecated') ? (0, _invariant2.default)(0, type.name + '.' + valueName + ' should provide "deprecationReason" instead ' + 'of "isDeprecated".') : void 0;
726 return {
727 name: valueName,
728 description: value.description,
729 isDeprecated: Boolean(value.deprecationReason),
730 deprecationReason: value.deprecationReason,
731 astNode: value.astNode,
732 value: value.hasOwnProperty('value') ? value.value : valueName
733 };
734 });
735} /* <T> */
736
737/**
738 * Input Object Type Definition
739 *
740 * An input object defines a structured collection of fields which may be
741 * supplied to a field argument.
742 *
743 * Using `NonNull` will ensure that a value must be provided by the query
744 *
745 * Example:
746 *
747 * const GeoPoint = new GraphQLInputObjectType({
748 * name: 'GeoPoint',
749 * fields: {
750 * lat: { type: GraphQLNonNull(GraphQLFloat) },
751 * lon: { type: GraphQLNonNull(GraphQLFloat) },
752 * alt: { type: GraphQLFloat, defaultValue: 0 },
753 * }
754 * });
755 *
756 */
757var GraphQLInputObjectType = exports.GraphQLInputObjectType = function () {
758 function GraphQLInputObjectType(config) {
759 _classCallCheck(this, GraphQLInputObjectType);
760
761 this.name = config.name;
762 this.description = config.description;
763 this.astNode = config.astNode;
764 this._typeConfig = config;
765 !(typeof config.name === 'string') ? (0, _invariant2.default)(0, 'Must provide name.') : void 0;
766 }
767
768 GraphQLInputObjectType.prototype.getFields = function getFields() {
769 return this._fields || (this._fields = this._defineFieldMap());
770 };
771
772 GraphQLInputObjectType.prototype._defineFieldMap = function _defineFieldMap() {
773 var _this = this;
774
775 var fieldMap = resolveThunk(this._typeConfig.fields) || {};
776 !isPlainObj(fieldMap) ? (0, _invariant2.default)(0, this.name + ' fields must be an object with field names as keys or a ' + 'function which returns such an object.') : void 0;
777 var resultFieldMap = Object.create(null);
778 Object.keys(fieldMap).forEach(function (fieldName) {
779 var field = _extends({}, fieldMap[fieldName], {
780 name: fieldName
781 });
782 !!field.hasOwnProperty('resolve') ? (0, _invariant2.default)(0, _this.name + '.' + fieldName + ' field type has a resolve property, but ' + 'Input Types cannot define resolvers.') : void 0;
783 resultFieldMap[fieldName] = field;
784 });
785 return resultFieldMap;
786 };
787
788 GraphQLInputObjectType.prototype.toString = function toString() {
789 return this.name;
790 };
791
792 return GraphQLInputObjectType;
793}();
794
795// Also provide toJSON and inspect aliases for toString.
796
797
798GraphQLInputObjectType.prototype.toJSON = GraphQLInputObjectType.prototype.toString;
799GraphQLInputObjectType.prototype.inspect = GraphQLInputObjectType.prototype.toString;
\No newline at end of file