From 4b50a6d6b61b463debbce9611fd409f1879908f5 Mon Sep 17 00:00:00 2001 From: Hugo Cuvillier Date: Sun, 16 Apr 2017 00:14:49 +1000 Subject: [PATCH] Fixes wrapped types definition (#7) * Fixes wrapped types definition * Fixes the schema definition in comments --- Sources/Graphiti/Schema/Schema.swift | 47 +++++++++---------- .../StarWarsIntrospectionTests.swift | 12 ++--- .../StarWarsTests/StarWarsSchema.swift | 26 +++++----- 3 files changed, 40 insertions(+), 45 deletions(-) diff --git a/Sources/Graphiti/Schema/Schema.swift b/Sources/Graphiti/Schema/Schema.swift index 5ebc6c03..cf0076b6 100644 --- a/Sources/Graphiti/Schema/Schema.swift +++ b/Sources/Graphiti/Schema/Schema.swift @@ -205,41 +205,36 @@ extension SchemaBuilder { return try types.map({ try getNamedType(from: $0) }) } - func getGraphQLType(from type: Any.Type) -> GraphQLType? { + static private func getGraphQLOptionalType(from type: GraphQLType, isOptional: Bool) -> GraphQLType? { + if isOptional { + return type + } else if let type = type as? GraphQLNullableType { + return GraphQLNonNull(type) + } else { + // TODO: Throw error + return nil + } + } + + func getGraphQLType(from type: Any.Type, isOptional: Bool = false) -> GraphQLType? { if let type = type as? Wrapper.Type { switch type.modifier { case .optional: - if let wrapper = type.wrappedType as? Wrapper.Type { - if case .reference = wrapper.modifier { - let name = fixName(String(describing: wrapper.wrappedType)) - return GraphQLTypeReference(name) - } else { - return getGraphQLType(from: type.wrappedType) - } - } else { - return graphQLTypeMap[AnyType(type.wrappedType)] - } + return getGraphQLType(from: type.wrappedType, isOptional: true) case .list: - if type.wrappedType is Wrapper.Type { - let unwrapped = getGraphQLType(from: type.wrappedType) - return unwrapped.map { GraphQLList($0) } - } else { - let unwrapped = graphQLTypeMap[AnyType(type.wrappedType)] - // TODO: check if it's nullable and throw error - return unwrapped.map { GraphQLList(GraphQLNonNull($0 as! GraphQLNullableType)) } + return getGraphQLType(from: type.wrappedType).flatMap { + SchemaBuilder.getGraphQLOptionalType(from: GraphQLList($0), isOptional: isOptional) } case .reference: let name = fixName(String(describing: type.wrappedType)) - return GraphQLNonNull(GraphQLTypeReference(name)) - } - } + let referenceType = GraphQLTypeReference(name) - return graphQLTypeMap[AnyType(type)].flatMap { - guard let nullable = $0 as? GraphQLNullableType else { - return nil + return SchemaBuilder.getGraphQLOptionalType(from: referenceType, isOptional: isOptional) + } + } else { + return graphQLTypeMap[AnyType(type)].flatMap { + SchemaBuilder.getGraphQLOptionalType(from: $0, isOptional: isOptional) } - - return GraphQLNonNull(nullable) } } diff --git a/Tests/GraphitiTests/StarWarsTests/StarWarsIntrospectionTests.swift b/Tests/GraphitiTests/StarWarsTests/StarWarsIntrospectionTests.swift index ddde1870..51f934b8 100644 --- a/Tests/GraphitiTests/StarWarsTests/StarWarsIntrospectionTests.swift +++ b/Tests/GraphitiTests/StarWarsTests/StarWarsIntrospectionTests.swift @@ -245,14 +245,14 @@ class StarWarsIntrospectionTests : XCTestCase { "name": "appearsIn", "type": [ "name": nil, - "kind": "LIST", + "kind": "NON_NULL", ], ], [ "name": "friends", "type": [ "name": nil, - "kind": "LIST", + "kind": "NON_NULL", ], ], [ @@ -319,9 +319,9 @@ class StarWarsIntrospectionTests : XCTestCase { "name": "appearsIn", "type": [ "name": nil, - "kind": "LIST", + "kind": "NON_NULL", "ofType": [ - "kind": "NON_NULL", + "kind": "LIST", "name": nil ] ], @@ -330,9 +330,9 @@ class StarWarsIntrospectionTests : XCTestCase { "name": "friends", "type": [ "name": nil, - "kind": "LIST", + "kind": "NON_NULL", "ofType": [ - "kind": "NON_NULL", + "kind": "LIST", "name": nil ] ], diff --git a/Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift b/Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift index 2185443c..44eafc2f 100644 --- a/Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift +++ b/Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift @@ -41,16 +41,16 @@ extension Planet: OutputType {} * interface Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * } * * type Human : Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * homePlanet: String! * } @@ -58,8 +58,8 @@ extension Planet: OutputType {} * type Droid : Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * primaryFunction: String! * } @@ -111,8 +111,8 @@ let starWarsSchema = try! Schema { schema in * interface Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * } */ @@ -161,7 +161,7 @@ let starWarsSchema = try! Schema { schema in * diameter: Int! * rotationPeriod: Int! * orbitalPeriod: Int! - * residents: [Human!] + * residents: [Human!]! * } */ try schema.object(type: Planet.self) { planet in @@ -184,8 +184,8 @@ let starWarsSchema = try! Schema { schema in * type Human : Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * homePlanet: Planet! * } @@ -222,8 +222,8 @@ let starWarsSchema = try! Schema { schema in * type Droid : Character { * id: String! * name: String! - * friends: [Character!] - * appearsIn: [Episode!] + * friends: [Character!]! + * appearsIn: [Episode!]! * secretBackstory: String * primaryFunction: String! * }