diff --git a/.travis.yml b/.travis.yml index f745875f..a92eae9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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)" + diff --git a/Sources/Graphiti/Schema/Schema.swift b/Sources/Graphiti/Schema/Schema.swift index c3406ef2..16e063ce 100644 --- a/Sources/Graphiti/Schema/Schema.swift +++ b/Sources/Graphiti/Schema/Schema.swift @@ -91,6 +91,31 @@ public final class SchemaBuilder { map(Type.self, to: objectType) } + public func inputObject( + type: Type.Type, + build: (InputObjectTypeBuilder) throws -> Void + ) throws { + let name = fixName(String(describing: Type.self)) + try inputObject(name: name, type: type, build: build) + } + + public func inputObject( + name: String, + type: Type.Type, + build: (InputObjectTypeBuilder) throws -> Void + ) throws { + let builder = InputObjectTypeBuilder(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, build: (InterfaceTypeBuilder) throws -> Void diff --git a/Sources/Graphiti/Types/InputObjectType.swift b/Sources/Graphiti/Types/InputObjectType.swift new file mode 100644 index 00000000..5f613d2e --- /dev/null +++ b/Sources/Graphiti/Types/InputObjectType.swift @@ -0,0 +1,24 @@ +import GraphQL + +public final class InputObjectTypeBuilder { + var schema: SchemaBuilder + + init(schema: SchemaBuilder) { + 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 + } + } +} + diff --git a/Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift b/Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift index 4ce89cae..76355a38 100644 --- a/Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift +++ b/Tests/GraphitiTests/HelloWorldTests/HelloWorldTests.swift @@ -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 { 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 {