forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPossibleTypeExtensionsRule.ts
More file actions
146 lines (132 loc) · 4.42 KB
/
PossibleTypeExtensionsRule.ts
File metadata and controls
146 lines (132 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { didYouMean } from '../../jsutils/didYouMean.js';
import { inspect } from '../../jsutils/inspect.js';
import { invariant } from '../../jsutils/invariant.js';
import { suggestionList } from '../../jsutils/suggestionList.js';
import { GraphQLError } from '../../error/GraphQLError.js';
import type {
TypeDefinitionNode,
TypeExtensionNode,
} from '../../language/ast.js';
import { Kind } from '../../language/kinds.js';
import { isTypeDefinitionNode } from '../../language/predicates.js';
import type { ASTVisitor } from '../../language/visitor.js';
import type { GraphQLNamedType } from '../../type/definition.js';
import {
isEnumType,
isInputObjectType,
isInterfaceType,
isObjectType,
isScalarType,
isUnionType,
} from '../../type/definition.js';
import type { SDLValidationContext } from '../ValidationContext.js';
/**
* Possible type extension
*
* A type extension is only valid if the type is defined and has the same kind.
*/
export function PossibleTypeExtensionsRule(
context: SDLValidationContext,
): ASTVisitor {
const schema = context.getSchema();
const definedTypes = new Map<string, TypeDefinitionNode>();
for (const def of context.getDocument().definitions) {
if (isTypeDefinitionNode(def)) {
definedTypes.set(def.name.value, def);
}
}
return {
ScalarTypeExtension: checkExtension,
ObjectTypeExtension: checkExtension,
InterfaceTypeExtension: checkExtension,
UnionTypeExtension: checkExtension,
EnumTypeExtension: checkExtension,
InputObjectTypeExtension: checkExtension,
};
function checkExtension(node: TypeExtensionNode): void {
const typeName = node.name.value;
const defNode = definedTypes.get(typeName);
const existingType = schema?.getType(typeName);
let expectedKind: Kind | undefined;
if (defNode != null) {
expectedKind = defKindToExtKind[defNode.kind];
} else if (existingType) {
expectedKind = typeToExtKind(existingType);
}
if (expectedKind != null) {
if (expectedKind !== node.kind) {
const kindStr = extensionKindToTypeName(node.kind);
context.reportError(
new GraphQLError(`Cannot extend non-${kindStr} type "${typeName}".`, {
nodes: defNode ? [defNode, node] : node,
}),
);
}
} else {
const allTypeNames = [
...definedTypes.keys(),
...Object.keys(schema?.getTypeMap() ?? {}),
];
context.reportError(
new GraphQLError(
`Cannot extend type "${typeName}" because it is not defined.` +
didYouMean(suggestionList(typeName, allTypeNames)),
{ nodes: node.name },
),
);
}
}
}
const defKindToExtKind = {
[Kind.SCALAR_TYPE_DEFINITION]: Kind.SCALAR_TYPE_EXTENSION,
[Kind.OBJECT_TYPE_DEFINITION]: Kind.OBJECT_TYPE_EXTENSION,
[Kind.INTERFACE_TYPE_DEFINITION]: Kind.INTERFACE_TYPE_EXTENSION,
[Kind.UNION_TYPE_DEFINITION]: Kind.UNION_TYPE_EXTENSION,
[Kind.ENUM_TYPE_DEFINITION]: Kind.ENUM_TYPE_EXTENSION,
[Kind.INPUT_OBJECT_TYPE_DEFINITION]: Kind.INPUT_OBJECT_TYPE_EXTENSION,
} as const;
function typeToExtKind(type: GraphQLNamedType): Kind {
if (isScalarType(type)) {
return Kind.SCALAR_TYPE_EXTENSION;
}
if (isObjectType(type)) {
return Kind.OBJECT_TYPE_EXTENSION;
}
if (isInterfaceType(type)) {
return Kind.INTERFACE_TYPE_EXTENSION;
}
if (isUnionType(type)) {
return Kind.UNION_TYPE_EXTENSION;
}
if (isEnumType(type)) {
return Kind.ENUM_TYPE_EXTENSION;
}
if (isInputObjectType(type)) {
return Kind.INPUT_OBJECT_TYPE_EXTENSION;
// c8 control statement technically placed a line early secondary to
// slight swc source mapping error (at least as compared to ts-node without swc)
/* c8 ignore next 4 */
}
// Not reachable. All possible types have been considered
invariant(false, 'Unexpected type: ' + inspect(type));
}
function extensionKindToTypeName(kind: Kind): string {
switch (kind) {
case Kind.SCALAR_TYPE_EXTENSION:
return 'scalar';
case Kind.OBJECT_TYPE_EXTENSION:
return 'object';
case Kind.INTERFACE_TYPE_EXTENSION:
return 'interface';
case Kind.UNION_TYPE_EXTENSION:
return 'union';
case Kind.ENUM_TYPE_EXTENSION:
return 'enum';
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
return 'input object';
// Not reachable. All possible types have been considered
/* c8 ignore next 2 */
default:
invariant(false, 'Unexpected kind: ' + inspect(kind));
}
}