Skip to content

Commit 4544619

Browse files
feat: Adds KnownTypeNamesRule SDL support
1 parent 3cecede commit 4544619

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
func isTypeSystemDefinitionNode(
3+
_ node: Node
4+
) -> Bool {
5+
return
6+
node.kind == Kind.schemaDefinition ||
7+
isTypeDefinitionNode(node) ||
8+
node.kind == Kind.directiveDefinition
9+
}
10+
11+
func isTypeDefinitionNode(
12+
_ node: Node
13+
) -> Bool {
14+
return
15+
node.kind == Kind.scalarTypeDefinition ||
16+
node.kind == Kind.objectTypeDefinition ||
17+
node.kind == Kind.interfaceTypeDefinition ||
18+
node.kind == Kind.unionTypeDefinition ||
19+
node.kind == Kind.enumTypeDefinition ||
20+
node.kind == Kind.inputObjectTypeDefinition
21+
}
22+
23+
func isTypeSystemExtensionNode(
24+
_ node: Node
25+
) -> Bool {
26+
return
27+
node.kind == Kind.schemaExtensionDefinition ||
28+
isTypeExtensionNode(node)
29+
}
30+
31+
func isTypeExtensionNode(
32+
_ node: Node
33+
) -> Bool {
34+
return
35+
node.kind == Kind.scalarExtensionDefinition ||
36+
node.kind == Kind.typeExtensionDefinition ||
37+
node.kind == Kind.interfaceExtensionDefinition ||
38+
node.kind == Kind.unionExtensionDefinition ||
39+
node.kind == Kind.enumExtensionDefinition ||
40+
node.kind == Kind.inputObjectExtensionDefinition
41+
}

Sources/GraphQL/Validation/Rules/KnownTypeNamesRule.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func KnownTypeNamesRule(context: SDLorNormalValidationContext) -> Visitor {
1717
}
1818
for definition in definitions {
1919
if
20-
let type = definition as? TypeDefinition,
21-
let nameResult = type.get(key: "name"),
20+
isTypeSystemDefinitionNode(definition),
21+
let nameResult = definition.get(key: "name"),
2222
case let .node(nameNode) = nameResult,
2323
let name = nameNode as? Name
2424
{
@@ -27,11 +27,18 @@ func KnownTypeNamesRule(context: SDLorNormalValidationContext) -> Visitor {
2727
}
2828

2929
return Visitor(
30-
enter: { node, _, _, _, _ in
30+
enter: { node, _, parent, _, ancestors in
3131
if let type = node as? NamedType {
3232
let typeName = type.name.value
3333
if !typeNames.contains(typeName) {
34-
// TODO: Add SDL support
34+
let definitionNode = ancestors.count > 2 ? ancestors[2] : parent
35+
var isSDL = false
36+
if let definitionNode = definitionNode, case let .node(node) = definitionNode {
37+
isSDL = isSDLNode(node)
38+
}
39+
if isSDL, standardTypeNames.contains(typeName) {
40+
return .continue
41+
}
3542

3643
let suggestedTypes = suggestionList(
3744
input: typeName,
@@ -50,3 +57,13 @@ func KnownTypeNamesRule(context: SDLorNormalValidationContext) -> Visitor {
5057
}
5158
)
5259
}
60+
61+
let standardTypeNames: Set<String> = {
62+
var result = specifiedScalarTypes.map { $0.name }
63+
result.append(contentsOf: introspectionTypes.map { $0.name })
64+
return Set(result)
65+
}()
66+
67+
func isSDLNode(_ value: Node) -> Bool {
68+
return isTypeSystemDefinitionNode(value) || isTypeSystemExtensionNode(value)
69+
}

0 commit comments

Comments
 (0)