Skip to content

Commit 9e504c9

Browse files
Merge pull request #56 from NeedleInAJayStack/fix/recursiveInputs
Adds support for reference types on Input fields
2 parents b17b26e + 3e52163 commit 9e504c9

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77
.library(name: "Graphiti", targets: ["Graphiti"]),
88
],
99
dependencies: [
10-
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", .upToNextMajor(from: "1.2.0"))
10+
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", .upToNextMajor(from: "1.3.0"))
1111
],
1212
targets: [
1313
.target(name: "Graphiti", dependencies: ["GraphQL"]),

Sources/Graphiti/Input/Input.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public final class Input<Resolver, Context, InputObjectType : Decodable> : Compo
1313
try typeProvider.map(InputObjectType.self, to: inputObjectType)
1414
}
1515

16-
func fields(typeProvider: TypeProvider) throws -> InputObjectConfigFieldMap {
17-
var map: InputObjectConfigFieldMap = [:]
16+
func fields(typeProvider: TypeProvider) throws -> InputObjectFieldMap {
17+
var map: InputObjectFieldMap = [:]
1818

1919
for field in fields {
2020
let (name, field) = try field.field(typeProvider: typeProvider)

Sources/Graphiti/InputField/InputField.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public extension InputField {
3232
}
3333
}
3434

35+
public extension InputField {
36+
convenience init<KeyPathType>(
37+
_ name: String,
38+
at keyPath: KeyPath<InputObjectType, KeyPathType>,
39+
as: FieldType.Type
40+
) {
41+
self.init(name: name)
42+
}
43+
}
44+
3545
public extension InputField where FieldType : Encodable {
3646
func defaultValue(_ defaultValue: FieldType) -> Self {
3747
self.defaultValue = AnyEncodable(defaultValue)

Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ struct ID : Codable {
2424
struct User : Codable {
2525
let id: String
2626
let name: String?
27+
let friends: [User]?
2728

28-
init(id: String, name: String?) {
29+
init(id: String, name: String?, friends: [User]?) {
2930
self.id = id
3031
self.name = name
32+
self.friends = friends
3133
}
3234

3335
init(_ input: UserInput) {
3436
self.id = input.id
3537
self.name = input.name
38+
if let friends = input.friends {
39+
self.friends = friends.map{ User($0) }
40+
} else {
41+
self.friends = nil
42+
}
3643
}
3744

3845
func toEvent(context: HelloContext, arguments: NoArguments) throws -> UserEvent {
@@ -43,6 +50,7 @@ struct User : Codable {
4350
struct UserInput : Codable {
4451
let id: String
4552
let name: String?
53+
let friends: [UserInput]?
4654
}
4755

4856
struct UserEvent : Codable {
@@ -85,7 +93,7 @@ struct HelloResolver {
8593
}
8694

8795
func getUser(context: HelloContext, arguments: NoArguments) -> User {
88-
User(id: "123", name: "John Doe")
96+
User(id: "123", name: "John Doe", friends: nil)
8997
}
9098

9199
struct AddUserArguments : Codable {
@@ -111,11 +119,13 @@ struct HelloAPI : API {
111119
Type(User.self) {
112120
Field("id", at: \.id)
113121
Field("name", at: \.name)
122+
Field("friends", at: \.friends, as: [TypeReference<User>]?.self)
114123
}
115124

116125
Input(UserInput.self) {
117126
InputField("id", at: \.id)
118127
InputField("name", at: \.name)
128+
InputField("friends", at: \.friends, as: [TypeReference<UserInput>]?.self)
119129
}
120130

121131
Type(UserEvent.self) {
@@ -330,6 +340,41 @@ class HelloWorldTests : XCTestCase {
330340

331341
wait(for: [expectation], timeout: 10)
332342
}
343+
344+
func testInputRecursive() throws {
345+
let mutation = """
346+
mutation addUser($user: UserInput!) {
347+
addUser(user: $user) {
348+
id,
349+
name,
350+
friends {
351+
id,
352+
name
353+
}
354+
}
355+
}
356+
"""
357+
358+
let variables: [String: Map] = ["user" : [ "id" : "123", "name" : "bob", "friends": [["id": "124", "name": "jeff"]]]]
359+
360+
let expected = GraphQLResult(
361+
data: ["addUser" : [ "id" : "123", "name" : "bob", "friends": [["id": "124", "name": "jeff"]]]]
362+
)
363+
364+
let expectation = XCTestExpectation()
365+
366+
api.execute(
367+
request: mutation,
368+
context: api.context,
369+
on: group,
370+
variables: variables
371+
).whenSuccess { result in
372+
XCTAssertEqual(result, expected)
373+
expectation.fulfill()
374+
}
375+
376+
wait(for: [expectation], timeout: 10)
377+
}
333378
}
334379

335380
extension HelloWorldTests {

0 commit comments

Comments
 (0)