1 | var _defKindToExtKind;
|
2 |
|
3 | function _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 |
|
5 | import inspect from "../../jsutils/inspect.mjs";
|
6 | import invariant from "../../jsutils/invariant.mjs";
|
7 | import didYouMean from "../../jsutils/didYouMean.mjs";
|
8 | import suggestionList from "../../jsutils/suggestionList.mjs";
|
9 | import { GraphQLError } from "../../error/GraphQLError.mjs";
|
10 | import { Kind } from "../../language/kinds.mjs";
|
11 | import { isTypeDefinitionNode } from "../../language/predicates.mjs";
|
12 | import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from "../../type/definition.mjs";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export 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 | }
|
69 | var 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 |
|
71 | function 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 | }
|
91 |
|
92 |
|
93 | if (isInputObjectType(type)) {
|
94 | return Kind.INPUT_OBJECT_TYPE_EXTENSION;
|
95 | }
|
96 |
|
97 |
|
98 | false || invariant(0, 'Unexpected type: ' + inspect(type));
|
99 | }
|
100 |
|
101 | function 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 | }
|
121 |
|
122 |
|
123 | false || invariant(0, 'Unexpected kind: ' + inspect(kind));
|
124 | }
|