Skip to content

Commit 9922b2d

Browse files
fix: Adds variable directive support
1 parent 07d415f commit 9922b2d

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public final class OperationDefinition {
401401
}
402402
return .array(variableDefinitions)
403403
case "directives":
404-
guard !variableDefinitions.isEmpty else {
404+
guard !directives.isEmpty else {
405405
return nil
406406
}
407407
return .array(directives)
@@ -475,12 +475,20 @@ public final class VariableDefinition {
475475
public private(set) var variable: Variable
476476
public private(set) var type: Type
477477
public private(set) var defaultValue: Value?
478+
public private(set) var directives: [Directive]
478479

479-
init(loc: Location? = nil, variable: Variable, type: Type, defaultValue: Value? = nil) {
480+
init(
481+
loc: Location? = nil,
482+
variable: Variable,
483+
type: Type,
484+
defaultValue: Value? = nil,
485+
directives: [Directive] = []
486+
) {
480487
self.loc = loc
481488
self.variable = variable
482489
self.type = type
483490
self.defaultValue = defaultValue
491+
self.directives = directives
484492
}
485493

486494
public func get(key: String) -> NodeResult? {
@@ -491,6 +499,11 @@ public final class VariableDefinition {
491499
return .node(type)
492500
case "defaultValue":
493501
return defaultValue.map { .node($0) }
502+
case "directives":
503+
guard !directives.isEmpty else {
504+
return nil
505+
}
506+
return .array(directives)
494507
default:
495508
return nil
496509
}
@@ -525,6 +538,14 @@ public final class VariableDefinition {
525538
return
526539
}
527540
self.defaultValue = defaultValue
541+
case "directives":
542+
guard
543+
case let .array(values) = value,
544+
let directives = values as? [Directive]
545+
else {
546+
return
547+
}
548+
self.directives = directives
528549
default:
529550
return
530551
}

Sources/GraphQL/Language/Parser.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ func parseVariableDefinition(lexer: Lexer) throws -> VariableDefinition {
276276
variable: parseVariable(lexer: lexer),
277277
type: (expect(lexer: lexer, kind: .colon), parseTypeReference(lexer: lexer)).1,
278278
defaultValue: skip(lexer: lexer, kind: .equals) ?
279-
parseValueLiteral(lexer: lexer, isConst: true) : nil
279+
parseValueLiteral(lexer: lexer, isConst: true) : nil,
280+
directives: parseDirectives(lexer: lexer)
280281
)
281282
}
282283

Sources/GraphQL/Language/Printer.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ extension OperationDefinition: Printable {
4848
extension VariableDefinition: Printable {
4949
var printed: String {
5050
variable + ": " + type.printed + wrap(" = ", defaultValue?.printed)
51-
// + wrap(" ", join(directives, " "))
52-
// TODO: variable directives are currently not supported
51+
+ wrap(" ", join(directives, " "))
5352
}
5453
}
5554

Sources/GraphQL/Language/Visitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let QueryDocumentKeys: [Kind: [String]] = [
33

44
.document: ["definitions"],
55
.operationDefinition: ["name", "variableDefinitions", "directives", "selectionSet"],
6-
.variableDefinition: ["variable", "type", "defaultValue"],
6+
.variableDefinition: ["variable", "type", "defaultValue", "directives"],
77
.variable: ["name"],
88
.selectionSet: ["selections"],
99
.field: ["alias", "name", "arguments", "directives", "selectionSet"],

Sources/GraphQL/Type/Directives.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public enum DirectiveLocation: String, Encodable {
77
case fragmentDefinition = "FRAGMENT_DEFINITION"
88
case fragmentSpread = "FRAGMENT_SPREAD"
99
case inlineFragment = "INLINE_FRAGMENT"
10+
case variableDefinition = "VARIABLE_DEFINITION"
1011
// Schema Definitions
1112
case schema = "SCHEMA"
1213
case scalar = "SCALAR"
@@ -33,7 +34,7 @@ public struct GraphQLDirective: Encodable {
3334

3435
public init(
3536
name: String,
36-
description: String,
37+
description: String = "",
3738
locations: [DirectiveLocation],
3839
args: GraphQLArgumentConfigMap = [:]
3940
) throws {

0 commit comments

Comments
 (0)