Skip to content

Commit

Permalink
Add support for adding basic input types to the schema via an InputOb… (
Browse files Browse the repository at this point in the history
#11)

* Add support for adding basic input types to the schema via an InputObjectTypeBuilder

* Update .travis.yml based on GraphQLSwift/GraphQL setup
  • Loading branch information
sportlabsMike authored and paulofaria committed Aug 12, 2017
1 parent 5db60d7 commit 5ddeef6
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 11 deletions.
27 changes: 16 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
notifications:
slack: zewo:VjyVCCQvTOw9yrbzQysZezD1
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode8
install:
- eval "$(curl -sL https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/install.sh)"
matrix:
include:
- os: osx
env: JOB=SwiftPM_OSX
osx_image: xcode8.3
- os: linux
env: JOB=SwiftPM_linux
dist: trusty
sudo: required
install:
- travis_retry eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
script:
- bash <(curl -s https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/build-test.sh) Graphiti
#after_success:
- bash <(curl -s https://raw.githubusercontent.com/Zewo/Zewo/master/Scripts/Travis/report-coverage.sh)
- swift build -c release
- swift build
- swift test
- eval "$(curl -sL https://raw.githubusercontent.com/lgaches/swifttravisci/master/codecov)"

25 changes: 25 additions & 0 deletions Sources/Graphiti/Schema/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ public final class SchemaBuilder<Root, Context> {
map(Type.self, to: objectType)
}

public func inputObject<Type: InputType>(
type: Type.Type,
build: (InputObjectTypeBuilder<Root, Context, Type>) throws -> Void
) throws {
let name = fixName(String(describing: Type.self))
try inputObject(name: name, type: type, build: build)
}

public func inputObject<Type: InputType>(
name: String,
type: Type.Type,
build: (InputObjectTypeBuilder<Root, Context, Type>) throws -> Void
) throws {
let builder = InputObjectTypeBuilder<Root, Context, Type>(schema: self)
try build(builder)

let inputObjectType = try GraphQLInputObjectType(
name: name,
description: builder.description,
fields: builder.fields
)

map(Type.self, to: inputObjectType)
}

public func interface<Type>(
type: Type.Type,
build: (InterfaceTypeBuilder<Root, Context, Type>) throws -> Void
Expand Down
24 changes: 24 additions & 0 deletions Sources/Graphiti/Types/InputObjectType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import GraphQL

public final class InputObjectTypeBuilder<Root, Context, Type> {
var schema: SchemaBuilder<Root, Context>

init(schema: SchemaBuilder<Root, Context>) {
self.schema = schema
}

public var description: String? = nil

var fields: InputObjectConfigFieldMap = [:]

/// Export all properties using reflection
///
/// - Throws: Reflection Errors
public func exportFields() throws {
for property in try properties(Type.self) {
let field = InputObjectField(type: try schema.getInputType(from: property.type, field: property.key))
fields[property.key] = field
}
}
}

66 changes: 66 additions & 0 deletions Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,72 @@ class HelloWorldTests : XCTestCase {
result = try schema.execute(request: query)
XCTAssertEqual(result, expected)
}

func testInput() throws {

struct Foo : OutputType {
let id: String
let name : String?

static func fromInput(_ input: FooInput) -> Foo {
return Foo(id: input.id, name: input.name)
}
}

struct FooInput : InputType {
let id: String
let name : String?
}

let schema = try Schema<NoRoot, NoContext> { schema in

try schema.object(type: Foo.self) { builder in

try builder.exportFields()
}

try schema.query { query in

try query.field(name: "foo", type: (Foo?).self) { (_,_,_,_) in

return Foo(id: "123", name: "bar")
}
}

try schema.inputObject(type: FooInput.self) { builder in

try builder.exportFields()
}

struct AddFooArguments : Arguments {

let input: FooInput
}

try schema.mutation { mutation in

try mutation.field(name: "addFoo", type: Foo.self) { (_, arguments: AddFooArguments, _, _) in

debugPrint(arguments)
return Foo.fromInput(arguments.input)
}
}

}

let mutation = "mutation addFoo($input: FooInput!) { addFoo(input:$input) { id, name } }"
let variables: [String:Map] = ["input" : [ "id" : "123", "name" : "bob" ]]
let expected: Map = ["data": ["addFoo" : [ "id" : "123", "name" : "bob" ]]]
do {
let result = try schema.execute(request: mutation, variables: variables)
XCTAssertEqual(result, expected)
debugPrint(result)
}
catch {
debugPrint(error)
}

}
}

extension HelloWorldTests {
Expand Down

0 comments on commit 5ddeef6

Please sign in to comment.