Skip to content

Commit 436998c

Browse files
feature: NoSchemaIntrospectionCustomRule
1 parent 7a2e1be commit 436998c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/GraphQL/Type/Introspection.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,18 @@ let TypeNameMetaFieldDef = GraphQLFieldDefinition(
476476
eventLoopGroup.next().makeSucceededFuture(info.parentType.name)
477477
}
478478
)
479+
480+
let introspectionTypeNames = [
481+
__Schema.name,
482+
__Directive.name,
483+
__DirectiveLocation.name,
484+
__Type.name,
485+
__Field.name,
486+
__InputValue.name,
487+
__EnumValue.name,
488+
__TypeKind.name,
489+
]
490+
491+
func isIntrospectionType(type: GraphQLNamedType) -> Bool {
492+
return introspectionTypeNames.contains(type.name)
493+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/**
3+
* Prohibit introspection queries
4+
*
5+
* A GraphQL document is only valid if all fields selected are not fields that
6+
* return an introspection type.
7+
*
8+
* Note: This rule is optional and is not part of the Validation section of the
9+
* GraphQL Specification. This rule effectively disables introspection, which
10+
* does not reflect best practices and should only be done if absolutely necessary.
11+
*/
12+
public func NoSchemaIntrospectionCustomRule(context: ValidationContext) -> Visitor {
13+
return Visitor(
14+
enter: { node, _, _, _, _ in
15+
if let node = node as? Field {
16+
if
17+
let type = getNamedType(type: context.type),
18+
isIntrospectionType(type: type)
19+
{
20+
context.report(
21+
error: GraphQLError(
22+
message: "GraphQL introspection has been disabled, but the requested query contained the field \(node.name.value)",
23+
nodes: [node]
24+
)
25+
)
26+
}
27+
}
28+
return .continue
29+
}
30+
)
31+
}

0 commit comments

Comments
 (0)