Skip to content

Commit ac9fbbc

Browse files
committed
Add variables are input types validation rule
1 parent ac2a4ed commit ac9fbbc

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Variables are input types
3+
*
4+
* A GraphQL operation is only valid if all the variables it defines are of
5+
* input types (scalar, enum, or input object).
6+
*
7+
* See https://spec.graphql.org/draft/#sec-Variables-Are-Input-Types
8+
*/
9+
struct VariablesAreInputTypesRule: ValidationRule {
10+
let context: ValidationContext
11+
func enter(variableDefinition: VariableDefinition, key: AnyKeyPath?, parent: VisitorParent?, ancestors: [VisitorParent]) -> VisitResult<VariableDefinition> {
12+
if context.inputType == nil {
13+
let error = GraphQLError(
14+
message: "Variable \"$\(variableDefinition.variable.name.value)\" cannot be non-input type \"\(variableDefinition.type.printed)\".",
15+
nodes: [variableDefinition.type]
16+
)
17+
context.report(error: error)
18+
}
19+
return .continue
20+
}
21+
}

Sources/GraphQL/Validation/SpecifiedRules.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ let specifiedRules: [ValidationRule.Type] = [
2626
// VariablesInAllowedPosition,
2727
// OverlappingFieldsCanBeMerged,
2828
// UniqueInputFieldNames,
29+
VariablesAreInputTypesRule.self
2930
]

0 commit comments

Comments
 (0)