graphql 16.14.0 → 16.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/error/GraphQLError.d.ts +151 -15
- package/error/GraphQLError.js +143 -12
- package/error/GraphQLError.mjs +143 -12
- package/error/index.d.ts +6 -0
- package/error/index.mjs +6 -0
- package/error/locatedError.d.ts +20 -0
- package/error/locatedError.js +21 -0
- package/error/locatedError.mjs +20 -0
- package/error/syntaxError.d.ts +15 -0
- package/error/syntaxError.js +16 -0
- package/error/syntaxError.mjs +15 -0
- package/execution/collectFields.js +6 -0
- package/execution/collectFields.mjs +6 -0
- package/execution/execute.d.ts +211 -12
- package/execution/execute.js +214 -23
- package/execution/execute.mjs +213 -23
- package/execution/index.d.ts +6 -0
- package/execution/index.mjs +6 -0
- package/execution/mapAsyncIterator.d.ts +2 -0
- package/execution/mapAsyncIterator.js +2 -0
- package/execution/mapAsyncIterator.mjs +2 -0
- package/execution/subscribe.d.ts +195 -5
- package/execution/subscribe.js +154 -11
- package/execution/subscribe.mjs +153 -11
- package/execution/values.d.ts +167 -10
- package/execution/values.js +149 -5
- package/execution/values.mjs +148 -5
- package/graphql.d.ts +163 -41
- package/graphql.js +126 -4
- package/graphql.mjs +131 -41
- package/index.d.ts +11 -17
- package/index.mjs +13 -19
- package/jsutils/Maybe.d.ts +5 -1
- package/jsutils/ObjMap.d.ts +4 -0
- package/jsutils/Path.d.ts +30 -0
- package/jsutils/Path.js +29 -0
- package/jsutils/Path.mjs +29 -0
- package/jsutils/PromiseOrValue.d.ts +1 -0
- package/jsutils/devAssert.d.ts +1 -0
- package/jsutils/devAssert.js +1 -0
- package/jsutils/devAssert.mjs +1 -0
- package/jsutils/didYouMean.d.ts +4 -1
- package/jsutils/didYouMean.js +4 -1
- package/jsutils/didYouMean.mjs +4 -1
- package/jsutils/groupBy.d.ts +2 -0
- package/jsutils/groupBy.js +2 -0
- package/jsutils/groupBy.mjs +2 -0
- package/jsutils/identityFunc.d.ts +2 -0
- package/jsutils/identityFunc.js +2 -0
- package/jsutils/identityFunc.mjs +2 -0
- package/jsutils/inspect.d.ts +2 -0
- package/jsutils/inspect.js +2 -0
- package/jsutils/inspect.mjs +2 -0
- package/jsutils/instanceOf.d.ts +2 -0
- package/jsutils/instanceOf.js +2 -0
- package/jsutils/instanceOf.mjs +2 -0
- package/jsutils/invariant.d.ts +1 -0
- package/jsutils/invariant.js +1 -0
- package/jsutils/invariant.mjs +1 -0
- package/jsutils/isAsyncIterable.d.ts +2 -0
- package/jsutils/isAsyncIterable.js +2 -0
- package/jsutils/isAsyncIterable.mjs +2 -0
- package/jsutils/isIterableObject.d.ts +6 -5
- package/jsutils/isIterableObject.js +6 -5
- package/jsutils/isIterableObject.mjs +6 -5
- package/jsutils/isObjectLike.d.ts +2 -0
- package/jsutils/isObjectLike.js +2 -0
- package/jsutils/isObjectLike.mjs +2 -0
- package/jsutils/isPromise.d.ts +2 -0
- package/jsutils/isPromise.js +2 -0
- package/jsutils/isPromise.mjs +2 -0
- package/jsutils/keyMap.d.ts +7 -14
- package/jsutils/keyMap.js +7 -14
- package/jsutils/keyMap.mjs +7 -14
- package/jsutils/keyValMap.d.ts +9 -6
- package/jsutils/keyValMap.js +9 -6
- package/jsutils/keyValMap.mjs +9 -6
- package/jsutils/mapValue.d.ts +2 -0
- package/jsutils/mapValue.js +2 -0
- package/jsutils/mapValue.mjs +2 -0
- package/jsutils/memoize3.d.ts +2 -0
- package/jsutils/memoize3.js +2 -0
- package/jsutils/memoize3.mjs +2 -0
- package/jsutils/naturalCompare.d.ts +2 -0
- package/jsutils/naturalCompare.js +2 -0
- package/jsutils/naturalCompare.mjs +2 -0
- package/jsutils/printPathArray.d.ts +2 -0
- package/jsutils/printPathArray.js +2 -0
- package/jsutils/printPathArray.mjs +2 -0
- package/jsutils/promiseForObject.d.ts +2 -0
- package/jsutils/promiseForObject.js +2 -0
- package/jsutils/promiseForObject.mjs +2 -0
- package/jsutils/promiseReduce.d.ts +2 -0
- package/jsutils/promiseReduce.js +2 -0
- package/jsutils/promiseReduce.mjs +2 -0
- package/jsutils/suggestionList.d.ts +2 -0
- package/jsutils/suggestionList.js +4 -0
- package/jsutils/suggestionList.mjs +4 -0
- package/jsutils/toError.d.ts +2 -0
- package/jsutils/toError.js +2 -0
- package/jsutils/toError.mjs +2 -0
- package/jsutils/toObjMap.d.ts +1 -0
- package/jsutils/toObjMap.js +1 -0
- package/jsutils/toObjMap.mjs +1 -0
- package/language/ast.d.ts +412 -54
- package/language/ast.js +95 -38
- package/language/ast.mjs +95 -38
- package/language/blockString.d.ts +1 -3
- package/language/blockString.js +1 -3
- package/language/blockString.mjs +1 -3
- package/language/directiveLocation.d.ts +28 -8
- package/language/directiveLocation.js +9 -6
- package/language/directiveLocation.mjs +9 -6
- package/language/index.d.ts +6 -0
- package/language/index.mjs +6 -0
- package/language/kinds.d.ts +57 -18
- package/language/kinds.js +9 -6
- package/language/kinds.mjs +9 -6
- package/language/lexer.d.ts +47 -14
- package/language/lexer.js +71 -13
- package/language/lexer.mjs +70 -13
- package/language/location.d.ts +16 -3
- package/language/location.js +14 -3
- package/language/location.mjs +14 -3
- package/language/parser.d.ts +236 -13
- package/language/parser.js +224 -0
- package/language/parser.mjs +224 -3
- package/language/predicates.d.ts +169 -0
- package/language/predicates.js +170 -0
- package/language/predicates.mjs +180 -0
- package/language/printLocation.d.ts +28 -0
- package/language/printLocation.js +29 -0
- package/language/printLocation.mjs +28 -0
- package/language/printString.d.ts +2 -0
- package/language/printString.js +5 -1
- package/language/printString.mjs +5 -1
- package/language/printer.d.ts +12 -0
- package/language/printer.js +19 -0
- package/language/printer.mjs +18 -0
- package/language/schemaCoordinateLexer.d.ts +8 -6
- package/language/schemaCoordinateLexer.js +10 -6
- package/language/schemaCoordinateLexer.mjs +10 -6
- package/language/source.d.ts +28 -0
- package/language/source.js +32 -0
- package/language/source.mjs +31 -0
- package/language/tokenKind.d.ts +30 -3
- package/language/tokenKind.js +8 -3
- package/language/tokenKind.mjs +8 -3
- package/language/visitor.d.ts +200 -72
- package/language/visitor.js +122 -50
- package/language/visitor.mjs +122 -54
- package/package.json +1 -1
- package/subscription/index.d.ts +9 -3
- package/subscription/index.mjs +9 -3
- package/type/assertName.d.ts +18 -1
- package/type/assertName.js +19 -1
- package/type/assertName.mjs +18 -1
- package/type/definition.d.ts +2540 -79
- package/type/definition.js +2214 -61
- package/type/definition.mjs +2241 -60
- package/type/directives.d.ts +193 -18
- package/type/directives.js +196 -19
- package/type/directives.mjs +196 -19
- package/type/index.d.ts +6 -0
- package/type/index.mjs +6 -0
- package/type/introspection.d.ts +36 -0
- package/type/introspection.js +33 -0
- package/type/introspection.mjs +41 -0
- package/type/scalars.d.ts +29 -2
- package/type/scalars.js +37 -2
- package/type/scalars.mjs +36 -2
- package/type/schema.d.ts +490 -28
- package/type/schema.js +484 -26
- package/type/schema.mjs +484 -26
- package/type/validate.d.ts +31 -0
- package/type/validate.js +32 -0
- package/type/validate.mjs +31 -0
- package/utilities/TypeInfo.d.ts +441 -1
- package/utilities/TypeInfo.js +444 -1
- package/utilities/TypeInfo.mjs +443 -1
- package/utilities/assertValidName.d.ts +27 -2
- package/utilities/assertValidName.js +28 -2
- package/utilities/assertValidName.mjs +27 -2
- package/utilities/astFromValue.d.ts +33 -3
- package/utilities/astFromValue.js +36 -3
- package/utilities/astFromValue.mjs +35 -3
- package/utilities/buildASTSchema.d.ts +65 -6
- package/utilities/buildASTSchema.js +65 -6
- package/utilities/buildASTSchema.mjs +65 -6
- package/utilities/buildClientSchema.d.ts +15 -0
- package/utilities/buildClientSchema.js +16 -0
- package/utilities/buildClientSchema.mjs +15 -0
- package/utilities/coerceInputValue.d.ts +46 -0
- package/utilities/coerceInputValue.js +47 -0
- package/utilities/coerceInputValue.mjs +46 -0
- package/utilities/concatAST.d.ts +12 -0
- package/utilities/concatAST.js +13 -0
- package/utilities/concatAST.mjs +12 -0
- package/utilities/extendSchema.d.ts +56 -3
- package/utilities/extendSchema.js +63 -3
- package/utilities/extendSchema.mjs +62 -3
- package/utilities/findBreakingChanges.d.ts +95 -0
- package/utilities/findBreakingChanges.js +68 -0
- package/utilities/findBreakingChanges.mjs +70 -0
- package/utilities/getIntrospectionQuery.d.ts +132 -0
- package/utilities/getIntrospectionQuery.js +41 -0
- package/utilities/getIntrospectionQuery.mjs +41 -0
- package/utilities/getOperationAST.d.ts +15 -0
- package/utilities/getOperationAST.js +16 -0
- package/utilities/getOperationAST.mjs +15 -0
- package/utilities/getOperationRootType.d.ts +18 -1
- package/utilities/getOperationRootType.js +19 -1
- package/utilities/getOperationRootType.mjs +18 -1
- package/utilities/index.d.ts +7 -0
- package/utilities/index.mjs +7 -0
- package/utilities/introspectionFromSchema.d.ts +54 -0
- package/utilities/introspectionFromSchema.js +55 -0
- package/utilities/introspectionFromSchema.mjs +54 -0
- package/utilities/lexicographicSortSchema.d.ts +35 -0
- package/utilities/lexicographicSortSchema.js +36 -0
- package/utilities/lexicographicSortSchema.mjs +35 -0
- package/utilities/printSchema.d.ts +63 -0
- package/utilities/printSchema.js +66 -0
- package/utilities/printSchema.mjs +67 -0
- package/utilities/resolveSchemaCoordinate.d.ts +62 -0
- package/utilities/resolveSchemaCoordinate.js +64 -0
- package/utilities/resolveSchemaCoordinate.mjs +63 -0
- package/utilities/separateOperations.d.ts +30 -0
- package/utilities/separateOperations.js +31 -0
- package/utilities/separateOperations.mjs +30 -0
- package/utilities/stripIgnoredCharacters.d.ts +13 -6
- package/utilities/stripIgnoredCharacters.js +16 -6
- package/utilities/stripIgnoredCharacters.mjs +15 -6
- package/utilities/typeComparators.d.ts +84 -0
- package/utilities/typeComparators.js +85 -0
- package/utilities/typeComparators.mjs +84 -0
- package/utilities/typeFromAST.d.ts +86 -0
- package/utilities/typeFromAST.js +3 -0
- package/utilities/typeFromAST.mjs +3 -0
- package/utilities/typedQueryDocumentNode.d.ts +4 -0
- package/utilities/valueFromAST.d.ts +38 -0
- package/utilities/valueFromAST.js +39 -0
- package/utilities/valueFromAST.mjs +38 -0
- package/utilities/valueFromASTUntyped.d.ts +15 -2
- package/utilities/valueFromASTUntyped.js +16 -2
- package/utilities/valueFromASTUntyped.mjs +15 -2
- package/validation/ValidationContext.d.ts +399 -0
- package/validation/ValidationContext.js +400 -0
- package/validation/ValidationContext.mjs +401 -0
- package/validation/index.d.ts +6 -0
- package/validation/index.mjs +6 -0
- package/validation/rules/ExecutableDefinitionsRule.d.ts +28 -0
- package/validation/rules/ExecutableDefinitionsRule.js +29 -0
- package/validation/rules/ExecutableDefinitionsRule.mjs +28 -0
- package/validation/rules/FieldsOnCorrectTypeRule.d.ts +28 -0
- package/validation/rules/FieldsOnCorrectTypeRule.js +33 -0
- package/validation/rules/FieldsOnCorrectTypeRule.mjs +32 -0
- package/validation/rules/FragmentsOnCompositeTypesRule.d.ts +28 -0
- package/validation/rules/FragmentsOnCompositeTypesRule.js +29 -0
- package/validation/rules/FragmentsOnCompositeTypesRule.mjs +28 -0
- package/validation/rules/KnownArgumentNamesRule.d.ts +29 -3
- package/validation/rules/KnownArgumentNamesRule.js +30 -3
- package/validation/rules/KnownArgumentNamesRule.mjs +29 -3
- package/validation/rules/KnownDirectivesRule.d.ts +28 -0
- package/validation/rules/KnownDirectivesRule.js +29 -0
- package/validation/rules/KnownDirectivesRule.mjs +28 -0
- package/validation/rules/KnownFragmentNamesRule.d.ts +28 -0
- package/validation/rules/KnownFragmentNamesRule.js +29 -0
- package/validation/rules/KnownFragmentNamesRule.mjs +28 -0
- package/validation/rules/KnownTypeNamesRule.d.ts +28 -0
- package/validation/rules/KnownTypeNamesRule.js +29 -0
- package/validation/rules/KnownTypeNamesRule.mjs +28 -0
- package/validation/rules/LoneAnonymousOperationRule.d.ts +28 -0
- package/validation/rules/LoneAnonymousOperationRule.js +29 -0
- package/validation/rules/LoneAnonymousOperationRule.mjs +28 -0
- package/validation/rules/LoneSchemaDefinitionRule.d.ts +21 -0
- package/validation/rules/LoneSchemaDefinitionRule.js +22 -0
- package/validation/rules/LoneSchemaDefinitionRule.mjs +21 -0
- package/validation/rules/MaxIntrospectionDepthRule.d.ts +31 -0
- package/validation/rules/MaxIntrospectionDepthRule.js +33 -0
- package/validation/rules/MaxIntrospectionDepthRule.mjs +34 -0
- package/validation/rules/NoFragmentCyclesRule.d.ts +28 -0
- package/validation/rules/NoFragmentCyclesRule.js +29 -0
- package/validation/rules/NoFragmentCyclesRule.mjs +28 -0
- package/validation/rules/NoUndefinedVariablesRule.d.ts +28 -0
- package/validation/rules/NoUndefinedVariablesRule.js +29 -0
- package/validation/rules/NoUndefinedVariablesRule.mjs +28 -0
- package/validation/rules/NoUnusedFragmentsRule.d.ts +28 -0
- package/validation/rules/NoUnusedFragmentsRule.js +29 -0
- package/validation/rules/NoUnusedFragmentsRule.mjs +28 -0
- package/validation/rules/NoUnusedVariablesRule.d.ts +29 -0
- package/validation/rules/NoUnusedVariablesRule.js +30 -0
- package/validation/rules/NoUnusedVariablesRule.mjs +29 -0
- package/validation/rules/OverlappingFieldsCanBeMergedRule.d.ts +33 -0
- package/validation/rules/OverlappingFieldsCanBeMergedRule.js +45 -6
- package/validation/rules/OverlappingFieldsCanBeMergedRule.mjs +45 -6
- package/validation/rules/PossibleFragmentSpreadsRule.d.ts +36 -0
- package/validation/rules/PossibleFragmentSpreadsRule.js +37 -0
- package/validation/rules/PossibleFragmentSpreadsRule.mjs +36 -0
- package/validation/rules/PossibleTypeExtensionsRule.d.ts +21 -0
- package/validation/rules/PossibleTypeExtensionsRule.js +22 -0
- package/validation/rules/PossibleTypeExtensionsRule.mjs +21 -0
- package/validation/rules/ProvidedRequiredArgumentsRule.d.ts +29 -3
- package/validation/rules/ProvidedRequiredArgumentsRule.js +30 -3
- package/validation/rules/ProvidedRequiredArgumentsRule.mjs +29 -3
- package/validation/rules/ScalarLeafsRule.d.ts +28 -0
- package/validation/rules/ScalarLeafsRule.js +29 -0
- package/validation/rules/ScalarLeafsRule.mjs +28 -0
- package/validation/rules/SingleFieldSubscriptionsRule.d.ts +33 -0
- package/validation/rules/SingleFieldSubscriptionsRule.js +34 -0
- package/validation/rules/SingleFieldSubscriptionsRule.mjs +33 -0
- package/validation/rules/UniqueArgumentDefinitionNamesRule.d.ts +21 -0
- package/validation/rules/UniqueArgumentDefinitionNamesRule.js +22 -0
- package/validation/rules/UniqueArgumentDefinitionNamesRule.mjs +21 -0
- package/validation/rules/UniqueArgumentNamesRule.d.ts +28 -0
- package/validation/rules/UniqueArgumentNamesRule.js +29 -0
- package/validation/rules/UniqueArgumentNamesRule.mjs +28 -0
- package/validation/rules/UniqueDirectiveNamesRule.d.ts +21 -0
- package/validation/rules/UniqueDirectiveNamesRule.js +22 -0
- package/validation/rules/UniqueDirectiveNamesRule.mjs +21 -0
- package/validation/rules/UniqueDirectivesPerLocationRule.d.ts +28 -0
- package/validation/rules/UniqueDirectivesPerLocationRule.js +29 -0
- package/validation/rules/UniqueDirectivesPerLocationRule.mjs +28 -0
- package/validation/rules/UniqueEnumValueNamesRule.d.ts +21 -0
- package/validation/rules/UniqueEnumValueNamesRule.js +22 -0
- package/validation/rules/UniqueEnumValueNamesRule.mjs +21 -0
- package/validation/rules/UniqueFieldDefinitionNamesRule.d.ts +21 -0
- package/validation/rules/UniqueFieldDefinitionNamesRule.js +22 -0
- package/validation/rules/UniqueFieldDefinitionNamesRule.mjs +21 -0
- package/validation/rules/UniqueFragmentNamesRule.d.ts +28 -0
- package/validation/rules/UniqueFragmentNamesRule.js +29 -0
- package/validation/rules/UniqueFragmentNamesRule.mjs +28 -0
- package/validation/rules/UniqueInputFieldNamesRule.d.ts +32 -0
- package/validation/rules/UniqueInputFieldNamesRule.js +33 -0
- package/validation/rules/UniqueInputFieldNamesRule.mjs +32 -0
- package/validation/rules/UniqueOperationNamesRule.d.ts +28 -0
- package/validation/rules/UniqueOperationNamesRule.js +29 -0
- package/validation/rules/UniqueOperationNamesRule.mjs +28 -0
- package/validation/rules/UniqueOperationTypesRule.d.ts +21 -0
- package/validation/rules/UniqueOperationTypesRule.js +22 -0
- package/validation/rules/UniqueOperationTypesRule.mjs +21 -0
- package/validation/rules/UniqueTypeNamesRule.d.ts +21 -0
- package/validation/rules/UniqueTypeNamesRule.js +22 -0
- package/validation/rules/UniqueTypeNamesRule.mjs +21 -0
- package/validation/rules/UniqueVariableNamesRule.d.ts +28 -0
- package/validation/rules/UniqueVariableNamesRule.js +29 -0
- package/validation/rules/UniqueVariableNamesRule.mjs +28 -0
- package/validation/rules/ValuesOfCorrectTypeRule.d.ts +28 -0
- package/validation/rules/ValuesOfCorrectTypeRule.js +31 -0
- package/validation/rules/ValuesOfCorrectTypeRule.mjs +30 -0
- package/validation/rules/VariablesAreInputTypesRule.d.ts +32 -0
- package/validation/rules/VariablesAreInputTypesRule.js +33 -0
- package/validation/rules/VariablesAreInputTypesRule.mjs +32 -0
- package/validation/rules/VariablesInAllowedPositionRule.d.ts +28 -0
- package/validation/rules/VariablesInAllowedPositionRule.js +35 -1
- package/validation/rules/VariablesInAllowedPositionRule.mjs +34 -1
- package/validation/rules/custom/NoDeprecatedCustomRule.d.ts +41 -0
- package/validation/rules/custom/NoDeprecatedCustomRule.js +42 -0
- package/validation/rules/custom/NoDeprecatedCustomRule.mjs +41 -0
- package/validation/rules/custom/NoSchemaIntrospectionCustomRule.d.ts +28 -0
- package/validation/rules/custom/NoSchemaIntrospectionCustomRule.js +29 -0
- package/validation/rules/custom/NoSchemaIntrospectionCustomRule.mjs +28 -0
- package/validation/specifiedRules.d.ts +2 -3
- package/validation/specifiedRules.js +2 -3
- package/validation/specifiedRules.mjs +2 -3
- package/validation/validate.d.ts +70 -9
- package/validation/validate.js +63 -7
- package/validation/validate.mjs +63 -7
- package/version.d.ts +3 -6
- package/version.js +6 -8
- package/version.mjs +5 -8
package/type/definition.js
CHANGED
|
@@ -87,6 +87,29 @@ var _valueFromASTUntyped = require('../utilities/valueFromASTUntyped.js');
|
|
|
87
87
|
|
|
88
88
|
var _assertName = require('./assertName.js');
|
|
89
89
|
|
|
90
|
+
/** @category Types */
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns true when the value is any GraphQL type.
|
|
94
|
+
* @param type - The GraphQL type to inspect.
|
|
95
|
+
* @returns True when the value is any GraphQL type.
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
99
|
+
* import { GraphQLList, GraphQLString, isType } from 'graphql/type';
|
|
100
|
+
*
|
|
101
|
+
* const schema = buildSchema(`
|
|
102
|
+
* type Query {
|
|
103
|
+
* name: String
|
|
104
|
+
* }
|
|
105
|
+
* `);
|
|
106
|
+
*
|
|
107
|
+
* isType(GraphQLString); // => true
|
|
108
|
+
* isType(new GraphQLList(GraphQLString)); // => true
|
|
109
|
+
* isType(schema.getType('Query')); // => true
|
|
110
|
+
* isType('String'); // => false
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
90
113
|
function isType(type) {
|
|
91
114
|
return (
|
|
92
115
|
isScalarType(type) ||
|
|
@@ -99,6 +122,27 @@ function isType(type) {
|
|
|
99
122
|
isNonNullType(type)
|
|
100
123
|
);
|
|
101
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Returns the value as a GraphQL type, or throws if it is not one.
|
|
127
|
+
* @param type - The GraphQL type to inspect.
|
|
128
|
+
* @returns The value typed as a GraphQL type.
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
132
|
+
* import { assertType } from 'graphql/type';
|
|
133
|
+
*
|
|
134
|
+
* const schema = buildSchema(`
|
|
135
|
+
* type Query {
|
|
136
|
+
* name: String
|
|
137
|
+
* }
|
|
138
|
+
* `);
|
|
139
|
+
*
|
|
140
|
+
* const queryType = assertType(schema.getType('Query'));
|
|
141
|
+
*
|
|
142
|
+
* queryType.toString(); // => 'Query'
|
|
143
|
+
* assertType('Query'); // throws an error
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
102
146
|
|
|
103
147
|
function assertType(type) {
|
|
104
148
|
if (!isType(type)) {
|
|
@@ -111,11 +155,52 @@ function assertType(type) {
|
|
|
111
155
|
}
|
|
112
156
|
/**
|
|
113
157
|
* There are predicates for each kind of GraphQL type.
|
|
158
|
+
* @param type - The GraphQL type to inspect.
|
|
159
|
+
* @returns True when the value is a GraphQLScalarType.
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
163
|
+
* import { isScalarType } from 'graphql/type';
|
|
164
|
+
*
|
|
165
|
+
* const schema = buildSchema(`
|
|
166
|
+
* scalar DateTime
|
|
167
|
+
*
|
|
168
|
+
* type Query {
|
|
169
|
+
* createdAt: DateTime
|
|
170
|
+
* }
|
|
171
|
+
* `);
|
|
172
|
+
*
|
|
173
|
+
* isScalarType(schema.getType('DateTime')); // => true
|
|
174
|
+
* isScalarType(schema.getType('Query')); // => false
|
|
175
|
+
* ```
|
|
114
176
|
*/
|
|
115
177
|
|
|
116
178
|
function isScalarType(type) {
|
|
117
179
|
return (0, _instanceOf.instanceOf)(type, GraphQLScalarType);
|
|
118
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Returns the value as a GraphQLScalarType, or throws if it is not one.
|
|
183
|
+
* @param type - The GraphQL type to inspect.
|
|
184
|
+
* @returns The value typed as a GraphQLScalarType.
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
188
|
+
* import { assertScalarType } from 'graphql/type';
|
|
189
|
+
*
|
|
190
|
+
* const schema = buildSchema(`
|
|
191
|
+
* scalar DateTime
|
|
192
|
+
*
|
|
193
|
+
* type Query {
|
|
194
|
+
* createdAt: DateTime
|
|
195
|
+
* }
|
|
196
|
+
* `);
|
|
197
|
+
*
|
|
198
|
+
* const dateTimeType = assertScalarType(schema.getType('DateTime'));
|
|
199
|
+
*
|
|
200
|
+
* dateTimeType.name; // => 'DateTime'
|
|
201
|
+
* assertScalarType(schema.getType('Query')); // throws an error
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
119
204
|
|
|
120
205
|
function assertScalarType(type) {
|
|
121
206
|
if (!isScalarType(type)) {
|
|
@@ -126,10 +211,66 @@ function assertScalarType(type) {
|
|
|
126
211
|
|
|
127
212
|
return type;
|
|
128
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Returns true when the value is a GraphQLObjectType.
|
|
216
|
+
* @param type - The GraphQL type to inspect.
|
|
217
|
+
* @returns True when the value is a GraphQLObjectType.
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
221
|
+
* import { isObjectType } from 'graphql/type';
|
|
222
|
+
*
|
|
223
|
+
* const schema = buildSchema(`
|
|
224
|
+
* input ReviewInput {
|
|
225
|
+
* stars: Int!
|
|
226
|
+
* }
|
|
227
|
+
*
|
|
228
|
+
* type User {
|
|
229
|
+
* name: String
|
|
230
|
+
* }
|
|
231
|
+
*
|
|
232
|
+
* type Query {
|
|
233
|
+
* user: User
|
|
234
|
+
* }
|
|
235
|
+
* `);
|
|
236
|
+
*
|
|
237
|
+
* isObjectType(schema.getType('User')); // => true
|
|
238
|
+
* isObjectType(schema.getType('ReviewInput')); // => false
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
129
241
|
|
|
130
242
|
function isObjectType(type) {
|
|
131
243
|
return (0, _instanceOf.instanceOf)(type, GraphQLObjectType);
|
|
132
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Returns the value as a GraphQLObjectType, or throws if it is not one.
|
|
247
|
+
* @param type - The GraphQL type to inspect.
|
|
248
|
+
* @returns The value typed as a GraphQLObjectType.
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
252
|
+
* import { assertObjectType } from 'graphql/type';
|
|
253
|
+
*
|
|
254
|
+
* const schema = buildSchema(`
|
|
255
|
+
* input ReviewInput {
|
|
256
|
+
* stars: Int!
|
|
257
|
+
* }
|
|
258
|
+
*
|
|
259
|
+
* type User {
|
|
260
|
+
* name: String
|
|
261
|
+
* }
|
|
262
|
+
*
|
|
263
|
+
* type Query {
|
|
264
|
+
* user: User
|
|
265
|
+
* }
|
|
266
|
+
* `);
|
|
267
|
+
*
|
|
268
|
+
* const userType = assertObjectType(schema.getType('User'));
|
|
269
|
+
*
|
|
270
|
+
* Object.keys(userType.getFields()); // => ['name']
|
|
271
|
+
* assertObjectType(schema.getType('ReviewInput')); // throws an error
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
133
274
|
|
|
134
275
|
function assertObjectType(type) {
|
|
135
276
|
if (!isObjectType(type)) {
|
|
@@ -140,10 +281,66 @@ function assertObjectType(type) {
|
|
|
140
281
|
|
|
141
282
|
return type;
|
|
142
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Returns true when the value is a GraphQLInterfaceType.
|
|
286
|
+
* @param type - The GraphQL type to inspect.
|
|
287
|
+
* @returns True when the value is a GraphQLInterfaceType.
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
291
|
+
* import { isInterfaceType } from 'graphql/type';
|
|
292
|
+
*
|
|
293
|
+
* const schema = buildSchema(`
|
|
294
|
+
* interface Node {
|
|
295
|
+
* id: ID!
|
|
296
|
+
* }
|
|
297
|
+
*
|
|
298
|
+
* type User implements Node {
|
|
299
|
+
* id: ID!
|
|
300
|
+
* }
|
|
301
|
+
*
|
|
302
|
+
* type Query {
|
|
303
|
+
* node: Node
|
|
304
|
+
* }
|
|
305
|
+
* `);
|
|
306
|
+
*
|
|
307
|
+
* isInterfaceType(schema.getType('Node')); // => true
|
|
308
|
+
* isInterfaceType(schema.getType('User')); // => false
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
143
311
|
|
|
144
312
|
function isInterfaceType(type) {
|
|
145
313
|
return (0, _instanceOf.instanceOf)(type, GraphQLInterfaceType);
|
|
146
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Returns the value as a GraphQLInterfaceType, or throws if it is not one.
|
|
317
|
+
* @param type - The GraphQL type to inspect.
|
|
318
|
+
* @returns The value typed as a GraphQLInterfaceType.
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
322
|
+
* import { assertInterfaceType } from 'graphql/type';
|
|
323
|
+
*
|
|
324
|
+
* const schema = buildSchema(`
|
|
325
|
+
* interface Node {
|
|
326
|
+
* id: ID!
|
|
327
|
+
* }
|
|
328
|
+
*
|
|
329
|
+
* type User implements Node {
|
|
330
|
+
* id: ID!
|
|
331
|
+
* }
|
|
332
|
+
*
|
|
333
|
+
* type Query {
|
|
334
|
+
* node: Node
|
|
335
|
+
* }
|
|
336
|
+
* `);
|
|
337
|
+
*
|
|
338
|
+
* const nodeType = assertInterfaceType(schema.getType('Node'));
|
|
339
|
+
*
|
|
340
|
+
* nodeType.name; // => 'Node'
|
|
341
|
+
* assertInterfaceType(schema.getType('User')); // throws an error
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
147
344
|
|
|
148
345
|
function assertInterfaceType(type) {
|
|
149
346
|
if (!isInterfaceType(type)) {
|
|
@@ -154,10 +351,70 @@ function assertInterfaceType(type) {
|
|
|
154
351
|
|
|
155
352
|
return type;
|
|
156
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* Returns true when the value is a GraphQLUnionType.
|
|
356
|
+
* @param type - The GraphQL type to inspect.
|
|
357
|
+
* @returns True when the value is a GraphQLUnionType.
|
|
358
|
+
* @example
|
|
359
|
+
* ```ts
|
|
360
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
361
|
+
* import { isUnionType } from 'graphql/type';
|
|
362
|
+
*
|
|
363
|
+
* const schema = buildSchema(`
|
|
364
|
+
* type Photo {
|
|
365
|
+
* url: String!
|
|
366
|
+
* }
|
|
367
|
+
*
|
|
368
|
+
* type Video {
|
|
369
|
+
* url: String!
|
|
370
|
+
* }
|
|
371
|
+
*
|
|
372
|
+
* union Media = Photo | Video
|
|
373
|
+
*
|
|
374
|
+
* type Query {
|
|
375
|
+
* media: [Media]
|
|
376
|
+
* }
|
|
377
|
+
* `);
|
|
378
|
+
*
|
|
379
|
+
* isUnionType(schema.getType('Media')); // => true
|
|
380
|
+
* isUnionType(schema.getType('Photo')); // => false
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
157
383
|
|
|
158
384
|
function isUnionType(type) {
|
|
159
385
|
return (0, _instanceOf.instanceOf)(type, GraphQLUnionType);
|
|
160
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Returns the value as a GraphQLUnionType, or throws if it is not one.
|
|
389
|
+
* @param type - The GraphQL type to inspect.
|
|
390
|
+
* @returns The value typed as a GraphQLUnionType.
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
394
|
+
* import { assertUnionType } from 'graphql/type';
|
|
395
|
+
*
|
|
396
|
+
* const schema = buildSchema(`
|
|
397
|
+
* type Photo {
|
|
398
|
+
* url: String!
|
|
399
|
+
* }
|
|
400
|
+
*
|
|
401
|
+
* type Video {
|
|
402
|
+
* url: String!
|
|
403
|
+
* }
|
|
404
|
+
*
|
|
405
|
+
* union Media = Photo | Video
|
|
406
|
+
*
|
|
407
|
+
* type Query {
|
|
408
|
+
* media: [Media]
|
|
409
|
+
* }
|
|
410
|
+
* `);
|
|
411
|
+
*
|
|
412
|
+
* const mediaType = assertUnionType(schema.getType('Media'));
|
|
413
|
+
*
|
|
414
|
+
* mediaType.getTypes().map((type) => type.name); // => ['Photo', 'Video']
|
|
415
|
+
* assertUnionType(schema.getType('Photo')); // throws an error
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
161
418
|
|
|
162
419
|
function assertUnionType(type) {
|
|
163
420
|
if (!isUnionType(type)) {
|
|
@@ -168,10 +425,60 @@ function assertUnionType(type) {
|
|
|
168
425
|
|
|
169
426
|
return type;
|
|
170
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* Returns true when the value is a GraphQLEnumType.
|
|
430
|
+
* @param type - The GraphQL type to inspect.
|
|
431
|
+
* @returns True when the value is a GraphQLEnumType.
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
435
|
+
* import { isEnumType } from 'graphql/type';
|
|
436
|
+
*
|
|
437
|
+
* const schema = buildSchema(`
|
|
438
|
+
* enum Episode {
|
|
439
|
+
* NEW_HOPE
|
|
440
|
+
* EMPIRE
|
|
441
|
+
* }
|
|
442
|
+
*
|
|
443
|
+
* type Query {
|
|
444
|
+
* favoriteEpisode: Episode
|
|
445
|
+
* }
|
|
446
|
+
* `);
|
|
447
|
+
*
|
|
448
|
+
* isEnumType(schema.getType('Episode')); // => true
|
|
449
|
+
* isEnumType(schema.getType('Query')); // => false
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
171
452
|
|
|
172
453
|
function isEnumType(type) {
|
|
173
454
|
return (0, _instanceOf.instanceOf)(type, GraphQLEnumType);
|
|
174
455
|
}
|
|
456
|
+
/**
|
|
457
|
+
* Returns the value as a GraphQLEnumType, or throws if it is not one.
|
|
458
|
+
* @param type - The GraphQL type to inspect.
|
|
459
|
+
* @returns The value typed as a GraphQLEnumType.
|
|
460
|
+
* @example
|
|
461
|
+
* ```ts
|
|
462
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
463
|
+
* import { assertEnumType } from 'graphql/type';
|
|
464
|
+
*
|
|
465
|
+
* const schema = buildSchema(`
|
|
466
|
+
* enum Episode {
|
|
467
|
+
* NEW_HOPE
|
|
468
|
+
* EMPIRE
|
|
469
|
+
* }
|
|
470
|
+
*
|
|
471
|
+
* type Query {
|
|
472
|
+
* favoriteEpisode: Episode
|
|
473
|
+
* }
|
|
474
|
+
* `);
|
|
475
|
+
*
|
|
476
|
+
* const episodeType = assertEnumType(schema.getType('Episode'));
|
|
477
|
+
*
|
|
478
|
+
* episodeType.getValues().map((value) => value.name); // => ['NEW_HOPE', 'EMPIRE']
|
|
479
|
+
* assertEnumType(schema.getType('Query')); // throws an error
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
175
482
|
|
|
176
483
|
function assertEnumType(type) {
|
|
177
484
|
if (!isEnumType(type)) {
|
|
@@ -182,10 +489,66 @@ function assertEnumType(type) {
|
|
|
182
489
|
|
|
183
490
|
return type;
|
|
184
491
|
}
|
|
492
|
+
/**
|
|
493
|
+
* Returns true when the value is a GraphQLInputObjectType.
|
|
494
|
+
* @param type - The GraphQL type to inspect.
|
|
495
|
+
* @returns True when the value is a GraphQLInputObjectType.
|
|
496
|
+
* @example
|
|
497
|
+
* ```ts
|
|
498
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
499
|
+
* import { isInputObjectType } from 'graphql/type';
|
|
500
|
+
*
|
|
501
|
+
* const schema = buildSchema(`
|
|
502
|
+
* input ReviewInput {
|
|
503
|
+
* stars: Int!
|
|
504
|
+
* }
|
|
505
|
+
*
|
|
506
|
+
* type Review {
|
|
507
|
+
* stars: Int!
|
|
508
|
+
* }
|
|
509
|
+
*
|
|
510
|
+
* type Query {
|
|
511
|
+
* review(input: ReviewInput): Review
|
|
512
|
+
* }
|
|
513
|
+
* `);
|
|
514
|
+
*
|
|
515
|
+
* isInputObjectType(schema.getType('ReviewInput')); // => true
|
|
516
|
+
* isInputObjectType(schema.getType('Review')); // => false
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
185
519
|
|
|
186
520
|
function isInputObjectType(type) {
|
|
187
521
|
return (0, _instanceOf.instanceOf)(type, GraphQLInputObjectType);
|
|
188
522
|
}
|
|
523
|
+
/**
|
|
524
|
+
* Returns the value as a GraphQLInputObjectType, or throws if it is not one.
|
|
525
|
+
* @param type - The GraphQL type to inspect.
|
|
526
|
+
* @returns The value typed as a GraphQLInputObjectType.
|
|
527
|
+
* @example
|
|
528
|
+
* ```ts
|
|
529
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
530
|
+
* import { assertInputObjectType } from 'graphql/type';
|
|
531
|
+
*
|
|
532
|
+
* const schema = buildSchema(`
|
|
533
|
+
* input ReviewInput {
|
|
534
|
+
* stars: Int!
|
|
535
|
+
* }
|
|
536
|
+
*
|
|
537
|
+
* type Review {
|
|
538
|
+
* stars: Int!
|
|
539
|
+
* }
|
|
540
|
+
*
|
|
541
|
+
* type Query {
|
|
542
|
+
* review(input: ReviewInput): Review
|
|
543
|
+
* }
|
|
544
|
+
* `);
|
|
545
|
+
*
|
|
546
|
+
* const inputType = assertInputObjectType(schema.getType('ReviewInput'));
|
|
547
|
+
*
|
|
548
|
+
* Object.keys(inputType.getFields()); // => ['stars']
|
|
549
|
+
* assertInputObjectType(schema.getType('Review')); // throws an error
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
189
552
|
|
|
190
553
|
function assertInputObjectType(type) {
|
|
191
554
|
if (!isInputObjectType(type)) {
|
|
@@ -198,10 +561,47 @@ function assertInputObjectType(type) {
|
|
|
198
561
|
|
|
199
562
|
return type;
|
|
200
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* Returns true when the value is a GraphQLList.
|
|
566
|
+
* @param type - The GraphQL type to inspect.
|
|
567
|
+
* @returns True when the value is a GraphQLList.
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
571
|
+
* import { GraphQLList, GraphQLString, isListType } from 'graphql/type';
|
|
572
|
+
*
|
|
573
|
+
* const schema = buildSchema(`
|
|
574
|
+
* type Query {
|
|
575
|
+
* tags: [String!]!
|
|
576
|
+
* }
|
|
577
|
+
* `);
|
|
578
|
+
*
|
|
579
|
+
* const tagsField = schema.getQueryType()?.getFields().tags;
|
|
580
|
+
*
|
|
581
|
+
* isListType(new GraphQLList(GraphQLString)); // => true
|
|
582
|
+
* isListType(GraphQLString); // => false
|
|
583
|
+
* isListType(tagsField?.type); // => false
|
|
584
|
+
* ```
|
|
585
|
+
*/
|
|
201
586
|
|
|
587
|
+
/** @internal */
|
|
202
588
|
function isListType(type) {
|
|
203
589
|
return (0, _instanceOf.instanceOf)(type, GraphQLList);
|
|
204
590
|
}
|
|
591
|
+
/**
|
|
592
|
+
* Returns the value as a GraphQLList, or throws if it is not one.
|
|
593
|
+
* @param type - The GraphQL type to inspect.
|
|
594
|
+
* @returns The value typed as a GraphQLList.
|
|
595
|
+
* @example
|
|
596
|
+
* ```ts
|
|
597
|
+
* import { GraphQLList, GraphQLString, assertListType } from 'graphql/type';
|
|
598
|
+
*
|
|
599
|
+
* const listType = assertListType(new GraphQLList(GraphQLString));
|
|
600
|
+
*
|
|
601
|
+
* listType.ofType; // => GraphQLString
|
|
602
|
+
* assertListType(GraphQLString); // throws an error
|
|
603
|
+
* ```
|
|
604
|
+
*/
|
|
205
605
|
|
|
206
606
|
function assertListType(type) {
|
|
207
607
|
if (!isListType(type)) {
|
|
@@ -212,10 +612,48 @@ function assertListType(type) {
|
|
|
212
612
|
|
|
213
613
|
return type;
|
|
214
614
|
}
|
|
615
|
+
/**
|
|
616
|
+
* Returns true when the value is a GraphQLNonNull.
|
|
617
|
+
* @param type - The GraphQL type to inspect.
|
|
618
|
+
* @returns True when the value is a GraphQLNonNull.
|
|
619
|
+
* @example
|
|
620
|
+
* ```ts
|
|
621
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
622
|
+
* import { GraphQLNonNull, GraphQLString, isNonNullType } from 'graphql/type';
|
|
623
|
+
*
|
|
624
|
+
* const schema = buildSchema(`
|
|
625
|
+
* type Query {
|
|
626
|
+
* name: String!
|
|
627
|
+
* nickname: String
|
|
628
|
+
* }
|
|
629
|
+
* `);
|
|
630
|
+
*
|
|
631
|
+
* const fields = schema.getQueryType()?.getFields();
|
|
632
|
+
*
|
|
633
|
+
* isNonNullType(new GraphQLNonNull(GraphQLString)); // => true
|
|
634
|
+
* isNonNullType(fields?.name.type); // => true
|
|
635
|
+
* isNonNullType(fields?.nickname.type); // => false
|
|
636
|
+
* ```
|
|
637
|
+
*/
|
|
215
638
|
|
|
639
|
+
/** @internal */
|
|
216
640
|
function isNonNullType(type) {
|
|
217
641
|
return (0, _instanceOf.instanceOf)(type, GraphQLNonNull);
|
|
218
642
|
}
|
|
643
|
+
/**
|
|
644
|
+
* Returns the value as a GraphQLNonNull, or throws if it is not one.
|
|
645
|
+
* @param type - The GraphQL type to inspect.
|
|
646
|
+
* @returns The value typed as a GraphQLNonNull.
|
|
647
|
+
* @example
|
|
648
|
+
* ```ts
|
|
649
|
+
* import { GraphQLNonNull, GraphQLString, assertNonNullType } from 'graphql/type';
|
|
650
|
+
*
|
|
651
|
+
* const nonNullType = assertNonNullType(new GraphQLNonNull(GraphQLString));
|
|
652
|
+
*
|
|
653
|
+
* nonNullType.ofType; // => GraphQLString
|
|
654
|
+
* assertNonNullType(GraphQLString); // throws an error
|
|
655
|
+
* ```
|
|
656
|
+
*/
|
|
219
657
|
|
|
220
658
|
function assertNonNullType(type) {
|
|
221
659
|
if (!isNonNullType(type)) {
|
|
@@ -226,10 +664,35 @@ function assertNonNullType(type) {
|
|
|
226
664
|
|
|
227
665
|
return type;
|
|
228
666
|
}
|
|
667
|
+
/** These types may be used as input types for arguments and directives. */
|
|
668
|
+
|
|
229
669
|
/**
|
|
230
|
-
*
|
|
670
|
+
* Returns true when the value can be used as a GraphQL input type.
|
|
671
|
+
* @param type - The GraphQL type to inspect.
|
|
672
|
+
* @returns True when the value can be used as a GraphQL input type.
|
|
673
|
+
* @example
|
|
674
|
+
* ```ts
|
|
675
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
676
|
+
* import { isInputType } from 'graphql/type';
|
|
677
|
+
*
|
|
678
|
+
* const schema = buildSchema(`
|
|
679
|
+
* input ReviewInput {
|
|
680
|
+
* stars: Int!
|
|
681
|
+
* }
|
|
682
|
+
*
|
|
683
|
+
* type Review {
|
|
684
|
+
* stars: Int!
|
|
685
|
+
* }
|
|
686
|
+
*
|
|
687
|
+
* type Query {
|
|
688
|
+
* review(input: ReviewInput): Review
|
|
689
|
+
* }
|
|
690
|
+
* `);
|
|
691
|
+
*
|
|
692
|
+
* isInputType(schema.getType('ReviewInput')); // => true
|
|
693
|
+
* isInputType(schema.getType('Review')); // => false
|
|
694
|
+
* ```
|
|
231
695
|
*/
|
|
232
|
-
|
|
233
696
|
function isInputType(type) {
|
|
234
697
|
return (
|
|
235
698
|
isScalarType(type) ||
|
|
@@ -238,6 +701,35 @@ function isInputType(type) {
|
|
|
238
701
|
(isWrappingType(type) && isInputType(type.ofType))
|
|
239
702
|
);
|
|
240
703
|
}
|
|
704
|
+
/**
|
|
705
|
+
* Returns the value as a GraphQL input type, or throws if it is not one.
|
|
706
|
+
* @param type - The GraphQL type to inspect.
|
|
707
|
+
* @returns The value typed as a GraphQL input type.
|
|
708
|
+
* @example
|
|
709
|
+
* ```ts
|
|
710
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
711
|
+
* import { assertInputType } from 'graphql/type';
|
|
712
|
+
*
|
|
713
|
+
* const schema = buildSchema(`
|
|
714
|
+
* input ReviewInput {
|
|
715
|
+
* stars: Int!
|
|
716
|
+
* }
|
|
717
|
+
*
|
|
718
|
+
* type Review {
|
|
719
|
+
* stars: Int!
|
|
720
|
+
* }
|
|
721
|
+
*
|
|
722
|
+
* type Query {
|
|
723
|
+
* review(input: ReviewInput): Review
|
|
724
|
+
* }
|
|
725
|
+
* `);
|
|
726
|
+
*
|
|
727
|
+
* const inputType = assertInputType(schema.getType('ReviewInput'));
|
|
728
|
+
*
|
|
729
|
+
* inputType.toString(); // => 'ReviewInput'
|
|
730
|
+
* assertInputType(schema.getType('Review')); // throws an error
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
241
733
|
|
|
242
734
|
function assertInputType(type) {
|
|
243
735
|
if (!isInputType(type)) {
|
|
@@ -248,10 +740,35 @@ function assertInputType(type) {
|
|
|
248
740
|
|
|
249
741
|
return type;
|
|
250
742
|
}
|
|
743
|
+
/** These types may be used as output types as the result of fields. */
|
|
744
|
+
|
|
251
745
|
/**
|
|
252
|
-
*
|
|
746
|
+
* Returns true when the value can be used as a GraphQL output type.
|
|
747
|
+
* @param type - The GraphQL type to inspect.
|
|
748
|
+
* @returns True when the value can be used as a GraphQL output type.
|
|
749
|
+
* @example
|
|
750
|
+
* ```ts
|
|
751
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
752
|
+
* import { isOutputType } from 'graphql/type';
|
|
753
|
+
*
|
|
754
|
+
* const schema = buildSchema(`
|
|
755
|
+
* input ReviewInput {
|
|
756
|
+
* stars: Int!
|
|
757
|
+
* }
|
|
758
|
+
*
|
|
759
|
+
* type Review {
|
|
760
|
+
* stars: Int!
|
|
761
|
+
* }
|
|
762
|
+
*
|
|
763
|
+
* type Query {
|
|
764
|
+
* review(input: ReviewInput): Review
|
|
765
|
+
* }
|
|
766
|
+
* `);
|
|
767
|
+
*
|
|
768
|
+
* isOutputType(schema.getType('Review')); // => true
|
|
769
|
+
* isOutputType(schema.getType('ReviewInput')); // => false
|
|
770
|
+
* ```
|
|
253
771
|
*/
|
|
254
|
-
|
|
255
772
|
function isOutputType(type) {
|
|
256
773
|
return (
|
|
257
774
|
isScalarType(type) ||
|
|
@@ -262,6 +779,35 @@ function isOutputType(type) {
|
|
|
262
779
|
(isWrappingType(type) && isOutputType(type.ofType))
|
|
263
780
|
);
|
|
264
781
|
}
|
|
782
|
+
/**
|
|
783
|
+
* Returns the value as a GraphQL output type, or throws if it is not one.
|
|
784
|
+
* @param type - The GraphQL type to inspect.
|
|
785
|
+
* @returns The value typed as a GraphQL output type.
|
|
786
|
+
* @example
|
|
787
|
+
* ```ts
|
|
788
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
789
|
+
* import { assertOutputType } from 'graphql/type';
|
|
790
|
+
*
|
|
791
|
+
* const schema = buildSchema(`
|
|
792
|
+
* input ReviewInput {
|
|
793
|
+
* stars: Int!
|
|
794
|
+
* }
|
|
795
|
+
*
|
|
796
|
+
* type Review {
|
|
797
|
+
* stars: Int!
|
|
798
|
+
* }
|
|
799
|
+
*
|
|
800
|
+
* type Query {
|
|
801
|
+
* review(input: ReviewInput): Review
|
|
802
|
+
* }
|
|
803
|
+
* `);
|
|
804
|
+
*
|
|
805
|
+
* const outputType = assertOutputType(schema.getType('Review'));
|
|
806
|
+
*
|
|
807
|
+
* outputType.toString(); // => 'Review'
|
|
808
|
+
* assertOutputType(schema.getType('ReviewInput')); // throws an error
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
265
811
|
|
|
266
812
|
function assertOutputType(type) {
|
|
267
813
|
if (!isOutputType(type)) {
|
|
@@ -272,13 +818,70 @@ function assertOutputType(type) {
|
|
|
272
818
|
|
|
273
819
|
return type;
|
|
274
820
|
}
|
|
821
|
+
/** These types may describe types which may be leaf values. */
|
|
822
|
+
|
|
275
823
|
/**
|
|
276
|
-
*
|
|
824
|
+
* Returns true when the value is a GraphQL scalar or enum type.
|
|
825
|
+
* @param type - The GraphQL type to inspect.
|
|
826
|
+
* @returns True when the value is a GraphQL scalar or enum type.
|
|
827
|
+
* @example
|
|
828
|
+
* ```ts
|
|
829
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
830
|
+
* import { isLeafType } from 'graphql/type';
|
|
831
|
+
*
|
|
832
|
+
* const schema = buildSchema(`
|
|
833
|
+
* enum Episode {
|
|
834
|
+
* NEW_HOPE
|
|
835
|
+
* }
|
|
836
|
+
*
|
|
837
|
+
* type Review {
|
|
838
|
+
* stars: Int!
|
|
839
|
+
* }
|
|
840
|
+
*
|
|
841
|
+
* type Query {
|
|
842
|
+
* episode: Episode
|
|
843
|
+
* review: Review
|
|
844
|
+
* }
|
|
845
|
+
* `);
|
|
846
|
+
*
|
|
847
|
+
* isLeafType(schema.getType('Episode')); // => true
|
|
848
|
+
* isLeafType(schema.getType('String')); // => true
|
|
849
|
+
* isLeafType(schema.getType('Review')); // => false
|
|
850
|
+
* ```
|
|
277
851
|
*/
|
|
278
|
-
|
|
279
852
|
function isLeafType(type) {
|
|
280
853
|
return isScalarType(type) || isEnumType(type);
|
|
281
854
|
}
|
|
855
|
+
/**
|
|
856
|
+
* Returns the value as a GraphQL leaf type, or throws if it is not one.
|
|
857
|
+
* @param type - The GraphQL type to inspect.
|
|
858
|
+
* @returns The value typed as a GraphQL leaf type.
|
|
859
|
+
* @example
|
|
860
|
+
* ```ts
|
|
861
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
862
|
+
* import { assertLeafType } from 'graphql/type';
|
|
863
|
+
*
|
|
864
|
+
* const schema = buildSchema(`
|
|
865
|
+
* enum Episode {
|
|
866
|
+
* NEW_HOPE
|
|
867
|
+
* }
|
|
868
|
+
*
|
|
869
|
+
* type Review {
|
|
870
|
+
* stars: Int!
|
|
871
|
+
* }
|
|
872
|
+
*
|
|
873
|
+
* type Query {
|
|
874
|
+
* episode: Episode
|
|
875
|
+
* review: Review
|
|
876
|
+
* }
|
|
877
|
+
* `);
|
|
878
|
+
*
|
|
879
|
+
* const episodeType = assertLeafType(schema.getType('Episode'));
|
|
880
|
+
*
|
|
881
|
+
* episodeType.toString(); // => 'Episode'
|
|
882
|
+
* assertLeafType(schema.getType('Review')); // throws an error
|
|
883
|
+
* ```
|
|
884
|
+
*/
|
|
282
885
|
|
|
283
886
|
function assertLeafType(type) {
|
|
284
887
|
if (!isLeafType(type)) {
|
|
@@ -289,13 +892,72 @@ function assertLeafType(type) {
|
|
|
289
892
|
|
|
290
893
|
return type;
|
|
291
894
|
}
|
|
895
|
+
/** These types may describe the parent context of a selection set. */
|
|
896
|
+
|
|
292
897
|
/**
|
|
293
|
-
*
|
|
898
|
+
* Returns true when the value is a GraphQL object, interface, or union type.
|
|
899
|
+
* @param type - The GraphQL type to inspect.
|
|
900
|
+
* @returns True when the value is a GraphQL object, interface, or union type.
|
|
901
|
+
* @example
|
|
902
|
+
* ```ts
|
|
903
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
904
|
+
* import { isCompositeType } from 'graphql/type';
|
|
905
|
+
*
|
|
906
|
+
* const schema = buildSchema(`
|
|
907
|
+
* interface Node {
|
|
908
|
+
* id: ID!
|
|
909
|
+
* }
|
|
910
|
+
*
|
|
911
|
+
* type User implements Node {
|
|
912
|
+
* id: ID!
|
|
913
|
+
* }
|
|
914
|
+
*
|
|
915
|
+
* union SearchResult = User
|
|
916
|
+
*
|
|
917
|
+
* type Query {
|
|
918
|
+
* node: Node
|
|
919
|
+
* search: [SearchResult]
|
|
920
|
+
* }
|
|
921
|
+
* `);
|
|
922
|
+
*
|
|
923
|
+
* isCompositeType(schema.getType('User')); // => true
|
|
924
|
+
* isCompositeType(schema.getType('Node')); // => true
|
|
925
|
+
* isCompositeType(schema.getType('SearchResult')); // => true
|
|
926
|
+
* isCompositeType(schema.getType('String')); // => false
|
|
927
|
+
* ```
|
|
294
928
|
*/
|
|
295
|
-
|
|
296
929
|
function isCompositeType(type) {
|
|
297
930
|
return isObjectType(type) || isInterfaceType(type) || isUnionType(type);
|
|
298
931
|
}
|
|
932
|
+
/**
|
|
933
|
+
* Returns the value as a GraphQL composite type, or throws if it is not one.
|
|
934
|
+
* @param type - The GraphQL type to inspect.
|
|
935
|
+
* @returns The value typed as a GraphQL composite type.
|
|
936
|
+
* @example
|
|
937
|
+
* ```ts
|
|
938
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
939
|
+
* import { assertCompositeType } from 'graphql/type';
|
|
940
|
+
*
|
|
941
|
+
* const schema = buildSchema(`
|
|
942
|
+
* interface Node {
|
|
943
|
+
* id: ID!
|
|
944
|
+
* }
|
|
945
|
+
*
|
|
946
|
+
* type User implements Node {
|
|
947
|
+
* id: ID!
|
|
948
|
+
* }
|
|
949
|
+
*
|
|
950
|
+
* type Query {
|
|
951
|
+
* node: Node
|
|
952
|
+
* }
|
|
953
|
+
* `);
|
|
954
|
+
*
|
|
955
|
+
* const userType = assertCompositeType(schema.getType('User'));
|
|
956
|
+
*
|
|
957
|
+
* userType.toString(); // => 'User'
|
|
958
|
+
* assertCompositeType(schema.getType('String')); // throws an error
|
|
959
|
+
* ```
|
|
960
|
+
*/
|
|
299
961
|
|
|
300
962
|
function assertCompositeType(type) {
|
|
301
963
|
if (!isCompositeType(type)) {
|
|
@@ -306,13 +968,71 @@ function assertCompositeType(type) {
|
|
|
306
968
|
|
|
307
969
|
return type;
|
|
308
970
|
}
|
|
971
|
+
/** These types may describe the parent context of a selection set. */
|
|
972
|
+
|
|
309
973
|
/**
|
|
310
|
-
*
|
|
974
|
+
* Returns true when the value is a GraphQL interface or union type.
|
|
975
|
+
* @param type - The GraphQL type to inspect.
|
|
976
|
+
* @returns True when the value is a GraphQL interface or union type.
|
|
977
|
+
* @example
|
|
978
|
+
* ```ts
|
|
979
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
980
|
+
* import { isAbstractType } from 'graphql/type';
|
|
981
|
+
*
|
|
982
|
+
* const schema = buildSchema(`
|
|
983
|
+
* interface Node {
|
|
984
|
+
* id: ID!
|
|
985
|
+
* }
|
|
986
|
+
*
|
|
987
|
+
* type User implements Node {
|
|
988
|
+
* id: ID!
|
|
989
|
+
* }
|
|
990
|
+
*
|
|
991
|
+
* union SearchResult = User
|
|
992
|
+
*
|
|
993
|
+
* type Query {
|
|
994
|
+
* node: Node
|
|
995
|
+
* search: [SearchResult]
|
|
996
|
+
* }
|
|
997
|
+
* `);
|
|
998
|
+
*
|
|
999
|
+
* isAbstractType(schema.getType('Node')); // => true
|
|
1000
|
+
* isAbstractType(schema.getType('SearchResult')); // => true
|
|
1001
|
+
* isAbstractType(schema.getType('User')); // => false
|
|
1002
|
+
* ```
|
|
311
1003
|
*/
|
|
312
|
-
|
|
313
1004
|
function isAbstractType(type) {
|
|
314
1005
|
return isInterfaceType(type) || isUnionType(type);
|
|
315
1006
|
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Returns the value as a GraphQL abstract type, or throws if it is not one.
|
|
1009
|
+
* @param type - The GraphQL type to inspect.
|
|
1010
|
+
* @returns The value typed as a GraphQL abstract type.
|
|
1011
|
+
* @example
|
|
1012
|
+
* ```ts
|
|
1013
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
1014
|
+
* import { assertAbstractType } from 'graphql/type';
|
|
1015
|
+
*
|
|
1016
|
+
* const schema = buildSchema(`
|
|
1017
|
+
* interface Node {
|
|
1018
|
+
* id: ID!
|
|
1019
|
+
* }
|
|
1020
|
+
*
|
|
1021
|
+
* type User implements Node {
|
|
1022
|
+
* id: ID!
|
|
1023
|
+
* }
|
|
1024
|
+
*
|
|
1025
|
+
* type Query {
|
|
1026
|
+
* node: Node
|
|
1027
|
+
* }
|
|
1028
|
+
* `);
|
|
1029
|
+
*
|
|
1030
|
+
* const nodeType = assertAbstractType(schema.getType('Node'));
|
|
1031
|
+
*
|
|
1032
|
+
* nodeType.toString(); // => 'Node'
|
|
1033
|
+
* assertAbstractType(schema.getType('User')); // throws an error
|
|
1034
|
+
* ```
|
|
1035
|
+
*/
|
|
316
1036
|
|
|
317
1037
|
function assertAbstractType(type) {
|
|
318
1038
|
if (!isAbstractType(type)) {
|
|
@@ -329,9 +1049,8 @@ function assertAbstractType(type) {
|
|
|
329
1049
|
* A list is a wrapping type which points to another type.
|
|
330
1050
|
* Lists are often created within the context of defining the fields of
|
|
331
1051
|
* an object type.
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
1052
|
+
* @typeParam T - The GraphQL type wrapped by this list type.
|
|
1053
|
+
* @example
|
|
335
1054
|
* ```ts
|
|
336
1055
|
* const PersonType = new GraphQLObjectType({
|
|
337
1056
|
* name: 'Person',
|
|
@@ -344,6 +1063,21 @@ function assertAbstractType(type) {
|
|
|
344
1063
|
*/
|
|
345
1064
|
|
|
346
1065
|
class GraphQLList {
|
|
1066
|
+
/** The type wrapped by this list or non-null type. */
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* Creates a GraphQLList instance.
|
|
1070
|
+
* @param ofType - The type to wrap.
|
|
1071
|
+
* @example
|
|
1072
|
+
* ```ts
|
|
1073
|
+
* import { GraphQLList, GraphQLString } from 'graphql/type';
|
|
1074
|
+
*
|
|
1075
|
+
* const stringList = new GraphQLList(GraphQLString);
|
|
1076
|
+
*
|
|
1077
|
+
* stringList.ofType; // => GraphQLString
|
|
1078
|
+
* String(stringList); // => '[String]'
|
|
1079
|
+
* ```
|
|
1080
|
+
*/
|
|
347
1081
|
constructor(ofType) {
|
|
348
1082
|
isType(ofType) ||
|
|
349
1083
|
(0, _devAssert.devAssert)(
|
|
@@ -352,14 +1086,45 @@ class GraphQLList {
|
|
|
352
1086
|
);
|
|
353
1087
|
this.ofType = ofType;
|
|
354
1088
|
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
1091
|
+
* @returns The built-in string tag for this object.
|
|
1092
|
+
*/
|
|
355
1093
|
|
|
356
1094
|
get [Symbol.toStringTag]() {
|
|
357
1095
|
return 'GraphQLList';
|
|
358
1096
|
}
|
|
1097
|
+
/**
|
|
1098
|
+
* Returns this wrapping type as a GraphQL type-reference string.
|
|
1099
|
+
* @returns The GraphQL type-reference string.
|
|
1100
|
+
* @example
|
|
1101
|
+
* ```ts
|
|
1102
|
+
* import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql/type';
|
|
1103
|
+
*
|
|
1104
|
+
* const stringList = new GraphQLList(GraphQLString);
|
|
1105
|
+
* const requiredStringList = new GraphQLList(new GraphQLNonNull(GraphQLString));
|
|
1106
|
+
*
|
|
1107
|
+
* stringList.toString(); // => '[String]'
|
|
1108
|
+
* requiredStringList.toString(); // => '[String!]'
|
|
1109
|
+
* ```
|
|
1110
|
+
*/
|
|
359
1111
|
|
|
360
1112
|
toString() {
|
|
361
1113
|
return '[' + String(this.ofType) + ']';
|
|
362
1114
|
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Returns the JSON representation used when this object is serialized.
|
|
1117
|
+
* @returns The JSON-serializable representation.
|
|
1118
|
+
* @example
|
|
1119
|
+
* ```ts
|
|
1120
|
+
* import { GraphQLList, GraphQLString } from 'graphql/type';
|
|
1121
|
+
*
|
|
1122
|
+
* const stringList = new GraphQLList(GraphQLString);
|
|
1123
|
+
*
|
|
1124
|
+
* stringList.toJSON(); // => '[String]'
|
|
1125
|
+
* JSON.stringify({ type: stringList }); // => '{"type":"[String]"}'
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
363
1128
|
|
|
364
1129
|
toJSON() {
|
|
365
1130
|
return this.toString();
|
|
@@ -373,9 +1138,8 @@ class GraphQLList {
|
|
|
373
1138
|
* an error is raised if this ever occurs during a request. It is useful for
|
|
374
1139
|
* fields which you can make a strong guarantee on non-nullability, for example
|
|
375
1140
|
* usually the id field of a database row will never be null.
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
1141
|
+
* @typeParam T - The nullable GraphQL type wrapped by this non-null type.
|
|
1142
|
+
* @example
|
|
379
1143
|
* ```ts
|
|
380
1144
|
* const RowType = new GraphQLObjectType({
|
|
381
1145
|
* name: 'Row',
|
|
@@ -384,12 +1148,28 @@ class GraphQLList {
|
|
|
384
1148
|
* })
|
|
385
1149
|
* })
|
|
386
1150
|
* ```
|
|
1151
|
+
*
|
|
387
1152
|
* Note: the enforcement of non-nullability occurs within the executor.
|
|
388
1153
|
*/
|
|
389
1154
|
|
|
390
1155
|
exports.GraphQLList = GraphQLList;
|
|
391
1156
|
|
|
392
1157
|
class GraphQLNonNull {
|
|
1158
|
+
/** The type wrapped by this list or non-null type. */
|
|
1159
|
+
|
|
1160
|
+
/**
|
|
1161
|
+
* Creates a GraphQLNonNull instance.
|
|
1162
|
+
* @param ofType - The type to wrap.
|
|
1163
|
+
* @example
|
|
1164
|
+
* ```ts
|
|
1165
|
+
* import { GraphQLNonNull, GraphQLString } from 'graphql/type';
|
|
1166
|
+
*
|
|
1167
|
+
* const requiredString = new GraphQLNonNull(GraphQLString);
|
|
1168
|
+
*
|
|
1169
|
+
* requiredString.ofType; // => GraphQLString
|
|
1170
|
+
* String(requiredString); // => 'String!'
|
|
1171
|
+
* ```
|
|
1172
|
+
*/
|
|
393
1173
|
constructor(ofType) {
|
|
394
1174
|
isNullableType(ofType) ||
|
|
395
1175
|
(0, _devAssert.devAssert)(
|
|
@@ -400,28 +1180,91 @@ class GraphQLNonNull {
|
|
|
400
1180
|
);
|
|
401
1181
|
this.ofType = ofType;
|
|
402
1182
|
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
1185
|
+
* @returns The built-in string tag for this object.
|
|
1186
|
+
*/
|
|
403
1187
|
|
|
404
1188
|
get [Symbol.toStringTag]() {
|
|
405
1189
|
return 'GraphQLNonNull';
|
|
406
1190
|
}
|
|
1191
|
+
/**
|
|
1192
|
+
* Returns this wrapping type as a GraphQL type-reference string.
|
|
1193
|
+
* @returns The GraphQL type-reference string.
|
|
1194
|
+
* @example
|
|
1195
|
+
* ```ts
|
|
1196
|
+
* import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql/type';
|
|
1197
|
+
*
|
|
1198
|
+
* const requiredString = new GraphQLNonNull(GraphQLString);
|
|
1199
|
+
* const requiredStringList = new GraphQLNonNull(
|
|
1200
|
+
* new GraphQLList(GraphQLString),
|
|
1201
|
+
* );
|
|
1202
|
+
*
|
|
1203
|
+
* requiredString.toString(); // => 'String!'
|
|
1204
|
+
* requiredStringList.toString(); // => '[String]!'
|
|
1205
|
+
* ```
|
|
1206
|
+
*/
|
|
407
1207
|
|
|
408
1208
|
toString() {
|
|
409
1209
|
return String(this.ofType) + '!';
|
|
410
1210
|
}
|
|
1211
|
+
/**
|
|
1212
|
+
* Returns the JSON representation used when this object is serialized.
|
|
1213
|
+
* @returns The JSON-serializable representation.
|
|
1214
|
+
* @example
|
|
1215
|
+
* ```ts
|
|
1216
|
+
* import { GraphQLNonNull, GraphQLString } from 'graphql/type';
|
|
1217
|
+
*
|
|
1218
|
+
* const requiredString = new GraphQLNonNull(GraphQLString);
|
|
1219
|
+
*
|
|
1220
|
+
* requiredString.toJSON(); // => 'String!'
|
|
1221
|
+
* JSON.stringify({ type: requiredString }); // => '{"type":"String!"}'
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
411
1224
|
|
|
412
1225
|
toJSON() {
|
|
413
1226
|
return this.toString();
|
|
414
1227
|
}
|
|
415
1228
|
}
|
|
416
|
-
/**
|
|
417
|
-
* These types wrap and modify other types
|
|
418
|
-
*/
|
|
1229
|
+
/** These types wrap and modify other types */
|
|
419
1230
|
|
|
420
1231
|
exports.GraphQLNonNull = GraphQLNonNull;
|
|
421
1232
|
|
|
1233
|
+
/**
|
|
1234
|
+
* Returns true when the value is a GraphQL list or non-null wrapper type.
|
|
1235
|
+
* @param type - The GraphQL type to inspect.
|
|
1236
|
+
* @returns True when the value is a GraphQL list or non-null wrapper type.
|
|
1237
|
+
* @example
|
|
1238
|
+
* ```ts
|
|
1239
|
+
* import {
|
|
1240
|
+
* GraphQLList,
|
|
1241
|
+
* GraphQLNonNull,
|
|
1242
|
+
* GraphQLString,
|
|
1243
|
+
* isWrappingType,
|
|
1244
|
+
* } from 'graphql/type';
|
|
1245
|
+
*
|
|
1246
|
+
* isWrappingType(new GraphQLList(GraphQLString)); // => true
|
|
1247
|
+
* isWrappingType(new GraphQLNonNull(GraphQLString)); // => true
|
|
1248
|
+
* isWrappingType(GraphQLString); // => false
|
|
1249
|
+
* ```
|
|
1250
|
+
*/
|
|
422
1251
|
function isWrappingType(type) {
|
|
423
1252
|
return isListType(type) || isNonNullType(type);
|
|
424
1253
|
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Returns the value as a GraphQL wrapping type, or throws if it is not one.
|
|
1256
|
+
* @param type - The GraphQL type to inspect.
|
|
1257
|
+
* @returns The value typed as a GraphQL wrapping type.
|
|
1258
|
+
* @example
|
|
1259
|
+
* ```ts
|
|
1260
|
+
* import { GraphQLList, GraphQLString, assertWrappingType } from 'graphql/type';
|
|
1261
|
+
*
|
|
1262
|
+
* const wrappingType = assertWrappingType(new GraphQLList(GraphQLString));
|
|
1263
|
+
*
|
|
1264
|
+
* wrappingType.toString(); // => '[String]'
|
|
1265
|
+
* assertWrappingType(GraphQLString); // throws an error
|
|
1266
|
+
* ```
|
|
1267
|
+
*/
|
|
425
1268
|
|
|
426
1269
|
function assertWrappingType(type) {
|
|
427
1270
|
if (!isWrappingType(type)) {
|
|
@@ -432,13 +1275,42 @@ function assertWrappingType(type) {
|
|
|
432
1275
|
|
|
433
1276
|
return type;
|
|
434
1277
|
}
|
|
1278
|
+
/** These types can all accept null as a value. */
|
|
1279
|
+
|
|
435
1280
|
/**
|
|
436
|
-
*
|
|
1281
|
+
* Returns true when the value is a GraphQL type that can accept null.
|
|
1282
|
+
* @param type - The GraphQL type to inspect.
|
|
1283
|
+
* @returns True when the value is a GraphQL type that can accept null.
|
|
1284
|
+
* @example
|
|
1285
|
+
* ```ts
|
|
1286
|
+
* import { GraphQLNonNull, GraphQLString, isNullableType } from 'graphql/type';
|
|
1287
|
+
*
|
|
1288
|
+
* isNullableType(GraphQLString); // => true
|
|
1289
|
+
* isNullableType(new GraphQLNonNull(GraphQLString)); // => false
|
|
1290
|
+
* isNullableType(null); // => false
|
|
1291
|
+
* ```
|
|
437
1292
|
*/
|
|
438
|
-
|
|
439
1293
|
function isNullableType(type) {
|
|
440
1294
|
return isType(type) && !isNonNullType(type);
|
|
441
1295
|
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Returns the value as a nullable GraphQL type, or throws if it is not one.
|
|
1298
|
+
* @param type - The GraphQL type to inspect.
|
|
1299
|
+
* @returns The value typed as a nullable GraphQL type.
|
|
1300
|
+
* @example
|
|
1301
|
+
* ```ts
|
|
1302
|
+
* import {
|
|
1303
|
+
* GraphQLNonNull,
|
|
1304
|
+
* GraphQLString,
|
|
1305
|
+
* assertNullableType,
|
|
1306
|
+
* } from 'graphql/type';
|
|
1307
|
+
*
|
|
1308
|
+
* const nullableType = assertNullableType(GraphQLString);
|
|
1309
|
+
*
|
|
1310
|
+
* nullableType; // => GraphQLString
|
|
1311
|
+
* assertNullableType(new GraphQLNonNull(GraphQLString)); // throws an error
|
|
1312
|
+
* ```
|
|
1313
|
+
*/
|
|
442
1314
|
|
|
443
1315
|
function assertNullableType(type) {
|
|
444
1316
|
if (!isNullableType(type)) {
|
|
@@ -449,16 +1321,40 @@ function assertNullableType(type) {
|
|
|
449
1321
|
|
|
450
1322
|
return type;
|
|
451
1323
|
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Returns the nullable type.
|
|
1326
|
+
* @param type - The GraphQL type to inspect.
|
|
1327
|
+
* @returns The nullable type after removing one non-null wrapper, if present.
|
|
1328
|
+
* @example
|
|
1329
|
+
* ```ts
|
|
1330
|
+
* import { getNullableType } from 'graphql/type';
|
|
1331
|
+
*
|
|
1332
|
+
* getNullableType(null); // => undefined
|
|
1333
|
+
* getNullableType(undefined); // => undefined
|
|
1334
|
+
* ```
|
|
1335
|
+
*/
|
|
452
1336
|
|
|
1337
|
+
/** @internal */
|
|
453
1338
|
function getNullableType(type) {
|
|
454
1339
|
if (type) {
|
|
455
1340
|
return isNonNullType(type) ? type.ofType : type;
|
|
456
1341
|
}
|
|
457
1342
|
}
|
|
1343
|
+
/** These named types do not include modifiers like List or NonNull. */
|
|
1344
|
+
|
|
458
1345
|
/**
|
|
459
|
-
*
|
|
1346
|
+
* Returns true when the value is a GraphQL named type.
|
|
1347
|
+
* @param type - The GraphQL type to inspect.
|
|
1348
|
+
* @returns True when the value is a GraphQL named type.
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```ts
|
|
1351
|
+
* import { GraphQLList, GraphQLString, isNamedType } from 'graphql/type';
|
|
1352
|
+
*
|
|
1353
|
+
* isNamedType(GraphQLString); // => true
|
|
1354
|
+
* isNamedType(new GraphQLList(GraphQLString)); // => false
|
|
1355
|
+
* isNamedType(null); // => false
|
|
1356
|
+
* ```
|
|
460
1357
|
*/
|
|
461
|
-
|
|
462
1358
|
function isNamedType(type) {
|
|
463
1359
|
return (
|
|
464
1360
|
isScalarType(type) ||
|
|
@@ -469,6 +1365,20 @@ function isNamedType(type) {
|
|
|
469
1365
|
isInputObjectType(type)
|
|
470
1366
|
);
|
|
471
1367
|
}
|
|
1368
|
+
/**
|
|
1369
|
+
* Returns the value as a GraphQL named type, or throws if it is not one.
|
|
1370
|
+
* @param type - The GraphQL type to inspect.
|
|
1371
|
+
* @returns The value typed as a GraphQL named type.
|
|
1372
|
+
* @example
|
|
1373
|
+
* ```ts
|
|
1374
|
+
* import { GraphQLList, GraphQLString, assertNamedType } from 'graphql/type';
|
|
1375
|
+
*
|
|
1376
|
+
* const namedType = assertNamedType(GraphQLString);
|
|
1377
|
+
*
|
|
1378
|
+
* namedType.name; // => 'String'
|
|
1379
|
+
* assertNamedType(new GraphQLList(GraphQLString)); // throws an error
|
|
1380
|
+
* ```
|
|
1381
|
+
*/
|
|
472
1382
|
|
|
473
1383
|
function assertNamedType(type) {
|
|
474
1384
|
if (!isNamedType(type)) {
|
|
@@ -479,7 +1389,20 @@ function assertNamedType(type) {
|
|
|
479
1389
|
|
|
480
1390
|
return type;
|
|
481
1391
|
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Returns the named type.
|
|
1394
|
+
* @param type - The GraphQL type to inspect.
|
|
1395
|
+
* @returns The named type after unwrapping all list and non-null wrappers.
|
|
1396
|
+
* @example
|
|
1397
|
+
* ```ts
|
|
1398
|
+
* import { getNamedType } from 'graphql/type';
|
|
1399
|
+
*
|
|
1400
|
+
* getNamedType(null); // => undefined
|
|
1401
|
+
* getNamedType(undefined); // => undefined
|
|
1402
|
+
* ```
|
|
1403
|
+
*/
|
|
482
1404
|
|
|
1405
|
+
/** @internal */
|
|
483
1406
|
function getNamedType(type) {
|
|
484
1407
|
if (type) {
|
|
485
1408
|
let unwrappedType = type;
|
|
@@ -494,18 +1417,50 @@ function getNamedType(type) {
|
|
|
494
1417
|
/**
|
|
495
1418
|
* Used while defining GraphQL types to allow for circular references in
|
|
496
1419
|
* otherwise immutable type definitions.
|
|
1420
|
+
* @typeParam T - The element type returned by the thunk or array.
|
|
497
1421
|
*/
|
|
498
1422
|
|
|
1423
|
+
/**
|
|
1424
|
+
* Resolves a thunked readonly array.
|
|
1425
|
+
* @param thunk - The thunk or value to resolve.
|
|
1426
|
+
* @returns The resolved readonly array.
|
|
1427
|
+
* @typeParam T - The element type resolved from the thunk or array.
|
|
1428
|
+
* @example
|
|
1429
|
+
* ```ts
|
|
1430
|
+
* import { GraphQLString, resolveReadonlyArrayThunk } from 'graphql/type';
|
|
1431
|
+
*
|
|
1432
|
+
* const lazyFields = resolveReadonlyArrayThunk(() => [GraphQLString]);
|
|
1433
|
+
* const fields = resolveReadonlyArrayThunk([GraphQLString]);
|
|
1434
|
+
*
|
|
1435
|
+
* lazyFields; // => [GraphQLString]
|
|
1436
|
+
* fields; // => [GraphQLString]
|
|
1437
|
+
* ```
|
|
1438
|
+
*/
|
|
499
1439
|
function resolveReadonlyArrayThunk(thunk) {
|
|
500
1440
|
return typeof thunk === 'function' ? thunk() : thunk;
|
|
501
1441
|
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Resolves a thunked object map.
|
|
1444
|
+
* @param thunk - The thunk or value to resolve.
|
|
1445
|
+
* @returns The resolved object map.
|
|
1446
|
+
* @typeParam T - The object-map value type resolved from the thunk or map.
|
|
1447
|
+
* @example
|
|
1448
|
+
* ```ts
|
|
1449
|
+
* import { GraphQLString, resolveObjMapThunk } from 'graphql/type';
|
|
1450
|
+
*
|
|
1451
|
+
* const lazyFields = resolveObjMapThunk(() => ({ name: GraphQLString }));
|
|
1452
|
+
* const fields = resolveObjMapThunk({ name: GraphQLString });
|
|
1453
|
+
*
|
|
1454
|
+
* lazyFields.name; // => GraphQLString
|
|
1455
|
+
* fields.name; // => GraphQLString
|
|
1456
|
+
* ```
|
|
1457
|
+
*/
|
|
502
1458
|
|
|
503
1459
|
function resolveObjMapThunk(thunk) {
|
|
504
1460
|
return typeof thunk === 'function' ? thunk() : thunk;
|
|
505
1461
|
}
|
|
506
1462
|
/**
|
|
507
1463
|
* Custom extensions
|
|
508
|
-
*
|
|
509
1464
|
* @remarks
|
|
510
1465
|
* Use a unique identifier name for your extension, for example the name of
|
|
511
1466
|
* your library or project. Do not use a shortened identifier as this increases
|
|
@@ -516,20 +1471,22 @@ function resolveObjMapThunk(thunk) {
|
|
|
516
1471
|
/**
|
|
517
1472
|
* Scalar Type Definition
|
|
518
1473
|
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
1474
|
+
* Scalar types define the leaf values of a GraphQL response and the input
|
|
1475
|
+
* values accepted by arguments and input object fields. A scalar type has a
|
|
1476
|
+
* name and coercion functions that validate and convert runtime values and
|
|
1477
|
+
* GraphQL literals.
|
|
522
1478
|
*
|
|
523
1479
|
* If a type's serialize function returns `null` or does not return a value
|
|
524
1480
|
* (i.e. it returns `undefined`) then an error will be raised and a `null`
|
|
525
|
-
* value will be returned in the response.
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
1481
|
+
* value will be returned in the response. Prefer validating inputs before
|
|
1482
|
+
* execution so clients receive input diagnostics before result coercion fails.
|
|
1483
|
+
* @typeParam TInternal - The internal runtime representation accepted by this scalar.
|
|
1484
|
+
* @typeParam TExternal - The serialized representation exposed in GraphQL results.
|
|
1485
|
+
* @example
|
|
529
1486
|
* ```ts
|
|
530
1487
|
* const OddType = new GraphQLScalarType({
|
|
531
1488
|
* name: 'Odd',
|
|
532
|
-
* serialize(value) {
|
|
1489
|
+
* serialize: (value) => {
|
|
533
1490
|
* if (!Number.isFinite(value)) {
|
|
534
1491
|
* throw new Error(
|
|
535
1492
|
* `Scalar "Odd" cannot represent "${value}" since it is not a finite number.`,
|
|
@@ -545,6 +1502,77 @@ function resolveObjMapThunk(thunk) {
|
|
|
545
1502
|
* ```
|
|
546
1503
|
*/
|
|
547
1504
|
class GraphQLScalarType {
|
|
1505
|
+
/** The GraphQL name for this schema element. */
|
|
1506
|
+
|
|
1507
|
+
/** Human-readable description for this schema element, if provided. */
|
|
1508
|
+
|
|
1509
|
+
/** URL identifying the behavior specified for this custom scalar. */
|
|
1510
|
+
|
|
1511
|
+
/** Function that converts internal values to externally visible scalar values. */
|
|
1512
|
+
|
|
1513
|
+
/** Function that converts variable input into this scalar's internal value. */
|
|
1514
|
+
|
|
1515
|
+
/** Function that converts AST input literals into this scalar's internal value. */
|
|
1516
|
+
|
|
1517
|
+
/** Extension fields to include in the formatted result. */
|
|
1518
|
+
|
|
1519
|
+
/** AST node from which this schema element was built, if available. */
|
|
1520
|
+
|
|
1521
|
+
/** AST extension nodes applied to this schema element. */
|
|
1522
|
+
|
|
1523
|
+
/**
|
|
1524
|
+
* Creates a GraphQLScalarType instance.
|
|
1525
|
+
* @param config - Configuration describing this object.
|
|
1526
|
+
* @example
|
|
1527
|
+
* ```ts
|
|
1528
|
+
* import { Kind, parse } from 'graphql/language';
|
|
1529
|
+
* import { GraphQLScalarType } from 'graphql/type';
|
|
1530
|
+
*
|
|
1531
|
+
* const document = parse(`
|
|
1532
|
+
* "Odd integer values."
|
|
1533
|
+
* scalar Odd @specifiedBy(url: "https://example.com/odd")
|
|
1534
|
+
*
|
|
1535
|
+
* extend scalar Odd @specifiedBy(url: "https://example.com/odd-v2")
|
|
1536
|
+
* `);
|
|
1537
|
+
*
|
|
1538
|
+
* const Odd = new GraphQLScalarType({
|
|
1539
|
+
* name: 'Odd',
|
|
1540
|
+
* description: 'Odd integer values.',
|
|
1541
|
+
* specifiedByURL: 'https://example.com/odd',
|
|
1542
|
+
* serialize: (value) => {
|
|
1543
|
+
* if (typeof value !== 'number' || value % 2 === 0) {
|
|
1544
|
+
* throw new TypeError('Odd can only serialize odd numbers.');
|
|
1545
|
+
* }
|
|
1546
|
+
* return value;
|
|
1547
|
+
* },
|
|
1548
|
+
* parseValue: (value) => {
|
|
1549
|
+
* if (typeof value !== 'number' || value % 2 === 0) {
|
|
1550
|
+
* throw new TypeError('Odd can only parse odd numbers.');
|
|
1551
|
+
* }
|
|
1552
|
+
* return value;
|
|
1553
|
+
* },
|
|
1554
|
+
* parseLiteral: (ast) => {
|
|
1555
|
+
* if (ast.kind !== Kind.INT) {
|
|
1556
|
+
* throw new TypeError('Odd can only parse integer literals.');
|
|
1557
|
+
* }
|
|
1558
|
+
* const value = Number(ast.value);
|
|
1559
|
+
* if (value % 2 === 0) {
|
|
1560
|
+
* throw new TypeError('Odd can only parse odd integer literals.');
|
|
1561
|
+
* }
|
|
1562
|
+
* return value;
|
|
1563
|
+
* },
|
|
1564
|
+
* extensions: { numeric: true },
|
|
1565
|
+
* astNode: document.definitions[0],
|
|
1566
|
+
* extensionASTNodes: [ document.definitions[1] ],
|
|
1567
|
+
* });
|
|
1568
|
+
*
|
|
1569
|
+
* Odd.description; // => 'Odd integer values.'
|
|
1570
|
+
* Odd.specifiedByURL; // => 'https://example.com/odd'
|
|
1571
|
+
* Odd.serialize(3); // => 3
|
|
1572
|
+
* Odd.parseValue(5); // => 5
|
|
1573
|
+
* Odd.extensions; // => { numeric: true }
|
|
1574
|
+
* ```
|
|
1575
|
+
*/
|
|
548
1576
|
constructor(config) {
|
|
549
1577
|
var _config$parseValue,
|
|
550
1578
|
_config$serialize,
|
|
@@ -603,10 +1631,35 @@ class GraphQLScalarType {
|
|
|
603
1631
|
);
|
|
604
1632
|
}
|
|
605
1633
|
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
1636
|
+
* @returns The built-in string tag for this object.
|
|
1637
|
+
*/
|
|
606
1638
|
|
|
607
1639
|
get [Symbol.toStringTag]() {
|
|
608
1640
|
return 'GraphQLScalarType';
|
|
609
1641
|
}
|
|
1642
|
+
/**
|
|
1643
|
+
* Returns a normalized configuration object for this object.
|
|
1644
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
1645
|
+
* @example
|
|
1646
|
+
* ```ts
|
|
1647
|
+
* import { GraphQLScalarType } from 'graphql/type';
|
|
1648
|
+
*
|
|
1649
|
+
* const Url = new GraphQLScalarType({
|
|
1650
|
+
* name: 'Url',
|
|
1651
|
+
* description: 'An absolute URL string.',
|
|
1652
|
+
* specifiedByURL: 'https://url.spec.whatwg.org/',
|
|
1653
|
+
* });
|
|
1654
|
+
*
|
|
1655
|
+
* const config = Url.toConfig();
|
|
1656
|
+
* const UrlCopy = new GraphQLScalarType(config);
|
|
1657
|
+
*
|
|
1658
|
+
* config.name; // => 'Url'
|
|
1659
|
+
* config.specifiedByURL; // => 'https://url.spec.whatwg.org/'
|
|
1660
|
+
* UrlCopy.name; // => Url.name
|
|
1661
|
+
* ```
|
|
1662
|
+
*/
|
|
610
1663
|
|
|
611
1664
|
toConfig() {
|
|
612
1665
|
return {
|
|
@@ -621,15 +1674,45 @@ class GraphQLScalarType {
|
|
|
621
1674
|
extensionASTNodes: this.extensionASTNodes,
|
|
622
1675
|
};
|
|
623
1676
|
}
|
|
1677
|
+
/**
|
|
1678
|
+
* Returns the schema coordinate identifying this scalar type.
|
|
1679
|
+
* @returns The schema coordinate for this scalar type.
|
|
1680
|
+
* @example
|
|
1681
|
+
* ```ts
|
|
1682
|
+
* import { GraphQLScalarType } from 'graphql/type';
|
|
1683
|
+
*
|
|
1684
|
+
* const DateTime = new GraphQLScalarType({ name: 'DateTime' });
|
|
1685
|
+
*
|
|
1686
|
+
* DateTime.toString(); // => 'DateTime'
|
|
1687
|
+
* String(DateTime); // => 'DateTime'
|
|
1688
|
+
* ```
|
|
1689
|
+
*/
|
|
624
1690
|
|
|
625
1691
|
toString() {
|
|
626
1692
|
return this.name;
|
|
627
1693
|
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Returns the JSON representation used when this object is serialized.
|
|
1696
|
+
* @returns The JSON-serializable representation.
|
|
1697
|
+
* @example
|
|
1698
|
+
* ```ts
|
|
1699
|
+
* import { GraphQLScalarType } from 'graphql/type';
|
|
1700
|
+
*
|
|
1701
|
+
* const DateTime = new GraphQLScalarType({ name: 'DateTime' });
|
|
1702
|
+
*
|
|
1703
|
+
* DateTime.toJSON(); // => 'DateTime'
|
|
1704
|
+
* JSON.stringify({ type: DateTime }); // => '{"type":"DateTime"}'
|
|
1705
|
+
* ```
|
|
1706
|
+
*/
|
|
628
1707
|
|
|
629
1708
|
toJSON() {
|
|
630
1709
|
return this.toString();
|
|
631
1710
|
}
|
|
632
1711
|
}
|
|
1712
|
+
/**
|
|
1713
|
+
* Serializes a runtime value as a scalar output value.
|
|
1714
|
+
* @typeParam TExternal - The serialized representation returned for GraphQL results.
|
|
1715
|
+
*/
|
|
633
1716
|
|
|
634
1717
|
exports.GraphQLScalarType = GraphQLScalarType;
|
|
635
1718
|
|
|
@@ -638,9 +1721,9 @@ exports.GraphQLScalarType = GraphQLScalarType;
|
|
|
638
1721
|
*
|
|
639
1722
|
* Almost all of the GraphQL types you define will be object types. Object types
|
|
640
1723
|
* have a name, but most importantly describe their fields.
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
1724
|
+
* @typeParam TSource - Source object type passed to resolvers.
|
|
1725
|
+
* @typeParam TContext - Context object type passed to resolvers.
|
|
1726
|
+
* @example
|
|
644
1727
|
* ```ts
|
|
645
1728
|
* const AddressType = new GraphQLObjectType({
|
|
646
1729
|
* name: 'Address',
|
|
@@ -649,20 +1732,18 @@ exports.GraphQLScalarType = GraphQLScalarType;
|
|
|
649
1732
|
* number: { type: GraphQLInt },
|
|
650
1733
|
* formatted: {
|
|
651
1734
|
* type: GraphQLString,
|
|
652
|
-
* resolve(obj) {
|
|
1735
|
+
* resolve: (obj) => {
|
|
653
1736
|
* return obj.number + ' ' + obj.street
|
|
654
1737
|
* }
|
|
655
1738
|
* }
|
|
656
1739
|
* }
|
|
657
1740
|
* });
|
|
658
1741
|
* ```
|
|
659
|
-
*
|
|
1742
|
+
* @example
|
|
660
1743
|
* When two types need to refer to each other, or a type needs to refer to
|
|
661
1744
|
* itself in a field, you can use a function expression (aka a closure or a
|
|
662
1745
|
* thunk) to supply the fields lazily.
|
|
663
1746
|
*
|
|
664
|
-
* Example:
|
|
665
|
-
*
|
|
666
1747
|
* ```ts
|
|
667
1748
|
* const PersonType = new GraphQLObjectType({
|
|
668
1749
|
* name: 'Person',
|
|
@@ -674,6 +1755,118 @@ exports.GraphQLScalarType = GraphQLScalarType;
|
|
|
674
1755
|
* ```
|
|
675
1756
|
*/
|
|
676
1757
|
class GraphQLObjectType {
|
|
1758
|
+
/** The GraphQL name for this schema element. */
|
|
1759
|
+
|
|
1760
|
+
/** Human-readable description for this schema element, if provided. */
|
|
1761
|
+
|
|
1762
|
+
/** Predicate used to determine whether a runtime value belongs to this object type. */
|
|
1763
|
+
|
|
1764
|
+
/** Extension fields to include in the formatted result. */
|
|
1765
|
+
|
|
1766
|
+
/** AST node from which this schema element was built, if available. */
|
|
1767
|
+
|
|
1768
|
+
/** AST extension nodes applied to this schema element. */
|
|
1769
|
+
|
|
1770
|
+
/**
|
|
1771
|
+
* Creates a GraphQLObjectType instance.
|
|
1772
|
+
* @param config - Configuration describing this object.
|
|
1773
|
+
* @example
|
|
1774
|
+
* ```ts
|
|
1775
|
+
* // Configure an object type with interfaces, fields, arguments, and metadata.
|
|
1776
|
+
* import { parse } from 'graphql/language';
|
|
1777
|
+
* import {
|
|
1778
|
+
* GraphQLID,
|
|
1779
|
+
* GraphQLInterfaceType,
|
|
1780
|
+
* GraphQLNonNull,
|
|
1781
|
+
* GraphQLObjectType,
|
|
1782
|
+
* GraphQLString,
|
|
1783
|
+
* } from 'graphql/type';
|
|
1784
|
+
*
|
|
1785
|
+
* const document = parse(`
|
|
1786
|
+
* type User implements Node {
|
|
1787
|
+
* id: ID!
|
|
1788
|
+
* name(format: String = "short"): String
|
|
1789
|
+
* }
|
|
1790
|
+
*
|
|
1791
|
+
* extend type User {
|
|
1792
|
+
* displayName: String
|
|
1793
|
+
* }
|
|
1794
|
+
* `);
|
|
1795
|
+
* const definition = document.definitions[0];
|
|
1796
|
+
* const nameField = definition.fields[1];
|
|
1797
|
+
* const formatArg = nameField.arguments[0];
|
|
1798
|
+
*
|
|
1799
|
+
* const Node = new GraphQLInterfaceType({
|
|
1800
|
+
* name: 'Node',
|
|
1801
|
+
* fields: {
|
|
1802
|
+
* id: { type: new GraphQLNonNull(GraphQLID) },
|
|
1803
|
+
* },
|
|
1804
|
+
* });
|
|
1805
|
+
*
|
|
1806
|
+
* const User = new GraphQLObjectType({
|
|
1807
|
+
* name: 'User',
|
|
1808
|
+
* description: 'A registered user.',
|
|
1809
|
+
* interfaces: [Node],
|
|
1810
|
+
* fields: {
|
|
1811
|
+
* id: { type: new GraphQLNonNull(GraphQLID) },
|
|
1812
|
+
* name: {
|
|
1813
|
+
* description: 'The formatted user name.',
|
|
1814
|
+
* type: GraphQLString,
|
|
1815
|
+
* args: {
|
|
1816
|
+
* format: {
|
|
1817
|
+
* description: 'Controls the name format.',
|
|
1818
|
+
* type: GraphQLString,
|
|
1819
|
+
* defaultValue: 'short',
|
|
1820
|
+
* deprecationReason: 'Use locale instead.',
|
|
1821
|
+
* extensions: { public: true },
|
|
1822
|
+
* astNode: formatArg,
|
|
1823
|
+
* },
|
|
1824
|
+
* },
|
|
1825
|
+
* resolve: (user, { format }) => {
|
|
1826
|
+
* return format === 'long' ? user.fullName : user.name;
|
|
1827
|
+
* },
|
|
1828
|
+
* deprecationReason: 'Use displayName.',
|
|
1829
|
+
* extensions: { cacheSeconds: 60 },
|
|
1830
|
+
* astNode: nameField,
|
|
1831
|
+
* },
|
|
1832
|
+
* },
|
|
1833
|
+
* isTypeOf: (value) => {
|
|
1834
|
+
* return typeof value === 'object' && value != null && 'id' in value;
|
|
1835
|
+
* },
|
|
1836
|
+
* extensions: { entity: 'User' },
|
|
1837
|
+
* astNode: definition,
|
|
1838
|
+
* extensionASTNodes: [ document.definitions[1] ],
|
|
1839
|
+
* });
|
|
1840
|
+
*
|
|
1841
|
+
* User.name; // => 'User'
|
|
1842
|
+
* User.getInterfaces(); // => [Node]
|
|
1843
|
+
* Object.keys(User.getFields()); // => ['id', 'name']
|
|
1844
|
+
* User.getFields().name.args[0].defaultValue; // => 'short'
|
|
1845
|
+
* User.extensions; // => { entity: 'User' }
|
|
1846
|
+
* ```
|
|
1847
|
+
* @example
|
|
1848
|
+
* ```ts
|
|
1849
|
+
* // This variant configures a subscription field with subscribe and resolve functions.
|
|
1850
|
+
* import { GraphQLObjectType, GraphQLString } from 'graphql/type';
|
|
1851
|
+
*
|
|
1852
|
+
* const Subscription = new GraphQLObjectType({
|
|
1853
|
+
* name: 'Subscription',
|
|
1854
|
+
* fields: {
|
|
1855
|
+
* greeting: {
|
|
1856
|
+
* type: GraphQLString,
|
|
1857
|
+
* subscribe: async function* () {
|
|
1858
|
+
* yield { greeting: 'Hello!' };
|
|
1859
|
+
* },
|
|
1860
|
+
* resolve: (event) => {
|
|
1861
|
+
* return event.greeting;
|
|
1862
|
+
* },
|
|
1863
|
+
* },
|
|
1864
|
+
* },
|
|
1865
|
+
* });
|
|
1866
|
+
*
|
|
1867
|
+
* typeof Subscription.getFields().greeting.subscribe; // => 'function'
|
|
1868
|
+
* ```
|
|
1869
|
+
*/
|
|
677
1870
|
constructor(config) {
|
|
678
1871
|
var _config$extensionASTN2;
|
|
679
1872
|
|
|
@@ -700,10 +1893,40 @@ class GraphQLObjectType {
|
|
|
700
1893
|
`but got: ${(0, _inspect.inspect)(config.isTypeOf)}.`,
|
|
701
1894
|
);
|
|
702
1895
|
}
|
|
1896
|
+
/**
|
|
1897
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
1898
|
+
* @returns The built-in string tag for this object.
|
|
1899
|
+
*/
|
|
703
1900
|
|
|
704
1901
|
get [Symbol.toStringTag]() {
|
|
705
1902
|
return 'GraphQLObjectType';
|
|
706
1903
|
}
|
|
1904
|
+
/**
|
|
1905
|
+
* Returns the fields defined by this type.
|
|
1906
|
+
* @returns The fields keyed by field name.
|
|
1907
|
+
* @example
|
|
1908
|
+
* ```ts
|
|
1909
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
1910
|
+
* import { assertObjectType } from 'graphql/type';
|
|
1911
|
+
*
|
|
1912
|
+
* const schema = buildSchema(`
|
|
1913
|
+
* type User {
|
|
1914
|
+
* id: ID!
|
|
1915
|
+
* name: String
|
|
1916
|
+
* }
|
|
1917
|
+
*
|
|
1918
|
+
* type Query {
|
|
1919
|
+
* viewer: User
|
|
1920
|
+
* }
|
|
1921
|
+
* `);
|
|
1922
|
+
*
|
|
1923
|
+
* const User = assertObjectType(schema.getType('User'));
|
|
1924
|
+
* const fields = User.getFields();
|
|
1925
|
+
*
|
|
1926
|
+
* Object.keys(fields); // => ['id', 'name']
|
|
1927
|
+
* String(fields.id.type); // => 'ID!'
|
|
1928
|
+
* ```
|
|
1929
|
+
*/
|
|
707
1930
|
|
|
708
1931
|
getFields() {
|
|
709
1932
|
if (typeof this._fields === 'function') {
|
|
@@ -712,6 +1935,33 @@ class GraphQLObjectType {
|
|
|
712
1935
|
|
|
713
1936
|
return this._fields;
|
|
714
1937
|
}
|
|
1938
|
+
/**
|
|
1939
|
+
* Returns the interfaces implemented by this type.
|
|
1940
|
+
* @returns The implemented interfaces.
|
|
1941
|
+
* @example
|
|
1942
|
+
* ```ts
|
|
1943
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
1944
|
+
* import { assertObjectType } from 'graphql/type';
|
|
1945
|
+
*
|
|
1946
|
+
* const schema = buildSchema(`
|
|
1947
|
+
* interface Node {
|
|
1948
|
+
* id: ID!
|
|
1949
|
+
* }
|
|
1950
|
+
*
|
|
1951
|
+
* type User implements Node {
|
|
1952
|
+
* id: ID!
|
|
1953
|
+
* }
|
|
1954
|
+
*
|
|
1955
|
+
* type Query {
|
|
1956
|
+
* viewer: User
|
|
1957
|
+
* }
|
|
1958
|
+
* `);
|
|
1959
|
+
*
|
|
1960
|
+
* const User = assertObjectType(schema.getType('User'));
|
|
1961
|
+
*
|
|
1962
|
+
* User.getInterfaces().map((type) => type.name); // => ['Node']
|
|
1963
|
+
* ```
|
|
1964
|
+
*/
|
|
715
1965
|
|
|
716
1966
|
getInterfaces() {
|
|
717
1967
|
if (typeof this._interfaces === 'function') {
|
|
@@ -720,6 +1970,27 @@ class GraphQLObjectType {
|
|
|
720
1970
|
|
|
721
1971
|
return this._interfaces;
|
|
722
1972
|
}
|
|
1973
|
+
/**
|
|
1974
|
+
* Returns a normalized configuration object for this object.
|
|
1975
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
1976
|
+
* @example
|
|
1977
|
+
* ```ts
|
|
1978
|
+
* import { GraphQLObjectType, GraphQLString } from 'graphql/type';
|
|
1979
|
+
*
|
|
1980
|
+
* const User = new GraphQLObjectType({
|
|
1981
|
+
* name: 'User',
|
|
1982
|
+
* fields: {
|
|
1983
|
+
* name: { type: GraphQLString },
|
|
1984
|
+
* },
|
|
1985
|
+
* });
|
|
1986
|
+
*
|
|
1987
|
+
* const config = User.toConfig();
|
|
1988
|
+
* const UserCopy = new GraphQLObjectType(config);
|
|
1989
|
+
*
|
|
1990
|
+
* config.fields.name.type; // => GraphQLString
|
|
1991
|
+
* UserCopy.getFields().name.type; // => GraphQLString
|
|
1992
|
+
* ```
|
|
1993
|
+
*/
|
|
723
1994
|
|
|
724
1995
|
toConfig() {
|
|
725
1996
|
return {
|
|
@@ -733,10 +2004,49 @@ class GraphQLObjectType {
|
|
|
733
2004
|
extensionASTNodes: this.extensionASTNodes,
|
|
734
2005
|
};
|
|
735
2006
|
}
|
|
2007
|
+
/**
|
|
2008
|
+
* Returns the schema coordinate identifying this object type.
|
|
2009
|
+
* @returns The schema coordinate for this object type.
|
|
2010
|
+
* @example
|
|
2011
|
+
* ```ts
|
|
2012
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2013
|
+
* import { assertObjectType } from 'graphql/type';
|
|
2014
|
+
*
|
|
2015
|
+
* const schema = buildSchema(`
|
|
2016
|
+
* type User {
|
|
2017
|
+
* name: String
|
|
2018
|
+
* }
|
|
2019
|
+
*
|
|
2020
|
+
* type Query {
|
|
2021
|
+
* viewer: User
|
|
2022
|
+
* }
|
|
2023
|
+
* `);
|
|
2024
|
+
*
|
|
2025
|
+
* const User = assertObjectType(schema.getType('User'));
|
|
2026
|
+
*
|
|
2027
|
+
* User.toString(); // => 'User'
|
|
2028
|
+
* ```
|
|
2029
|
+
*/
|
|
736
2030
|
|
|
737
2031
|
toString() {
|
|
738
2032
|
return this.name;
|
|
739
2033
|
}
|
|
2034
|
+
/**
|
|
2035
|
+
* Returns the JSON representation used when this object is serialized.
|
|
2036
|
+
* @returns The JSON-serializable representation.
|
|
2037
|
+
* @example
|
|
2038
|
+
* ```ts
|
|
2039
|
+
* import { GraphQLObjectType, GraphQLString } from 'graphql/type';
|
|
2040
|
+
*
|
|
2041
|
+
* const User = new GraphQLObjectType({
|
|
2042
|
+
* name: 'User',
|
|
2043
|
+
* fields: { name: { type: GraphQLString } },
|
|
2044
|
+
* });
|
|
2045
|
+
*
|
|
2046
|
+
* User.toJSON(); // => 'User'
|
|
2047
|
+
* JSON.stringify({ type: User }); // => '{"type":"User"}'
|
|
2048
|
+
* ```
|
|
2049
|
+
*/
|
|
740
2050
|
|
|
741
2051
|
toJSON() {
|
|
742
2052
|
return this.toString();
|
|
@@ -807,6 +2117,7 @@ function defineFieldMap(config) {
|
|
|
807
2117
|
};
|
|
808
2118
|
});
|
|
809
2119
|
}
|
|
2120
|
+
/** @internal */
|
|
810
2121
|
|
|
811
2122
|
function defineArguments(config) {
|
|
812
2123
|
return Object.entries(config).map(([argName, argConfig]) => ({
|
|
@@ -836,9 +2147,7 @@ function fieldsToFieldsConfig(fields) {
|
|
|
836
2147
|
astNode: field.astNode,
|
|
837
2148
|
}));
|
|
838
2149
|
}
|
|
839
|
-
/**
|
|
840
|
-
* @internal
|
|
841
|
-
*/
|
|
2150
|
+
/** @internal */
|
|
842
2151
|
|
|
843
2152
|
function argsToArgsConfig(args) {
|
|
844
2153
|
return (0, _keyValMap.keyValMap)(
|
|
@@ -854,10 +2163,46 @@ function argsToArgsConfig(args) {
|
|
|
854
2163
|
}),
|
|
855
2164
|
);
|
|
856
2165
|
}
|
|
2166
|
+
/**
|
|
2167
|
+
* Configuration used to construct a GraphQLObjectType.
|
|
2168
|
+
* @typeParam TSource - Source object type passed to resolvers.
|
|
2169
|
+
* @typeParam TContext - Context object type passed to resolvers.
|
|
2170
|
+
*/
|
|
857
2171
|
|
|
2172
|
+
/**
|
|
2173
|
+
* Returns true when the argument is non-null and has no default value.
|
|
2174
|
+
* @param arg - The argument definition to inspect.
|
|
2175
|
+
* @returns True when the argument is non-null and has no default value.
|
|
2176
|
+
* @example
|
|
2177
|
+
* ```ts
|
|
2178
|
+
* import {
|
|
2179
|
+
* GraphQLInt,
|
|
2180
|
+
* GraphQLNonNull,
|
|
2181
|
+
* GraphQLString,
|
|
2182
|
+
* isRequiredArgument,
|
|
2183
|
+
* } from 'graphql/type';
|
|
2184
|
+
*
|
|
2185
|
+
* const requiredArgument = { name: 'id', type: new GraphQLNonNull(GraphQLInt) };
|
|
2186
|
+
* const optionalArgument = { name: 'name', type: GraphQLString };
|
|
2187
|
+
* const argumentWithDefault = {
|
|
2188
|
+
* name: 'limit',
|
|
2189
|
+
* type: new GraphQLNonNull(GraphQLInt),
|
|
2190
|
+
* defaultValue: 10,
|
|
2191
|
+
* };
|
|
2192
|
+
*
|
|
2193
|
+
* isRequiredArgument(requiredArgument); // => true
|
|
2194
|
+
* isRequiredArgument(optionalArgument); // => false
|
|
2195
|
+
* isRequiredArgument(argumentWithDefault); // => false
|
|
2196
|
+
* ```
|
|
2197
|
+
*/
|
|
858
2198
|
function isRequiredArgument(arg) {
|
|
859
2199
|
return isNonNullType(arg.type) && arg.defaultValue === undefined;
|
|
860
2200
|
}
|
|
2201
|
+
/**
|
|
2202
|
+
* A map of field names to resolved field definitions.
|
|
2203
|
+
* @typeParam TSource - Source object type passed to resolvers.
|
|
2204
|
+
* @typeParam TContext - Context object type passed to resolvers.
|
|
2205
|
+
*/
|
|
861
2206
|
|
|
862
2207
|
/**
|
|
863
2208
|
* Interface Type Definition
|
|
@@ -866,9 +2211,7 @@ function isRequiredArgument(arg) {
|
|
|
866
2211
|
* is used to describe what types are possible, what fields are in common across
|
|
867
2212
|
* all types, as well as a function to determine which type is actually used
|
|
868
2213
|
* when the field is resolved.
|
|
869
|
-
*
|
|
870
|
-
* Example:
|
|
871
|
-
*
|
|
2214
|
+
* @example
|
|
872
2215
|
* ```ts
|
|
873
2216
|
* const EntityType = new GraphQLInterfaceType({
|
|
874
2217
|
* name: 'Entity',
|
|
@@ -879,6 +2222,70 @@ function isRequiredArgument(arg) {
|
|
|
879
2222
|
* ```
|
|
880
2223
|
*/
|
|
881
2224
|
class GraphQLInterfaceType {
|
|
2225
|
+
/** The GraphQL name for this schema element. */
|
|
2226
|
+
|
|
2227
|
+
/** Human-readable description for this schema element, if provided. */
|
|
2228
|
+
|
|
2229
|
+
/** Function that resolves the concrete object type for this abstract type. */
|
|
2230
|
+
|
|
2231
|
+
/** Extension fields to include in the formatted result. */
|
|
2232
|
+
|
|
2233
|
+
/** AST node from which this schema element was built, if available. */
|
|
2234
|
+
|
|
2235
|
+
/** AST extension nodes applied to this schema element. */
|
|
2236
|
+
|
|
2237
|
+
/**
|
|
2238
|
+
* Creates a GraphQLInterfaceType instance.
|
|
2239
|
+
* @param config - Configuration describing this object.
|
|
2240
|
+
* @example
|
|
2241
|
+
* ```ts
|
|
2242
|
+
* import { parse } from 'graphql/language';
|
|
2243
|
+
* import { GraphQLID, GraphQLInterfaceType, GraphQLNonNull } from 'graphql/type';
|
|
2244
|
+
*
|
|
2245
|
+
* const document = parse(`
|
|
2246
|
+
* interface Node {
|
|
2247
|
+
* id: ID!
|
|
2248
|
+
* }
|
|
2249
|
+
*
|
|
2250
|
+
* interface Resource implements Node {
|
|
2251
|
+
* id: ID!
|
|
2252
|
+
* }
|
|
2253
|
+
*
|
|
2254
|
+
* extend interface Resource {
|
|
2255
|
+
* url: String
|
|
2256
|
+
* }
|
|
2257
|
+
* `);
|
|
2258
|
+
*
|
|
2259
|
+
* const Node = new GraphQLInterfaceType({
|
|
2260
|
+
* name: 'Node',
|
|
2261
|
+
* fields: {
|
|
2262
|
+
* id: { type: new GraphQLNonNull(GraphQLID) },
|
|
2263
|
+
* },
|
|
2264
|
+
* });
|
|
2265
|
+
*
|
|
2266
|
+
* const Resource = new GraphQLInterfaceType({
|
|
2267
|
+
* name: 'Resource',
|
|
2268
|
+
* description: 'An addressable resource.',
|
|
2269
|
+
* interfaces: [Node],
|
|
2270
|
+
* fields: {
|
|
2271
|
+
* id: { type: new GraphQLNonNull(GraphQLID) },
|
|
2272
|
+
* },
|
|
2273
|
+
* resolveType: (value) => {
|
|
2274
|
+
* return typeof value === 'object' && value != null && 'url' in value
|
|
2275
|
+
* ? 'WebPage'
|
|
2276
|
+
* : null;
|
|
2277
|
+
* },
|
|
2278
|
+
* extensions: { abstract: true },
|
|
2279
|
+
* astNode: document.definitions[1],
|
|
2280
|
+
* extensionASTNodes: [ document.definitions[2] ],
|
|
2281
|
+
* });
|
|
2282
|
+
*
|
|
2283
|
+
* Resource.name; // => 'Resource'
|
|
2284
|
+
* Resource.getInterfaces(); // => [Node]
|
|
2285
|
+
* Object.keys(Resource.getFields()); // => ['id']
|
|
2286
|
+
* Resource.extensions; // => { abstract: true }
|
|
2287
|
+
* ```
|
|
2288
|
+
*/
|
|
882
2289
|
constructor(config) {
|
|
883
2290
|
var _config$extensionASTN3;
|
|
884
2291
|
|
|
@@ -902,10 +2309,43 @@ class GraphQLInterfaceType {
|
|
|
902
2309
|
`but got: ${(0, _inspect.inspect)(config.resolveType)}.`,
|
|
903
2310
|
);
|
|
904
2311
|
}
|
|
2312
|
+
/**
|
|
2313
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
2314
|
+
* @returns The built-in string tag for this object.
|
|
2315
|
+
*/
|
|
905
2316
|
|
|
906
2317
|
get [Symbol.toStringTag]() {
|
|
907
2318
|
return 'GraphQLInterfaceType';
|
|
908
2319
|
}
|
|
2320
|
+
/**
|
|
2321
|
+
* Returns the fields defined by this type.
|
|
2322
|
+
* @returns The fields keyed by field name.
|
|
2323
|
+
* @example
|
|
2324
|
+
* ```ts
|
|
2325
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2326
|
+
* import { assertInterfaceType } from 'graphql/type';
|
|
2327
|
+
*
|
|
2328
|
+
* const schema = buildSchema(`
|
|
2329
|
+
* interface Node {
|
|
2330
|
+
* id: ID!
|
|
2331
|
+
* }
|
|
2332
|
+
*
|
|
2333
|
+
* type User implements Node {
|
|
2334
|
+
* id: ID!
|
|
2335
|
+
* }
|
|
2336
|
+
*
|
|
2337
|
+
* type Query {
|
|
2338
|
+
* node: Node
|
|
2339
|
+
* }
|
|
2340
|
+
* `);
|
|
2341
|
+
*
|
|
2342
|
+
* const Node = assertInterfaceType(schema.getType('Node'));
|
|
2343
|
+
* const fields = Node.getFields();
|
|
2344
|
+
*
|
|
2345
|
+
* Object.keys(fields); // => ['id']
|
|
2346
|
+
* String(fields.id.type); // => 'ID!'
|
|
2347
|
+
* ```
|
|
2348
|
+
*/
|
|
909
2349
|
|
|
910
2350
|
getFields() {
|
|
911
2351
|
if (typeof this._fields === 'function') {
|
|
@@ -914,6 +2354,39 @@ class GraphQLInterfaceType {
|
|
|
914
2354
|
|
|
915
2355
|
return this._fields;
|
|
916
2356
|
}
|
|
2357
|
+
/**
|
|
2358
|
+
* Returns the interfaces implemented by this type.
|
|
2359
|
+
* @returns The implemented interfaces.
|
|
2360
|
+
* @example
|
|
2361
|
+
* ```ts
|
|
2362
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2363
|
+
* import { assertInterfaceType } from 'graphql/type';
|
|
2364
|
+
*
|
|
2365
|
+
* const schema = buildSchema(`
|
|
2366
|
+
* interface Resource {
|
|
2367
|
+
* url: String!
|
|
2368
|
+
* }
|
|
2369
|
+
*
|
|
2370
|
+
* interface Image implements Resource {
|
|
2371
|
+
* url: String!
|
|
2372
|
+
* width: Int
|
|
2373
|
+
* }
|
|
2374
|
+
*
|
|
2375
|
+
* type Photo implements Resource & Image {
|
|
2376
|
+
* url: String!
|
|
2377
|
+
* width: Int
|
|
2378
|
+
* }
|
|
2379
|
+
*
|
|
2380
|
+
* type Query {
|
|
2381
|
+
* image: Image
|
|
2382
|
+
* }
|
|
2383
|
+
* `);
|
|
2384
|
+
*
|
|
2385
|
+
* const Image = assertInterfaceType(schema.getType('Image'));
|
|
2386
|
+
*
|
|
2387
|
+
* Image.getInterfaces().map((type) => type.name); // => ['Resource']
|
|
2388
|
+
* ```
|
|
2389
|
+
*/
|
|
917
2390
|
|
|
918
2391
|
getInterfaces() {
|
|
919
2392
|
if (typeof this._interfaces === 'function') {
|
|
@@ -922,6 +2395,27 @@ class GraphQLInterfaceType {
|
|
|
922
2395
|
|
|
923
2396
|
return this._interfaces;
|
|
924
2397
|
}
|
|
2398
|
+
/**
|
|
2399
|
+
* Returns a normalized configuration object for this object.
|
|
2400
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
2401
|
+
* @example
|
|
2402
|
+
* ```ts
|
|
2403
|
+
* import { GraphQLID, GraphQLInterfaceType, GraphQLNonNull } from 'graphql/type';
|
|
2404
|
+
*
|
|
2405
|
+
* const Node = new GraphQLInterfaceType({
|
|
2406
|
+
* name: 'Node',
|
|
2407
|
+
* fields: {
|
|
2408
|
+
* id: { type: new GraphQLNonNull(GraphQLID) },
|
|
2409
|
+
* },
|
|
2410
|
+
* });
|
|
2411
|
+
*
|
|
2412
|
+
* const config = Node.toConfig();
|
|
2413
|
+
* const NodeCopy = new GraphQLInterfaceType(config);
|
|
2414
|
+
*
|
|
2415
|
+
* String(config.fields.id.type); // => 'ID!'
|
|
2416
|
+
* String(NodeCopy.getFields().id.type); // => 'ID!'
|
|
2417
|
+
* ```
|
|
2418
|
+
*/
|
|
925
2419
|
|
|
926
2420
|
toConfig() {
|
|
927
2421
|
return {
|
|
@@ -935,15 +2429,63 @@ class GraphQLInterfaceType {
|
|
|
935
2429
|
extensionASTNodes: this.extensionASTNodes,
|
|
936
2430
|
};
|
|
937
2431
|
}
|
|
2432
|
+
/**
|
|
2433
|
+
* Returns the schema coordinate identifying this interface type.
|
|
2434
|
+
* @returns The schema coordinate for this interface type.
|
|
2435
|
+
* @example
|
|
2436
|
+
* ```ts
|
|
2437
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2438
|
+
* import { assertInterfaceType } from 'graphql/type';
|
|
2439
|
+
*
|
|
2440
|
+
* const schema = buildSchema(`
|
|
2441
|
+
* interface Node {
|
|
2442
|
+
* id: ID!
|
|
2443
|
+
* }
|
|
2444
|
+
*
|
|
2445
|
+
* type User implements Node {
|
|
2446
|
+
* id: ID!
|
|
2447
|
+
* }
|
|
2448
|
+
*
|
|
2449
|
+
* type Query {
|
|
2450
|
+
* node: Node
|
|
2451
|
+
* }
|
|
2452
|
+
* `);
|
|
2453
|
+
*
|
|
2454
|
+
* const Node = assertInterfaceType(schema.getType('Node'));
|
|
2455
|
+
*
|
|
2456
|
+
* Node.toString(); // => 'Node'
|
|
2457
|
+
* ```
|
|
2458
|
+
*/
|
|
938
2459
|
|
|
939
2460
|
toString() {
|
|
940
2461
|
return this.name;
|
|
941
2462
|
}
|
|
2463
|
+
/**
|
|
2464
|
+
* Returns the JSON representation used when this object is serialized.
|
|
2465
|
+
* @returns The JSON-serializable representation.
|
|
2466
|
+
* @example
|
|
2467
|
+
* ```ts
|
|
2468
|
+
* import { GraphQLInterfaceType, GraphQLString } from 'graphql/type';
|
|
2469
|
+
*
|
|
2470
|
+
* const Named = new GraphQLInterfaceType({
|
|
2471
|
+
* name: 'Named',
|
|
2472
|
+
* fields: { name: { type: GraphQLString } },
|
|
2473
|
+
* });
|
|
2474
|
+
*
|
|
2475
|
+
* Named.toJSON(); // => 'Named'
|
|
2476
|
+
* JSON.stringify({ type: Named }); // => '{"type":"Named"}'
|
|
2477
|
+
* ```
|
|
2478
|
+
*/
|
|
942
2479
|
|
|
943
2480
|
toJSON() {
|
|
944
2481
|
return this.toString();
|
|
945
2482
|
}
|
|
946
2483
|
}
|
|
2484
|
+
/**
|
|
2485
|
+
* Configuration used to construct a GraphQLInterfaceType.
|
|
2486
|
+
* @typeParam TSource - Source object type passed to resolvers.
|
|
2487
|
+
* @typeParam TContext - Context object type passed to resolvers.
|
|
2488
|
+
*/
|
|
947
2489
|
|
|
948
2490
|
exports.GraphQLInterfaceType = GraphQLInterfaceType;
|
|
949
2491
|
|
|
@@ -953,14 +2495,12 @@ exports.GraphQLInterfaceType = GraphQLInterfaceType;
|
|
|
953
2495
|
* When a field can return one of a heterogeneous set of types, a Union type
|
|
954
2496
|
* is used to describe what types are possible as well as providing a function
|
|
955
2497
|
* to determine which type is actually used when the field is resolved.
|
|
956
|
-
*
|
|
957
|
-
* Example:
|
|
958
|
-
*
|
|
2498
|
+
* @example
|
|
959
2499
|
* ```ts
|
|
960
2500
|
* const PetType = new GraphQLUnionType({
|
|
961
2501
|
* name: 'Pet',
|
|
962
|
-
* types: [
|
|
963
|
-
* resolveType(value) {
|
|
2502
|
+
* types: [DogType, CatType],
|
|
2503
|
+
* resolveType: (value) => {
|
|
964
2504
|
* if (value instanceof Dog) {
|
|
965
2505
|
* return DogType;
|
|
966
2506
|
* }
|
|
@@ -972,6 +2512,60 @@ exports.GraphQLInterfaceType = GraphQLInterfaceType;
|
|
|
972
2512
|
* ```
|
|
973
2513
|
*/
|
|
974
2514
|
class GraphQLUnionType {
|
|
2515
|
+
/** The GraphQL name for this schema element. */
|
|
2516
|
+
|
|
2517
|
+
/** Human-readable description for this schema element, if provided. */
|
|
2518
|
+
|
|
2519
|
+
/** Function that resolves the concrete object type for this abstract type. */
|
|
2520
|
+
|
|
2521
|
+
/** Extension fields to include in the formatted result. */
|
|
2522
|
+
|
|
2523
|
+
/** AST node from which this schema element was built, if available. */
|
|
2524
|
+
|
|
2525
|
+
/** AST extension nodes applied to this schema element. */
|
|
2526
|
+
|
|
2527
|
+
/**
|
|
2528
|
+
* Creates a GraphQLUnionType instance.
|
|
2529
|
+
* @param config - Configuration describing this object.
|
|
2530
|
+
* @example
|
|
2531
|
+
* ```ts
|
|
2532
|
+
* import { parse } from 'graphql/language';
|
|
2533
|
+
* import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql/type';
|
|
2534
|
+
*
|
|
2535
|
+
* const document = parse(`
|
|
2536
|
+
* union Media = Photo | Video
|
|
2537
|
+
*
|
|
2538
|
+
* extend union Media = Audio
|
|
2539
|
+
* `);
|
|
2540
|
+
*
|
|
2541
|
+
* const Photo = new GraphQLObjectType({
|
|
2542
|
+
* name: 'Photo',
|
|
2543
|
+
* fields: { url: { type: GraphQLString } },
|
|
2544
|
+
* });
|
|
2545
|
+
* const Video = new GraphQLObjectType({
|
|
2546
|
+
* name: 'Video',
|
|
2547
|
+
* fields: { url: { type: GraphQLString } },
|
|
2548
|
+
* });
|
|
2549
|
+
*
|
|
2550
|
+
* const Media = new GraphQLUnionType({
|
|
2551
|
+
* name: 'Media',
|
|
2552
|
+
* description: 'Media that can appear in a search result.',
|
|
2553
|
+
* types: [Photo, Video],
|
|
2554
|
+
* resolveType: (value) => {
|
|
2555
|
+
* return typeof value === 'object' && value != null && 'duration' in value
|
|
2556
|
+
* ? 'Video'
|
|
2557
|
+
* : 'Photo';
|
|
2558
|
+
* },
|
|
2559
|
+
* extensions: { searchable: true },
|
|
2560
|
+
* astNode: document.definitions[0],
|
|
2561
|
+
* extensionASTNodes: [ document.definitions[1] ],
|
|
2562
|
+
* });
|
|
2563
|
+
*
|
|
2564
|
+
* Media.description; // => 'Media that can appear in a search result.'
|
|
2565
|
+
* Media.getTypes().map((type) => type.name); // => ['Photo', 'Video']
|
|
2566
|
+
* Media.extensions; // => { searchable: true }
|
|
2567
|
+
* ```
|
|
2568
|
+
*/
|
|
975
2569
|
constructor(config) {
|
|
976
2570
|
var _config$extensionASTN4;
|
|
977
2571
|
|
|
@@ -994,10 +2588,43 @@ class GraphQLUnionType {
|
|
|
994
2588
|
`but got: ${(0, _inspect.inspect)(config.resolveType)}.`,
|
|
995
2589
|
);
|
|
996
2590
|
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
2593
|
+
* @returns The built-in string tag for this object.
|
|
2594
|
+
*/
|
|
997
2595
|
|
|
998
2596
|
get [Symbol.toStringTag]() {
|
|
999
2597
|
return 'GraphQLUnionType';
|
|
1000
2598
|
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Returns the object types included in this union.
|
|
2601
|
+
* @returns The union member object types.
|
|
2602
|
+
* @example
|
|
2603
|
+
* ```ts
|
|
2604
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2605
|
+
* import { assertUnionType } from 'graphql/type';
|
|
2606
|
+
*
|
|
2607
|
+
* const schema = buildSchema(`
|
|
2608
|
+
* type Photo {
|
|
2609
|
+
* url: String!
|
|
2610
|
+
* }
|
|
2611
|
+
*
|
|
2612
|
+
* type Video {
|
|
2613
|
+
* url: String!
|
|
2614
|
+
* }
|
|
2615
|
+
*
|
|
2616
|
+
* union Media = Photo | Video
|
|
2617
|
+
*
|
|
2618
|
+
* type Query {
|
|
2619
|
+
* media: [Media]
|
|
2620
|
+
* }
|
|
2621
|
+
* `);
|
|
2622
|
+
*
|
|
2623
|
+
* const Media = assertUnionType(schema.getType('Media'));
|
|
2624
|
+
*
|
|
2625
|
+
* Media.getTypes().map((type) => type.name); // => ['Photo', 'Video']
|
|
2626
|
+
* ```
|
|
2627
|
+
*/
|
|
1001
2628
|
|
|
1002
2629
|
getTypes() {
|
|
1003
2630
|
if (typeof this._types === 'function') {
|
|
@@ -1006,6 +2633,32 @@ class GraphQLUnionType {
|
|
|
1006
2633
|
|
|
1007
2634
|
return this._types;
|
|
1008
2635
|
}
|
|
2636
|
+
/**
|
|
2637
|
+
* Returns a normalized configuration object for this object.
|
|
2638
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
2639
|
+
* @example
|
|
2640
|
+
* ```ts
|
|
2641
|
+
* import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql/type';
|
|
2642
|
+
*
|
|
2643
|
+
* const Photo = new GraphQLObjectType({
|
|
2644
|
+
* name: 'Photo',
|
|
2645
|
+
* fields: { url: { type: GraphQLString } },
|
|
2646
|
+
* });
|
|
2647
|
+
* const Video = new GraphQLObjectType({
|
|
2648
|
+
* name: 'Video',
|
|
2649
|
+
* fields: { url: { type: GraphQLString } },
|
|
2650
|
+
* });
|
|
2651
|
+
* const Media = new GraphQLUnionType({
|
|
2652
|
+
* name: 'Media',
|
|
2653
|
+
* types: [Photo, Video],
|
|
2654
|
+
* });
|
|
2655
|
+
*
|
|
2656
|
+
* const config = Media.toConfig();
|
|
2657
|
+
* const MediaCopy = new GraphQLUnionType(config);
|
|
2658
|
+
*
|
|
2659
|
+
* MediaCopy.getTypes().map((type) => type.name); // => ['Photo', 'Video']
|
|
2660
|
+
* ```
|
|
2661
|
+
*/
|
|
1009
2662
|
|
|
1010
2663
|
toConfig() {
|
|
1011
2664
|
return {
|
|
@@ -1018,10 +2671,55 @@ class GraphQLUnionType {
|
|
|
1018
2671
|
extensionASTNodes: this.extensionASTNodes,
|
|
1019
2672
|
};
|
|
1020
2673
|
}
|
|
2674
|
+
/**
|
|
2675
|
+
* Returns the schema coordinate identifying this union type.
|
|
2676
|
+
* @returns The schema coordinate for this union type.
|
|
2677
|
+
* @example
|
|
2678
|
+
* ```ts
|
|
2679
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2680
|
+
* import { assertUnionType } from 'graphql/type';
|
|
2681
|
+
*
|
|
2682
|
+
* const schema = buildSchema(`
|
|
2683
|
+
* type Photo {
|
|
2684
|
+
* url: String!
|
|
2685
|
+
* }
|
|
2686
|
+
*
|
|
2687
|
+
* union SearchResult = Photo
|
|
2688
|
+
*
|
|
2689
|
+
* type Query {
|
|
2690
|
+
* search: [SearchResult]
|
|
2691
|
+
* }
|
|
2692
|
+
* `);
|
|
2693
|
+
*
|
|
2694
|
+
* const SearchResult = assertUnionType(schema.getType('SearchResult'));
|
|
2695
|
+
*
|
|
2696
|
+
* SearchResult.toString(); // => 'SearchResult'
|
|
2697
|
+
* ```
|
|
2698
|
+
*/
|
|
1021
2699
|
|
|
1022
2700
|
toString() {
|
|
1023
2701
|
return this.name;
|
|
1024
2702
|
}
|
|
2703
|
+
/**
|
|
2704
|
+
* Returns the JSON representation used when this object is serialized.
|
|
2705
|
+
* @returns The JSON-serializable representation.
|
|
2706
|
+
* @example
|
|
2707
|
+
* ```ts
|
|
2708
|
+
* import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql/type';
|
|
2709
|
+
*
|
|
2710
|
+
* const Photo = new GraphQLObjectType({
|
|
2711
|
+
* name: 'Photo',
|
|
2712
|
+
* fields: { url: { type: GraphQLString } },
|
|
2713
|
+
* });
|
|
2714
|
+
* const SearchResult = new GraphQLUnionType({
|
|
2715
|
+
* name: 'SearchResult',
|
|
2716
|
+
* types: [Photo],
|
|
2717
|
+
* });
|
|
2718
|
+
*
|
|
2719
|
+
* SearchResult.toJSON(); // => 'SearchResult'
|
|
2720
|
+
* JSON.stringify({ type: SearchResult }); // => '{"type":"SearchResult"}'
|
|
2721
|
+
* ```
|
|
2722
|
+
*/
|
|
1025
2723
|
|
|
1026
2724
|
toJSON() {
|
|
1027
2725
|
return this.toString();
|
|
@@ -1039,25 +2737,32 @@ function defineTypes(config) {
|
|
|
1039
2737
|
);
|
|
1040
2738
|
return types;
|
|
1041
2739
|
}
|
|
2740
|
+
/**
|
|
2741
|
+
* Configuration used to construct a GraphQLUnionType.
|
|
2742
|
+
* @typeParam TSource - Source object type passed to resolvers.
|
|
2743
|
+
* @typeParam TContext - Context object type passed to resolvers.
|
|
2744
|
+
*/
|
|
1042
2745
|
|
|
1043
2746
|
/**
|
|
1044
2747
|
* Enum Type Definition
|
|
1045
2748
|
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
* Example:
|
|
1051
|
-
*
|
|
2749
|
+
* Enum types define leaf values whose serialized form is one of a fixed set
|
|
2750
|
+
* of GraphQL enum names. Internally, enum values can map to any runtime value,
|
|
2751
|
+
* often integers.
|
|
2752
|
+
* @example
|
|
1052
2753
|
* ```ts
|
|
2754
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
2755
|
+
*
|
|
1053
2756
|
* const RGBType = new GraphQLEnumType({
|
|
1054
2757
|
* name: 'RGB',
|
|
1055
2758
|
* values: {
|
|
1056
2759
|
* RED: { value: 0 },
|
|
1057
2760
|
* GREEN: { value: 1 },
|
|
1058
|
-
* BLUE: { value: 2 }
|
|
1059
|
-
* }
|
|
2761
|
+
* BLUE: { value: 2 },
|
|
2762
|
+
* },
|
|
1060
2763
|
* });
|
|
2764
|
+
*
|
|
2765
|
+
* RGBType.getValue('GREEN')?.value; // => 1
|
|
1061
2766
|
* ```
|
|
1062
2767
|
*
|
|
1063
2768
|
* Note: If a value is not provided in a definition, the name of the enum value
|
|
@@ -1065,6 +2770,66 @@ function defineTypes(config) {
|
|
|
1065
2770
|
*/
|
|
1066
2771
|
class GraphQLEnumType {
|
|
1067
2772
|
/* <T> */
|
|
2773
|
+
/** The GraphQL name for this schema element. */
|
|
2774
|
+
|
|
2775
|
+
/** Human-readable description for this schema element, if provided. */
|
|
2776
|
+
|
|
2777
|
+
/** Extension fields to include in the formatted result. */
|
|
2778
|
+
|
|
2779
|
+
/** AST node from which this schema element was built, if available. */
|
|
2780
|
+
|
|
2781
|
+
/** AST extension nodes applied to this schema element. */
|
|
2782
|
+
|
|
2783
|
+
/**
|
|
2784
|
+
* Creates a GraphQLEnumType instance.
|
|
2785
|
+
* @param config - Configuration describing this object.
|
|
2786
|
+
* @example
|
|
2787
|
+
* ```ts
|
|
2788
|
+
* import { parse } from 'graphql/language';
|
|
2789
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
2790
|
+
*
|
|
2791
|
+
* const document = parse(`
|
|
2792
|
+
* enum Episode {
|
|
2793
|
+
* NEW_HOPE
|
|
2794
|
+
* EMPIRE
|
|
2795
|
+
* JEDI
|
|
2796
|
+
* }
|
|
2797
|
+
*
|
|
2798
|
+
* extend enum Episode {
|
|
2799
|
+
* FORCE_AWAKENS
|
|
2800
|
+
* }
|
|
2801
|
+
* `);
|
|
2802
|
+
* const definition = document.definitions[0];
|
|
2803
|
+
*
|
|
2804
|
+
* const Episode = new GraphQLEnumType({
|
|
2805
|
+
* name: 'Episode',
|
|
2806
|
+
* description: 'A Star Wars film episode.',
|
|
2807
|
+
* values: {
|
|
2808
|
+
* NEW_HOPE: {
|
|
2809
|
+
* value: 4,
|
|
2810
|
+
* description: 'Released in 1977.',
|
|
2811
|
+
* extensions: { trilogy: 'original' },
|
|
2812
|
+
* astNode: definition.values[0],
|
|
2813
|
+
* },
|
|
2814
|
+
* EMPIRE: { value: 5, astNode: definition.values[1] },
|
|
2815
|
+
* JEDI: {
|
|
2816
|
+
* value: 6,
|
|
2817
|
+
* deprecationReason: 'Use RETURN_OF_THE_JEDI.',
|
|
2818
|
+
* astNode: definition.values[2],
|
|
2819
|
+
* },
|
|
2820
|
+
* },
|
|
2821
|
+
* extensions: { catalog: 'films' },
|
|
2822
|
+
* astNode: definition,
|
|
2823
|
+
* extensionASTNodes: [ document.definitions[1] ],
|
|
2824
|
+
* });
|
|
2825
|
+
*
|
|
2826
|
+
* Episode.description; // => 'A Star Wars film episode.'
|
|
2827
|
+
* Episode.serialize(5); // => 'EMPIRE'
|
|
2828
|
+
* Episode.parseValue('JEDI'); // => 6
|
|
2829
|
+
* Episode.getValue('JEDI').deprecationReason; // => 'Use RETURN_OF_THE_JEDI.'
|
|
2830
|
+
* Episode.extensions; // => { catalog: 'films' }
|
|
2831
|
+
* ```
|
|
2832
|
+
*/
|
|
1068
2833
|
constructor(config) {
|
|
1069
2834
|
var _config$extensionASTN5;
|
|
1070
2835
|
|
|
@@ -1084,10 +2849,39 @@ class GraphQLEnumType {
|
|
|
1084
2849
|
this._valueLookup = null;
|
|
1085
2850
|
this._nameLookup = null;
|
|
1086
2851
|
}
|
|
2852
|
+
/**
|
|
2853
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
2854
|
+
* @returns The built-in string tag for this object.
|
|
2855
|
+
*/
|
|
1087
2856
|
|
|
1088
2857
|
get [Symbol.toStringTag]() {
|
|
1089
2858
|
return 'GraphQLEnumType';
|
|
1090
2859
|
}
|
|
2860
|
+
/**
|
|
2861
|
+
* Returns the values defined by this enum type.
|
|
2862
|
+
* @returns Enum value definitions in schema order.
|
|
2863
|
+
* @example
|
|
2864
|
+
* ```ts
|
|
2865
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2866
|
+
* import { assertEnumType } from 'graphql/type';
|
|
2867
|
+
*
|
|
2868
|
+
* const schema = buildSchema(`
|
|
2869
|
+
* enum Episode {
|
|
2870
|
+
* NEW_HOPE
|
|
2871
|
+
* EMPIRE
|
|
2872
|
+
* JEDI
|
|
2873
|
+
* }
|
|
2874
|
+
*
|
|
2875
|
+
* type Query {
|
|
2876
|
+
* episode: Episode
|
|
2877
|
+
* }
|
|
2878
|
+
* `);
|
|
2879
|
+
*
|
|
2880
|
+
* const Episode = assertEnumType(schema.getType('Episode'));
|
|
2881
|
+
*
|
|
2882
|
+
* Episode.getValues().map((value) => value.name); // => ['NEW_HOPE', 'EMPIRE', 'JEDI']
|
|
2883
|
+
* ```
|
|
2884
|
+
*/
|
|
1091
2885
|
|
|
1092
2886
|
getValues() {
|
|
1093
2887
|
if (typeof this._values === 'function') {
|
|
@@ -1096,6 +2890,32 @@ class GraphQLEnumType {
|
|
|
1096
2890
|
|
|
1097
2891
|
return this._values;
|
|
1098
2892
|
}
|
|
2893
|
+
/**
|
|
2894
|
+
* Returns the enum value definition for a value name.
|
|
2895
|
+
* @param name - The GraphQL name to look up.
|
|
2896
|
+
* @returns The matching enum value definition, if it exists.
|
|
2897
|
+
* @example
|
|
2898
|
+
* ```ts
|
|
2899
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
2900
|
+
* import { assertEnumType } from 'graphql/type';
|
|
2901
|
+
*
|
|
2902
|
+
* const schema = buildSchema(`
|
|
2903
|
+
* enum Episode {
|
|
2904
|
+
* NEW_HOPE
|
|
2905
|
+
* EMPIRE
|
|
2906
|
+
* }
|
|
2907
|
+
*
|
|
2908
|
+
* type Query {
|
|
2909
|
+
* episode: Episode
|
|
2910
|
+
* }
|
|
2911
|
+
* `);
|
|
2912
|
+
*
|
|
2913
|
+
* const Episode = assertEnumType(schema.getType('Episode'));
|
|
2914
|
+
*
|
|
2915
|
+
* Episode.getValue('EMPIRE')?.name; // => 'EMPIRE'
|
|
2916
|
+
* Episode.getValue('JEDI'); // => undefined
|
|
2917
|
+
* ```
|
|
2918
|
+
*/
|
|
1099
2919
|
|
|
1100
2920
|
getValue(name) {
|
|
1101
2921
|
if (this._nameLookup === null) {
|
|
@@ -1107,6 +2927,27 @@ class GraphQLEnumType {
|
|
|
1107
2927
|
|
|
1108
2928
|
return this._nameLookup[name];
|
|
1109
2929
|
}
|
|
2930
|
+
/**
|
|
2931
|
+
* Serializes a runtime enum value as a GraphQL enum name.
|
|
2932
|
+
* @param outputValue - Runtime enum value to serialize.
|
|
2933
|
+
* @returns The GraphQL enum name for the runtime value.
|
|
2934
|
+
* @example
|
|
2935
|
+
* ```ts
|
|
2936
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
2937
|
+
*
|
|
2938
|
+
* const RGB = new GraphQLEnumType({
|
|
2939
|
+
* name: 'RGB',
|
|
2940
|
+
* values: {
|
|
2941
|
+
* RED: { value: 0 },
|
|
2942
|
+
* GREEN: { value: 1 },
|
|
2943
|
+
* BLUE: { value: 2 },
|
|
2944
|
+
* },
|
|
2945
|
+
* });
|
|
2946
|
+
*
|
|
2947
|
+
* RGB.serialize(1); // => 'GREEN'
|
|
2948
|
+
* RGB.serialize(3); // throws an error
|
|
2949
|
+
* ```
|
|
2950
|
+
*/
|
|
1110
2951
|
|
|
1111
2952
|
serialize(outputValue) {
|
|
1112
2953
|
if (this._valueLookup === null) {
|
|
@@ -1127,6 +2968,28 @@ class GraphQLEnumType {
|
|
|
1127
2968
|
|
|
1128
2969
|
return enumValue.name;
|
|
1129
2970
|
}
|
|
2971
|
+
/**
|
|
2972
|
+
* Parses a GraphQL enum name from variable input.
|
|
2973
|
+
* @param inputValue - Runtime input value to parse.
|
|
2974
|
+
* @returns The internal enum value represented by the input name.
|
|
2975
|
+
* @example
|
|
2976
|
+
* ```ts
|
|
2977
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
2978
|
+
*
|
|
2979
|
+
* const RGB = new GraphQLEnumType({
|
|
2980
|
+
* name: 'RGB',
|
|
2981
|
+
* values: {
|
|
2982
|
+
* RED: { value: 0 },
|
|
2983
|
+
* GREEN: { value: 1 },
|
|
2984
|
+
* BLUE: { value: 2 },
|
|
2985
|
+
* },
|
|
2986
|
+
* });
|
|
2987
|
+
*
|
|
2988
|
+
* RGB.parseValue('BLUE'); // => 2
|
|
2989
|
+
* RGB.parseValue('PURPLE'); // throws an error
|
|
2990
|
+
* RGB.parseValue(2); // throws an error
|
|
2991
|
+
* ```
|
|
2992
|
+
*/
|
|
1130
2993
|
|
|
1131
2994
|
parseValue(inputValue) /* T */
|
|
1132
2995
|
{
|
|
@@ -1149,6 +3012,29 @@ class GraphQLEnumType {
|
|
|
1149
3012
|
|
|
1150
3013
|
return enumValue.value;
|
|
1151
3014
|
}
|
|
3015
|
+
/**
|
|
3016
|
+
* Parses a GraphQL enum name from an AST value literal.
|
|
3017
|
+
* @param valueNode - AST value literal to parse.
|
|
3018
|
+
* @param _variables - Runtime variable values; ignored because enum literals cannot contain variables.
|
|
3019
|
+
* @returns The internal enum value represented by the literal.
|
|
3020
|
+
* @example
|
|
3021
|
+
* ```ts
|
|
3022
|
+
* import { parseValue } from 'graphql/language';
|
|
3023
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
3024
|
+
*
|
|
3025
|
+
* const RGB = new GraphQLEnumType({
|
|
3026
|
+
* name: 'RGB',
|
|
3027
|
+
* values: {
|
|
3028
|
+
* RED: { value: 0 },
|
|
3029
|
+
* GREEN: { value: 1 },
|
|
3030
|
+
* BLUE: { value: 2 },
|
|
3031
|
+
* },
|
|
3032
|
+
* });
|
|
3033
|
+
*
|
|
3034
|
+
* RGB.parseLiteral(parseValue('RED')); // => 0
|
|
3035
|
+
* RGB.parseLiteral(parseValue('"RED"')); // throws an error
|
|
3036
|
+
* ```
|
|
3037
|
+
*/
|
|
1152
3038
|
|
|
1153
3039
|
parseLiteral(valueNode, _variables) /* T */
|
|
1154
3040
|
{
|
|
@@ -1179,6 +3065,29 @@ class GraphQLEnumType {
|
|
|
1179
3065
|
|
|
1180
3066
|
return enumValue.value;
|
|
1181
3067
|
}
|
|
3068
|
+
/**
|
|
3069
|
+
* Returns a normalized configuration object for this object.
|
|
3070
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
3071
|
+
* @example
|
|
3072
|
+
* ```ts
|
|
3073
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
3074
|
+
*
|
|
3075
|
+
* const RGB = new GraphQLEnumType({
|
|
3076
|
+
* name: 'RGB',
|
|
3077
|
+
* values: {
|
|
3078
|
+
* RED: { value: 0 },
|
|
3079
|
+
* GREEN: { value: 1 },
|
|
3080
|
+
* BLUE: { value: 2 },
|
|
3081
|
+
* },
|
|
3082
|
+
* });
|
|
3083
|
+
*
|
|
3084
|
+
* const config = RGB.toConfig();
|
|
3085
|
+
* const RGBCopy = new GraphQLEnumType(config);
|
|
3086
|
+
*
|
|
3087
|
+
* config.values.GREEN.value; // => 1
|
|
3088
|
+
* RGBCopy.serialize(2); // => 'BLUE'
|
|
3089
|
+
* ```
|
|
3090
|
+
*/
|
|
1182
3091
|
|
|
1183
3092
|
toConfig() {
|
|
1184
3093
|
const values = (0, _keyValMap.keyValMap)(
|
|
@@ -1201,10 +3110,51 @@ class GraphQLEnumType {
|
|
|
1201
3110
|
extensionASTNodes: this.extensionASTNodes,
|
|
1202
3111
|
};
|
|
1203
3112
|
}
|
|
3113
|
+
/**
|
|
3114
|
+
* Returns the schema coordinate identifying this enum type.
|
|
3115
|
+
* @returns The schema coordinate for this enum type.
|
|
3116
|
+
* @example
|
|
3117
|
+
* ```ts
|
|
3118
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
3119
|
+
* import { assertEnumType } from 'graphql/type';
|
|
3120
|
+
*
|
|
3121
|
+
* const schema = buildSchema(`
|
|
3122
|
+
* enum Episode {
|
|
3123
|
+
* NEW_HOPE
|
|
3124
|
+
* }
|
|
3125
|
+
*
|
|
3126
|
+
* type Query {
|
|
3127
|
+
* episode: Episode
|
|
3128
|
+
* }
|
|
3129
|
+
* `);
|
|
3130
|
+
*
|
|
3131
|
+
* const Episode = assertEnumType(schema.getType('Episode'));
|
|
3132
|
+
*
|
|
3133
|
+
* Episode.toString(); // => 'Episode'
|
|
3134
|
+
* ```
|
|
3135
|
+
*/
|
|
1204
3136
|
|
|
1205
3137
|
toString() {
|
|
1206
3138
|
return this.name;
|
|
1207
3139
|
}
|
|
3140
|
+
/**
|
|
3141
|
+
* Returns the JSON representation used when this object is serialized.
|
|
3142
|
+
* @returns The JSON-serializable representation.
|
|
3143
|
+
* @example
|
|
3144
|
+
* ```ts
|
|
3145
|
+
* import { GraphQLEnumType } from 'graphql/type';
|
|
3146
|
+
*
|
|
3147
|
+
* const Episode = new GraphQLEnumType({
|
|
3148
|
+
* name: 'Episode',
|
|
3149
|
+
* values: {
|
|
3150
|
+
* NEW_HOPE: {},
|
|
3151
|
+
* },
|
|
3152
|
+
* });
|
|
3153
|
+
*
|
|
3154
|
+
* Episode.toJSON(); // => 'Episode'
|
|
3155
|
+
* JSON.stringify({ type: Episode }); // => '{"type":"Episode"}'
|
|
3156
|
+
* ```
|
|
3157
|
+
*/
|
|
1208
3158
|
|
|
1209
3159
|
toJSON() {
|
|
1210
3160
|
return this.toString();
|
|
@@ -1247,6 +3197,7 @@ function defineEnumValues(typeName, valueMap) {
|
|
|
1247
3197
|
};
|
|
1248
3198
|
});
|
|
1249
3199
|
}
|
|
3200
|
+
/** Configuration used to construct a GraphQLEnumType. */
|
|
1250
3201
|
|
|
1251
3202
|
/**
|
|
1252
3203
|
* Input Object Type Definition
|
|
@@ -1255,9 +3206,7 @@ function defineEnumValues(typeName, valueMap) {
|
|
|
1255
3206
|
* supplied to a field argument.
|
|
1256
3207
|
*
|
|
1257
3208
|
* Using `NonNull` will ensure that a value must be provided by the query
|
|
1258
|
-
*
|
|
1259
|
-
* Example:
|
|
1260
|
-
*
|
|
3209
|
+
* @example
|
|
1261
3210
|
* ```ts
|
|
1262
3211
|
* const GeoPoint = new GraphQLInputObjectType({
|
|
1263
3212
|
* name: 'GeoPoint',
|
|
@@ -1270,6 +3219,86 @@ function defineEnumValues(typeName, valueMap) {
|
|
|
1270
3219
|
* ```
|
|
1271
3220
|
*/
|
|
1272
3221
|
class GraphQLInputObjectType {
|
|
3222
|
+
/** The GraphQL name for this schema element. */
|
|
3223
|
+
|
|
3224
|
+
/** Human-readable description for this schema element, if provided. */
|
|
3225
|
+
|
|
3226
|
+
/** Extension fields to include in the formatted result. */
|
|
3227
|
+
|
|
3228
|
+
/** AST node from which this schema element was built, if available. */
|
|
3229
|
+
|
|
3230
|
+
/** AST extension nodes applied to this schema element. */
|
|
3231
|
+
|
|
3232
|
+
/** Whether this input object uses the experimental OneOf input object semantics. */
|
|
3233
|
+
|
|
3234
|
+
/**
|
|
3235
|
+
* Creates a GraphQLInputObjectType instance.
|
|
3236
|
+
* @param config - Configuration describing this object.
|
|
3237
|
+
* @example
|
|
3238
|
+
* ```ts
|
|
3239
|
+
* import { parse } from 'graphql/language';
|
|
3240
|
+
* import {
|
|
3241
|
+
* GraphQLID,
|
|
3242
|
+
* GraphQLInputObjectType,
|
|
3243
|
+
* GraphQLInt,
|
|
3244
|
+
* GraphQLNonNull,
|
|
3245
|
+
* GraphQLString,
|
|
3246
|
+
* } from 'graphql/type';
|
|
3247
|
+
*
|
|
3248
|
+
* const document = parse(`
|
|
3249
|
+
* input ReviewInput {
|
|
3250
|
+
* stars: Int!
|
|
3251
|
+
* commentary: String
|
|
3252
|
+
* }
|
|
3253
|
+
*
|
|
3254
|
+
* extend input ReviewInput {
|
|
3255
|
+
* body: String
|
|
3256
|
+
* }
|
|
3257
|
+
* `);
|
|
3258
|
+
* const definition = document.definitions[0];
|
|
3259
|
+
*
|
|
3260
|
+
* const ReviewInput = new GraphQLInputObjectType({
|
|
3261
|
+
* name: 'ReviewInput',
|
|
3262
|
+
* description: 'Input collected when reviewing a product.',
|
|
3263
|
+
* fields: {
|
|
3264
|
+
* stars: {
|
|
3265
|
+
* description: 'Star rating from one to five.',
|
|
3266
|
+
* type: new GraphQLNonNull(GraphQLInt),
|
|
3267
|
+
* extensions: { min: 1, max: 5 },
|
|
3268
|
+
* astNode: definition.fields[0],
|
|
3269
|
+
* },
|
|
3270
|
+
* commentary: {
|
|
3271
|
+
* type: GraphQLString,
|
|
3272
|
+
* defaultValue: '',
|
|
3273
|
+
* deprecationReason: 'Use body.',
|
|
3274
|
+
* astNode: definition.fields[1],
|
|
3275
|
+
* },
|
|
3276
|
+
* },
|
|
3277
|
+
* extensions: { form: 'review' },
|
|
3278
|
+
* astNode: definition,
|
|
3279
|
+
* extensionASTNodes: [ document.definitions[1] ],
|
|
3280
|
+
* isOneOf: false,
|
|
3281
|
+
* });
|
|
3282
|
+
* const SearchBy = new GraphQLInputObjectType({
|
|
3283
|
+
* name: 'SearchBy',
|
|
3284
|
+
* fields: {
|
|
3285
|
+
* id: { type: GraphQLID },
|
|
3286
|
+
* slug: { type: GraphQLString },
|
|
3287
|
+
* },
|
|
3288
|
+
* isOneOf: true,
|
|
3289
|
+
* });
|
|
3290
|
+
*
|
|
3291
|
+
* const fields = ReviewInput.getFields();
|
|
3292
|
+
*
|
|
3293
|
+
* ReviewInput.description; // => 'Input collected when reviewing a product.'
|
|
3294
|
+
* String(fields.stars.type); // => 'Int!'
|
|
3295
|
+
* fields.stars.extensions; // => { min: 1, max: 5 }
|
|
3296
|
+
* fields.commentary.defaultValue; // => ''
|
|
3297
|
+
* fields.commentary.deprecationReason; // => 'Use body.'
|
|
3298
|
+
* ReviewInput.isOneOf; // => false
|
|
3299
|
+
* SearchBy.isOneOf; // => true
|
|
3300
|
+
* ```
|
|
3301
|
+
*/
|
|
1273
3302
|
constructor(config) {
|
|
1274
3303
|
var _config$extensionASTN6, _config$isOneOf;
|
|
1275
3304
|
|
|
@@ -1288,10 +3317,40 @@ class GraphQLInputObjectType {
|
|
|
1288
3317
|
: false;
|
|
1289
3318
|
this._fields = defineInputFieldMap.bind(undefined, config);
|
|
1290
3319
|
}
|
|
3320
|
+
/**
|
|
3321
|
+
* Returns the value used by `Object.prototype.toString`.
|
|
3322
|
+
* @returns The built-in string tag for this object.
|
|
3323
|
+
*/
|
|
1291
3324
|
|
|
1292
3325
|
get [Symbol.toStringTag]() {
|
|
1293
3326
|
return 'GraphQLInputObjectType';
|
|
1294
3327
|
}
|
|
3328
|
+
/**
|
|
3329
|
+
* Returns the fields defined by this type.
|
|
3330
|
+
* @returns The fields keyed by field name.
|
|
3331
|
+
* @example
|
|
3332
|
+
* ```ts
|
|
3333
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
3334
|
+
* import { assertInputObjectType } from 'graphql/type';
|
|
3335
|
+
*
|
|
3336
|
+
* const schema = buildSchema(`
|
|
3337
|
+
* input ReviewInput {
|
|
3338
|
+
* stars: Int!
|
|
3339
|
+
* commentary: String = ""
|
|
3340
|
+
* }
|
|
3341
|
+
*
|
|
3342
|
+
* type Query {
|
|
3343
|
+
* reviews(filter: ReviewInput): [String]
|
|
3344
|
+
* }
|
|
3345
|
+
* `);
|
|
3346
|
+
*
|
|
3347
|
+
* const ReviewInput = assertInputObjectType(schema.getType('ReviewInput'));
|
|
3348
|
+
* const fields = ReviewInput.getFields();
|
|
3349
|
+
*
|
|
3350
|
+
* Object.keys(fields); // => ['stars', 'commentary']
|
|
3351
|
+
* fields.commentary.defaultValue; // => ''
|
|
3352
|
+
* ```
|
|
3353
|
+
*/
|
|
1295
3354
|
|
|
1296
3355
|
getFields() {
|
|
1297
3356
|
if (typeof this._fields === 'function') {
|
|
@@ -1300,6 +3359,31 @@ class GraphQLInputObjectType {
|
|
|
1300
3359
|
|
|
1301
3360
|
return this._fields;
|
|
1302
3361
|
}
|
|
3362
|
+
/**
|
|
3363
|
+
* Returns a normalized configuration object for this object.
|
|
3364
|
+
* @returns A configuration object that can be used to recreate this object.
|
|
3365
|
+
* @example
|
|
3366
|
+
* ```ts
|
|
3367
|
+
* import {
|
|
3368
|
+
* GraphQLInputObjectType,
|
|
3369
|
+
* GraphQLInt,
|
|
3370
|
+
* GraphQLNonNull,
|
|
3371
|
+
* } from 'graphql/type';
|
|
3372
|
+
*
|
|
3373
|
+
* const ReviewInput = new GraphQLInputObjectType({
|
|
3374
|
+
* name: 'ReviewInput',
|
|
3375
|
+
* fields: {
|
|
3376
|
+
* stars: { type: new GraphQLNonNull(GraphQLInt) },
|
|
3377
|
+
* },
|
|
3378
|
+
* });
|
|
3379
|
+
*
|
|
3380
|
+
* const config = ReviewInput.toConfig();
|
|
3381
|
+
* const ReviewInputCopy = new GraphQLInputObjectType(config);
|
|
3382
|
+
*
|
|
3383
|
+
* String(config.fields.stars.type); // => 'Int!'
|
|
3384
|
+
* String(ReviewInputCopy.getFields().stars.type); // => 'Int!'
|
|
3385
|
+
* ```
|
|
3386
|
+
*/
|
|
1303
3387
|
|
|
1304
3388
|
toConfig() {
|
|
1305
3389
|
const fields = (0, _mapValue.mapValue)(this.getFields(), (field) => ({
|
|
@@ -1320,10 +3404,51 @@ class GraphQLInputObjectType {
|
|
|
1320
3404
|
isOneOf: this.isOneOf,
|
|
1321
3405
|
};
|
|
1322
3406
|
}
|
|
3407
|
+
/**
|
|
3408
|
+
* Returns the schema coordinate identifying this input object type.
|
|
3409
|
+
* @returns The schema coordinate for this input object type.
|
|
3410
|
+
* @example
|
|
3411
|
+
* ```ts
|
|
3412
|
+
* import { buildSchema } from 'graphql/utilities';
|
|
3413
|
+
* import { assertInputObjectType } from 'graphql/type';
|
|
3414
|
+
*
|
|
3415
|
+
* const schema = buildSchema(`
|
|
3416
|
+
* input ReviewInput {
|
|
3417
|
+
* stars: Int!
|
|
3418
|
+
* }
|
|
3419
|
+
*
|
|
3420
|
+
* type Query {
|
|
3421
|
+
* reviews(filter: ReviewInput): [String]
|
|
3422
|
+
* }
|
|
3423
|
+
* `);
|
|
3424
|
+
*
|
|
3425
|
+
* const ReviewInput = assertInputObjectType(schema.getType('ReviewInput'));
|
|
3426
|
+
*
|
|
3427
|
+
* ReviewInput.toString(); // => 'ReviewInput'
|
|
3428
|
+
* ```
|
|
3429
|
+
*/
|
|
1323
3430
|
|
|
1324
3431
|
toString() {
|
|
1325
3432
|
return this.name;
|
|
1326
3433
|
}
|
|
3434
|
+
/**
|
|
3435
|
+
* Returns the JSON representation used when this object is serialized.
|
|
3436
|
+
* @returns The JSON-serializable representation.
|
|
3437
|
+
* @example
|
|
3438
|
+
* ```ts
|
|
3439
|
+
* import { GraphQLInputObjectType, GraphQLString } from 'graphql/type';
|
|
3440
|
+
*
|
|
3441
|
+
* const ReviewInput = new GraphQLInputObjectType({
|
|
3442
|
+
* name: 'ReviewInput',
|
|
3443
|
+
* fields: {
|
|
3444
|
+
* commentary: { type: GraphQLString },
|
|
3445
|
+
* },
|
|
3446
|
+
* });
|
|
3447
|
+
*
|
|
3448
|
+
* ReviewInput.toJSON(); // => 'ReviewInput'
|
|
3449
|
+
* JSON.stringify({ type: ReviewInput }); // => '{"type":"ReviewInput"}'
|
|
3450
|
+
* ```
|
|
3451
|
+
*/
|
|
1327
3452
|
|
|
1328
3453
|
toJSON() {
|
|
1329
3454
|
return this.toString();
|
|
@@ -1356,7 +3481,35 @@ function defineInputFieldMap(config) {
|
|
|
1356
3481
|
};
|
|
1357
3482
|
});
|
|
1358
3483
|
}
|
|
3484
|
+
/** Configuration used to construct a GraphQLInputObjectType. */
|
|
1359
3485
|
|
|
3486
|
+
/**
|
|
3487
|
+
* Returns true when the input field is non-null and has no default value.
|
|
3488
|
+
* @param field - The input field definition to inspect.
|
|
3489
|
+
* @returns True when the input field is non-null and has no default value.
|
|
3490
|
+
* @example
|
|
3491
|
+
* ```ts
|
|
3492
|
+
* import {
|
|
3493
|
+
* GraphQLInt,
|
|
3494
|
+
* GraphQLNonNull,
|
|
3495
|
+
* GraphQLString,
|
|
3496
|
+
* isRequiredInputField,
|
|
3497
|
+
* } from 'graphql/type';
|
|
3498
|
+
*
|
|
3499
|
+
* const requiredField = { name: 'id', type: new GraphQLNonNull(GraphQLInt) };
|
|
3500
|
+
* const optionalField = { name: 'name', type: GraphQLString };
|
|
3501
|
+
* const fieldWithDefault = {
|
|
3502
|
+
* name: 'limit',
|
|
3503
|
+
* type: new GraphQLNonNull(GraphQLInt),
|
|
3504
|
+
* defaultValue: 10,
|
|
3505
|
+
* };
|
|
3506
|
+
*
|
|
3507
|
+
* isRequiredInputField(requiredField); // => true
|
|
3508
|
+
* isRequiredInputField(optionalField); // => false
|
|
3509
|
+
* isRequiredInputField(fieldWithDefault); // => false
|
|
3510
|
+
* ```
|
|
3511
|
+
*/
|
|
1360
3512
|
function isRequiredInputField(field) {
|
|
1361
3513
|
return isNonNullType(field.type) && field.defaultValue === undefined;
|
|
1362
3514
|
}
|
|
3515
|
+
/** A map of input field names to resolved input field definitions. */
|