/
UniqueFieldDefinitionNamesRule.ts
93 lines (80 loc) · 2.7 KB
/
UniqueFieldDefinitionNamesRule.ts
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
import { GraphQLError } from '../../error/GraphQLError.js';
import type {
FieldDefinitionNode,
InputValueDefinitionNode,
NameNode,
} from '../../language/ast.js';
import type { ASTVisitor } from '../../language/visitor.js';
import type { GraphQLNamedType } from '../../type/definition.js';
import {
isInputObjectType,
isInterfaceType,
isObjectType,
} from '../../type/definition.js';
import type { SDLValidationContext } from '../ValidationContext.js';
/**
* Unique field definition names
*
* A GraphQL complex type is only valid if all its fields are uniquely named.
*/
export function UniqueFieldDefinitionNamesRule(
context: SDLValidationContext,
): ASTVisitor {
const schema = context.getSchema();
const existingTypeMap = schema ? schema.getTypeMap() : Object.create(null);
const knownFieldNames = new Map<string, Map<string, NameNode>>();
return {
InputObjectTypeDefinition: checkFieldUniqueness,
InputObjectTypeExtension: checkFieldUniqueness,
InterfaceTypeDefinition: checkFieldUniqueness,
InterfaceTypeExtension: checkFieldUniqueness,
ObjectTypeDefinition: checkFieldUniqueness,
ObjectTypeExtension: checkFieldUniqueness,
};
function checkFieldUniqueness(node: {
readonly name: NameNode;
readonly fields?:
| ReadonlyArray<InputValueDefinitionNode | FieldDefinitionNode>
| undefined;
}) {
const typeName = node.name.value;
let fieldNames = knownFieldNames.get(typeName);
if (fieldNames == null) {
fieldNames = new Map();
knownFieldNames.set(typeName, fieldNames);
}
// FIXME: https://github.com/graphql/graphql-js/issues/2203
/* c8 ignore next */
const fieldNodes = node.fields ?? [];
for (const fieldDef of fieldNodes) {
const fieldName = fieldDef.name.value;
if (hasField(existingTypeMap[typeName], fieldName)) {
context.reportError(
new GraphQLError(
`Field "${typeName}.${fieldName}" already exists in the schema. It cannot also be defined in this type extension.`,
{ nodes: fieldDef.name },
),
);
continue;
}
const knownFieldName = fieldNames.get(fieldName);
if (knownFieldName != null) {
context.reportError(
new GraphQLError(
`Field "${typeName}.${fieldName}" can only be defined once.`,
{ nodes: [knownFieldName, fieldDef.name] },
),
);
} else {
fieldNames.set(fieldName, fieldDef.name);
}
}
return false;
}
}
function hasField(type: GraphQLNamedType, fieldName: string): boolean {
if (isObjectType(type) || isInterfaceType(type) || isInputObjectType(type)) {
return type.getFields()[fieldName] != null;
}
return false;
}