UNPKG

5.94 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 */
10'use strict';
11
12var ID = 'id';
13var ID_TYPE = 'ID';
14
15/**
16 * Determine if the given type may implement the named type:
17 * - it is the named type
18 * - it implements the named interface
19 * - it is an abstract type and *some* of its concrete types may
20 * implement the named type
21 */
22function mayImplement(schema, type, typeName) {
23 var unmodifiedType = getRawType(type);
24 return unmodifiedType.toString() === typeName || implementsInterface(unmodifiedType, typeName) || isAbstractType(unmodifiedType) && hasConcreteTypeThatImplements(schema, unmodifiedType, typeName);
25}
26
27function canHaveSelections(type) {
28 return type instanceof require("graphql").GraphQLObjectType || type instanceof require("graphql").GraphQLInterfaceType;
29}
30/**
31 * Implements duck typing that checks whether a type has an id field of the ID
32 * type. This is approximating what we can hopefully do with the __id proposal
33 * a bit more cleanly.
34 *
35 * https://github.com/graphql/graphql-future/blob/master/01%20-%20__id.md
36 */
37
38
39function hasID(schema, type) {
40 var unmodifiedType = getRawType(type);
41 !(unmodifiedType instanceof require("graphql").GraphQLObjectType || unmodifiedType instanceof require("graphql").GraphQLInterfaceType) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLSchemaUtils.hasID(): Expected a concrete type or interface, ' + 'got type `%s`.', type) : require("fbjs/lib/invariant")(false) : void 0;
42 var idType = schema.getType(ID_TYPE);
43 var idField = unmodifiedType.getFields()[ID];
44 return idField && getRawType(idField.type) === idType;
45}
46/**
47 * Determine if a type is abstract (not concrete).
48 *
49 * Note: This is used in place of the `graphql` version of the function in order
50 * to not break `instanceof` checks with Jest. This version also unwraps
51 * non-null/list wrapper types.
52 */
53
54
55function isAbstractType(type) {
56 var rawType = getRawType(type);
57 return rawType instanceof require("graphql").GraphQLInterfaceType || rawType instanceof require("graphql").GraphQLUnionType;
58}
59
60function isUnionType(type) {
61 return type instanceof require("graphql").GraphQLUnionType;
62}
63/**
64 * Get the unmodified type, with list/null wrappers removed.
65 */
66
67
68function getRawType(type) {
69 return require("./nullthrowsOSS")(require("graphql").getNamedType(type));
70}
71/**
72 * Gets the non-list type, removing the list wrapper if present.
73 */
74
75
76function getSingularType(type) {
77 var unmodifiedType = type;
78
79 while (unmodifiedType instanceof require("graphql").GraphQLList) {
80 unmodifiedType = unmodifiedType.ofType;
81 }
82
83 return unmodifiedType;
84}
85/**
86 * @public
87 */
88
89
90function implementsInterface(type, interfaceName) {
91 return getInterfaces(type).some(function (interfaceType) {
92 return interfaceType.toString() === interfaceName;
93 });
94}
95/**
96 * @private
97 */
98
99
100function hasConcreteTypeThatImplements(schema, type, interfaceName) {
101 return isAbstractType(type) && getConcreteTypes(schema, type).some(function (concreteType) {
102 return implementsInterface(concreteType, interfaceName);
103 });
104}
105/**
106 * @private
107 */
108
109
110function getConcreteTypes(schema, type) {
111 return schema.getPossibleTypes(require("graphql").assertAbstractType(type));
112}
113/**
114 * @private
115 */
116
117
118function getInterfaces(type) {
119 if (type instanceof require("graphql").GraphQLObjectType) {
120 return type.getInterfaces();
121 }
122
123 return [];
124}
125/**
126 * @public
127 *
128 * Determine if an AST node contains a fragment/operation definition.
129 */
130
131
132function isExecutableDefinitionAST(ast) {
133 return ast.kind === 'FragmentDefinition' || ast.kind === 'OperationDefinition';
134}
135/**
136 * @public
137 *
138 * Determine if an AST node contains a schema definition.
139 */
140
141
142function isSchemaDefinitionAST(ast) {
143 return ast.kind === 'SchemaDefinition' || ast.kind === 'ScalarTypeDefinition' || ast.kind === 'ObjectTypeDefinition' || ast.kind === 'InterfaceTypeDefinition' || ast.kind === 'UnionTypeDefinition' || ast.kind === 'EnumTypeDefinition' || ast.kind === 'InputObjectTypeDefinition' || ast.kind === 'DirectiveDefinition' || ast.kind === 'ScalarTypeExtension' || ast.kind === 'ObjectTypeExtension' || ast.kind === 'InterfaceTypeExtension' || ast.kind === 'UnionTypeExtension' || ast.kind === 'EnumTypeExtension' || ast.kind === 'InputObjectTypeExtension';
144}
145
146function assertTypeWithFields(type) {
147 !(type instanceof require("graphql").GraphQLObjectType || type instanceof require("graphql").GraphQLInterfaceType) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLSchemaUtils: Expected type `%s` to be an object or interface type.', type) : require("fbjs/lib/invariant")(false) : void 0;
148 return type;
149}
150/**
151 * Helper for calling `typeFromAST()` with a clear warning when the type does
152 * not exist. This enables the pattern `assertXXXType(getTypeFromAST(...))`,
153 * emitting distinct errors for unknown types vs types of the wrong category.
154 */
155
156
157function getTypeFromAST(schema, ast) {
158 var type = require("graphql").typeFromAST(schema, ast);
159
160 !require("graphql").isType(type) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'GraphQLSchemaUtils: Unknown type `%s`.', require("graphql").print(ast)) : require("fbjs/lib/invariant")(false) : void 0;
161 return type;
162}
163
164module.exports = {
165 assertTypeWithFields: assertTypeWithFields,
166 canHaveSelections: canHaveSelections,
167 getNullableType: require("graphql").getNullableType,
168 getRawType: getRawType,
169 getSingularType: getSingularType,
170 getTypeFromAST: getTypeFromAST,
171 hasID: hasID,
172 implementsInterface: implementsInterface,
173 isAbstractType: isAbstractType,
174 isUnionType: isUnionType,
175 isExecutableDefinitionAST: isExecutableDefinitionAST,
176 isSchemaDefinitionAST: isSchemaDefinitionAST,
177 mayImplement: mayImplement
178};
\No newline at end of file