Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions Sources/Graphiti/Schema/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
],
[
Expand Down Expand Up @@ -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
]
],
Expand All @@ -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
]
],
Expand Down
26 changes: 13 additions & 13 deletions Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ 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!
* }
*
* type Droid : Character {
* id: String!
* name: String!
* friends: [Character!]
* appearsIn: [Episode!]
* friends: [Character!]!
* appearsIn: [Episode!]!
* secretBackstory: String
* primaryFunction: String!
* }
Expand Down Expand Up @@ -111,8 +111,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
* interface Character {
* id: String!
* name: String!
* friends: [Character!]
* appearsIn: [Episode!]
* friends: [Character!]!
* appearsIn: [Episode!]!
* secretBackstory: String
* }
*/
Expand Down Expand Up @@ -161,7 +161,7 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
* diameter: Int!
* rotationPeriod: Int!
* orbitalPeriod: Int!
* residents: [Human!]
* residents: [Human!]!
* }
*/
try schema.object(type: Planet.self) { planet in
Expand All @@ -184,8 +184,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
* type Human : Character {
* id: String!
* name: String!
* friends: [Character!]
* appearsIn: [Episode!]
* friends: [Character!]!
* appearsIn: [Episode!]!
* secretBackstory: String
* homePlanet: Planet!
* }
Expand Down Expand Up @@ -222,8 +222,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
* type Droid : Character {
* id: String!
* name: String!
* friends: [Character!]
* appearsIn: [Episode!]
* friends: [Character!]!
* appearsIn: [Episode!]!
* secretBackstory: String
* primaryFunction: String!
* }
Expand Down