UNPKG

4.57 kBJavaScriptView Raw
1var _defKindToExtKind;
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5import inspect from "../../jsutils/inspect.mjs";
6import invariant from "../../jsutils/invariant.mjs";
7import didYouMean from "../../jsutils/didYouMean.mjs";
8import suggestionList from "../../jsutils/suggestionList.mjs";
9import { GraphQLError } from "../../error/GraphQLError.mjs";
10import { Kind } from "../../language/kinds.mjs";
11import { isTypeDefinitionNode } from "../../language/predicates.mjs";
12import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from "../../type/definition.mjs";
13
14/**
15 * Possible type extension
16 *
17 * A type extension is only valid if the type is defined and has the same kind.
18 */
19export function PossibleTypeExtensionsRule(context) {
20 var schema = context.getSchema();
21 var definedTypes = Object.create(null);
22
23 for (var _i2 = 0, _context$getDocument$2 = context.getDocument().definitions; _i2 < _context$getDocument$2.length; _i2++) {
24 var def = _context$getDocument$2[_i2];
25
26 if (isTypeDefinitionNode(def)) {
27 definedTypes[def.name.value] = def;
28 }
29 }
30
31 return {
32 ScalarTypeExtension: checkExtension,
33 ObjectTypeExtension: checkExtension,
34 InterfaceTypeExtension: checkExtension,
35 UnionTypeExtension: checkExtension,
36 EnumTypeExtension: checkExtension,
37 InputObjectTypeExtension: checkExtension
38 };
39
40 function checkExtension(node) {
41 var typeName = node.name.value;
42 var defNode = definedTypes[typeName];
43 var existingType = schema === null || schema === void 0 ? void 0 : schema.getType(typeName);
44 var expectedKind;
45
46 if (defNode) {
47 expectedKind = defKindToExtKind[defNode.kind];
48 } else if (existingType) {
49 expectedKind = typeToExtKind(existingType);
50 }
51
52 if (expectedKind) {
53 if (expectedKind !== node.kind) {
54 var kindStr = extensionKindToTypeName(node.kind);
55 context.reportError(new GraphQLError("Cannot extend non-".concat(kindStr, " type \"").concat(typeName, "\"."), defNode ? [defNode, node] : node));
56 }
57 } else {
58 var allTypeNames = Object.keys(definedTypes);
59
60 if (schema) {
61 allTypeNames = allTypeNames.concat(Object.keys(schema.getTypeMap()));
62 }
63
64 var suggestedTypes = suggestionList(typeName, allTypeNames);
65 context.reportError(new GraphQLError("Cannot extend type \"".concat(typeName, "\" because it is not defined.") + didYouMean(suggestedTypes), node.name));
66 }
67 }
68}
69var defKindToExtKind = (_defKindToExtKind = {}, _defineProperty(_defKindToExtKind, Kind.SCALAR_TYPE_DEFINITION, Kind.SCALAR_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.UNION_TYPE_DEFINITION, Kind.UNION_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.ENUM_TYPE_DEFINITION, Kind.ENUM_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.INPUT_OBJECT_TYPE_DEFINITION, Kind.INPUT_OBJECT_TYPE_EXTENSION), _defKindToExtKind);
70
71function typeToExtKind(type) {
72 if (isScalarType(type)) {
73 return Kind.SCALAR_TYPE_EXTENSION;
74 }
75
76 if (isObjectType(type)) {
77 return Kind.OBJECT_TYPE_EXTENSION;
78 }
79
80 if (isInterfaceType(type)) {
81 return Kind.INTERFACE_TYPE_EXTENSION;
82 }
83
84 if (isUnionType(type)) {
85 return Kind.UNION_TYPE_EXTENSION;
86 }
87
88 if (isEnumType(type)) {
89 return Kind.ENUM_TYPE_EXTENSION;
90 } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
91
92
93 if (isInputObjectType(type)) {
94 return Kind.INPUT_OBJECT_TYPE_EXTENSION;
95 } // istanbul ignore next (Not reachable. All possible types have been considered)
96
97
98 false || invariant(0, 'Unexpected type: ' + inspect(type));
99}
100
101function extensionKindToTypeName(kind) {
102 switch (kind) {
103 case Kind.SCALAR_TYPE_EXTENSION:
104 return 'scalar';
105
106 case Kind.OBJECT_TYPE_EXTENSION:
107 return 'object';
108
109 case Kind.INTERFACE_TYPE_EXTENSION:
110 return 'interface';
111
112 case Kind.UNION_TYPE_EXTENSION:
113 return 'union';
114
115 case Kind.ENUM_TYPE_EXTENSION:
116 return 'enum';
117
118 case Kind.INPUT_OBJECT_TYPE_EXTENSION:
119 return 'input object';
120 } // istanbul ignore next (Not reachable. All possible types have been considered)
121
122
123 false || invariant(0, 'Unexpected kind: ' + inspect(kind));
124}