Skip to content

Commit 5ddeef6

Browse files
sportlabsMikepaulofaria
authored andcommitted
Add support for adding basic input types to the schema via an InputOb… (#11)
* Add support for adding basic input types to the schema via an InputObjectTypeBuilder * Update .travis.yml based on GraphQLSwift/GraphQL setup
1 parent 5db60d7 commit 5ddeef6

File tree

4 files changed

+131
-11
lines changed

4 files changed

+131
-11
lines changed

.travis.yml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
notifications:
22
slack: zewo:VjyVCCQvTOw9yrbzQysZezD1
3-
os:
4-
- linux
5-
- osx
63
language: generic
7-
sudo: required
8-
dist: trusty
9-
osx_image: xcode8
10-
install:
11-
- eval "$(curl -sL https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/install.sh)"
4+
matrix:
5+
include:
6+
- os: osx
7+
env: JOB=SwiftPM_OSX
8+
osx_image: xcode8.3
9+
- os: linux
10+
env: JOB=SwiftPM_linux
11+
dist: trusty
12+
sudo: required
13+
install:
14+
- travis_retry eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
1215
script:
13-
- bash <(curl -s https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/build-test.sh) Graphiti
14-
#after_success:
15-
- bash <(curl -s https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/report-coverage.sh)
16+
- swift build -c release
17+
- swift build
18+
- swift test
19+
- eval "$(curl -sL https://raw.githubusercontent.com/lgaches/swifttravisci/master/codecov)"
20+

Sources/Graphiti/Schema/Schema.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ public final class SchemaBuilder<Root, Context> {
9191
map(Type.self, to: objectType)
9292
}
9393

94+
public func inputObject<Type: InputType>(
95+
type: Type.Type,
96+
build: (InputObjectTypeBuilder<Root, Context, Type>) throws -> Void
97+
) throws {
98+
let name = fixName(String(describing: Type.self))
99+
try inputObject(name: name, type: type, build: build)
100+
}
101+
102+
public func inputObject<Type: InputType>(
103+
name: String,
104+
type: Type.Type,
105+
build: (InputObjectTypeBuilder<Root, Context, Type>) throws -> Void
106+
) throws {
107+
let builder = InputObjectTypeBuilder<Root, Context, Type>(schema: self)
108+
try build(builder)
109+
110+
let inputObjectType = try GraphQLInputObjectType(
111+
name: name,
112+
description: builder.description,
113+
fields: builder.fields
114+
)
115+
116+
map(Type.self, to: inputObjectType)
117+
}
118+
94119
public func interface<Type>(
95120
type: Type.Type,
96121
build: (InterfaceTypeBuilder<Root, Context, Type>) throws -> Void
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import GraphQL
2+
3+
public final class InputObjectTypeBuilder<Root, Context, Type> {
4+
var schema: SchemaBuilder<Root, Context>
5+
6+
init(schema: SchemaBuilder<Root, Context>) {
7+
self.schema = schema
8+
}
9+
10+
public var description: String? = nil
11+
12+
var fields: InputObjectConfigFieldMap = [:]
13+
14+
/// Export all properties using reflection
15+
///
16+
/// - Throws: Reflection Errors
17+
public func exportFields() throws {
18+
for property in try properties(Type.self) {
19+
let field = InputObjectField(type: try schema.getInputType(from: property.type, field: property.key))
20+
fields[property.key] = field
21+
}
22+
}
23+
}
24+

Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,72 @@ class HelloWorldTests : XCTestCase {
101101
result = try schema.execute(request: query)
102102
XCTAssertEqual(result, expected)
103103
}
104+
105+
func testInput() throws {
106+
107+
struct Foo : OutputType {
108+
let id: String
109+
let name : String?
110+
111+
static func fromInput(_ input: FooInput) -> Foo {
112+
return Foo(id: input.id, name: input.name)
113+
}
114+
}
115+
116+
struct FooInput : InputType {
117+
let id: String
118+
let name : String?
119+
}
120+
121+
let schema = try Schema<NoRoot, NoContext> { schema in
122+
123+
try schema.object(type: Foo.self) { builder in
124+
125+
try builder.exportFields()
126+
}
127+
128+
try schema.query { query in
129+
130+
try query.field(name: "foo", type: (Foo?).self) { (_,_,_,_) in
131+
132+
return Foo(id: "123", name: "bar")
133+
}
134+
}
135+
136+
try schema.inputObject(type: FooInput.self) { builder in
137+
138+
try builder.exportFields()
139+
}
140+
141+
struct AddFooArguments : Arguments {
142+
143+
let input: FooInput
144+
}
145+
146+
try schema.mutation { mutation in
147+
148+
try mutation.field(name: "addFoo", type: Foo.self) { (_, arguments: AddFooArguments, _, _) in
149+
150+
debugPrint(arguments)
151+
return Foo.fromInput(arguments.input)
152+
}
153+
}
154+
155+
}
156+
157+
let mutation = "mutation addFoo($input: FooInput!) { addFoo(input:$input) { id, name } }"
158+
let variables: [String:Map] = ["input" : [ "id" : "123", "name" : "bob" ]]
159+
let expected: Map = ["data": ["addFoo" : [ "id" : "123", "name" : "bob" ]]]
160+
do {
161+
let result = try schema.execute(request: mutation, variables: variables)
162+
XCTAssertEqual(result, expected)
163+
debugPrint(result)
164+
}
165+
catch {
166+
debugPrint(error)
167+
}
168+
169+
}
104170
}
105171

106172
extension HelloWorldTests {

0 commit comments

Comments
 (0)