Skip to content

Commit 707f61f

Browse files
Fixes default values and adds tests
1 parent dbf92a5 commit 707f61f

File tree

4 files changed

+339
-76
lines changed

4 files changed

+339
-76
lines changed

Sources/GraphQL/Execution/Values.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func coerceValue(type: GraphQLInputType, value: Map) throws -> Map? {
152152
var fieldValue = try coerceValue(type: field!.type, value: value[fieldName] ?? .null)
153153

154154
if fieldValue == .null {
155-
fieldValue = field.flatMap({ $0.defaultValue.map({ .string($0) }) })
155+
fieldValue = field.flatMap({ $0.defaultValue })
156156
} else {
157157
objCopy[fieldName] = fieldValue
158158
}

Sources/GraphQL/Type/Definition.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import OrderedCollections
12
import Foundation
23
import NIO
34

@@ -1274,12 +1275,12 @@ func defineInputObjectFieldMap(
12741275

12751276
public struct InputObjectField {
12761277
public let type: GraphQLInputType
1277-
public let defaultValue: String?
1278+
public let defaultValue: Map?
12781279
public let description: String?
12791280

12801281
public init(type: GraphQLInputType, defaultValue: Map? = nil, description: String? = nil) {
12811282
self.type = type
1282-
self.defaultValue = defaultValue?.description
1283+
self.defaultValue = defaultValue
12831284
self.description = description
12841285
}
12851286
}
@@ -1290,13 +1291,13 @@ public final class InputObjectFieldDefinition {
12901291
public let name: String
12911292
public internal(set) var type: GraphQLInputType
12921293
public let description: String?
1293-
public let defaultValue: String?
1294+
public let defaultValue: Map?
12941295

12951296
init(
12961297
name: String,
12971298
type: GraphQLInputType,
12981299
description: String? = nil,
1299-
defaultValue: String? = nil
1300+
defaultValue: Map? = nil
13001301
) {
13011302
self.name = name
13021303
self.type = type
@@ -1351,7 +1352,7 @@ extension InputObjectFieldDefinition : KeySubscriptable {
13511352
}
13521353
}
13531354

1354-
public typealias InputObjectFieldDefinitionMap = [String: InputObjectFieldDefinition]
1355+
public typealias InputObjectFieldDefinitionMap = OrderedDictionary<String, InputObjectFieldDefinition>
13551356

13561357
/**
13571358
* List Modifier

Sources/GraphQL/Utilities/ValueFromAST.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,13 @@ func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: M
7575
type: field.type,
7676
variables: variables
7777
)
78-
79-
if fieldValue == .null {
80-
if let defaultValue = field.defaultValue {
81-
obj[fieldName] = .string(defaultValue)
82-
} else {
83-
obj[fieldName] = .null
84-
}
78+
obj[fieldName] = fieldValue
79+
} else {
80+
if let defaultValue = field.defaultValue {
81+
obj[fieldName] = defaultValue
8582
} else {
86-
obj[fieldName] = fieldValue
83+
obj[fieldName] = .undefined
8784
}
88-
} else {
89-
obj[fieldName] = .undefined
9085
}
9186

9287
return obj

0 commit comments

Comments
 (0)