Skip to content

Commit 1042554

Browse files
committed
Fix default values encoding.
1 parent 01784d0 commit 1042554

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

Sources/GraphQL/Execution/Execute.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func execute(
285285
exeContext: buildContext,
286286
operation: buildContext.operation,
287287
rootValue: rootValue
288-
).flatMapThrowing { data -> GraphQLResult in
288+
).flatMapThrowing { data -> GraphQLResult in
289289
var dataMap: Map = [:]
290290

291291
for (key, value) in data {

Sources/GraphQL/Execution/Values.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func getArgumentValues(argDefs: [GraphQLArgumentDefinition], argASTs: [Argument]
4242
)
4343

4444
if value == nil {
45-
value = argDef.defaultValue
45+
value = argDef.defaultValue.map({ .string($0) })
4646
}
4747

4848
if let value = value {
@@ -150,7 +150,7 @@ func coerceValue(type: GraphQLInputType, value: Map) throws -> Map? {
150150
var fieldValue = try coerceValue(type: field!.type, value: value[fieldName] ?? .null)
151151

152152
if fieldValue == .null {
153-
fieldValue = field?.defaultValue
153+
fieldValue = field.flatMap({ $0.defaultValue.map({ .string($0) }) })
154154
} else {
155155
objCopy[fieldName] = fieldValue
156156
}

Sources/GraphQL/Type/Definition.swift

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
import NIO
23

34
/**
@@ -666,7 +667,7 @@ public struct GraphQLArgument {
666667
public struct GraphQLArgumentDefinition {
667668
public let name: String
668669
public let type: GraphQLInputType
669-
public let defaultValue: Map?
670+
public let defaultValue: String?
670671
public let description: String?
671672

672673
init(
@@ -677,7 +678,14 @@ public struct GraphQLArgumentDefinition {
677678
) {
678679
self.name = name
679680
self.type = type
680-
self.defaultValue = defaultValue
681+
682+
self.defaultValue = try? defaultValue.flatMap {
683+
String(
684+
data: try JSONEncoder().encode($0),
685+
encoding: .utf8
686+
)
687+
}
688+
681689
self.description = description
682690
}
683691
}
@@ -1253,12 +1261,19 @@ func defineInputObjectFieldMap(
12531261

12541262
public struct InputObjectField {
12551263
public let type: GraphQLInputType
1256-
public let defaultValue: Map?
1264+
public let defaultValue: String?
12571265
public let description: String?
12581266

12591267
public init(type: GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
12601268
self.type = type
1261-
self.defaultValue = defaultValue
1269+
1270+
self.defaultValue = try? defaultValue.flatMap {
1271+
String(
1272+
data: try JSONEncoder().encode($0),
1273+
encoding: .utf8
1274+
)
1275+
}
1276+
12621277
self.description = description
12631278
}
12641279
}
@@ -1269,7 +1284,7 @@ public struct InputObjectFieldDefinition {
12691284
public let name: String
12701285
public let description: String?
12711286
public let type: GraphQLInputType
1272-
public let defaultValue: Map?
1287+
public let defaultValue: String?
12731288
}
12741289

12751290
extension InputObjectFieldDefinition : Encodable {

Sources/GraphQL/Type/Introspection.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ let __InputValue = try! GraphQLObjectType(
360360
return nil
361361
}
362362

363-
return defaultValue
363+
return .string(defaultValue)
364364
}
365365
)
366366
]

Sources/GraphQL/Utilities/ValueFromAST.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
7575
)
7676

7777
if fieldValue == .null {
78-
fieldValue = field?.defaultValue
78+
fieldValue = field.flatMap({ $0.defaultValue.map({ .string($0) }) })
7979
} else {
8080
obj[fieldName] = fieldValue
8181
}

0 commit comments

Comments
 (0)