Skip to content

Commit 4b50a6d

Browse files
cuvapaulofaria
authored andcommitted
Fixes wrapped types definition (#7)
* Fixes wrapped types definition * Fixes the schema definition in comments
1 parent aa9d909 commit 4b50a6d

File tree

3 files changed

+40
-45
lines changed

3 files changed

+40
-45
lines changed

Sources/Graphiti/Schema/Schema.swift

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -205,41 +205,36 @@ extension SchemaBuilder {
205205
return try types.map({ try getNamedType(from: $0) })
206206
}
207207

208-
func getGraphQLType(from type: Any.Type) -> GraphQLType? {
208+
static private func getGraphQLOptionalType(from type: GraphQLType, isOptional: Bool) -> GraphQLType? {
209+
if isOptional {
210+
return type
211+
} else if let type = type as? GraphQLNullableType {
212+
return GraphQLNonNull(type)
213+
} else {
214+
// TODO: Throw error
215+
return nil
216+
}
217+
}
218+
219+
func getGraphQLType(from type: Any.Type, isOptional: Bool = false) -> GraphQLType? {
209220
if let type = type as? Wrapper.Type {
210221
switch type.modifier {
211222
case .optional:
212-
if let wrapper = type.wrappedType as? Wrapper.Type {
213-
if case .reference = wrapper.modifier {
214-
let name = fixName(String(describing: wrapper.wrappedType))
215-
return GraphQLTypeReference(name)
216-
} else {
217-
return getGraphQLType(from: type.wrappedType)
218-
}
219-
} else {
220-
return graphQLTypeMap[AnyType(type.wrappedType)]
221-
}
223+
return getGraphQLType(from: type.wrappedType, isOptional: true)
222224
case .list:
223-
if type.wrappedType is Wrapper.Type {
224-
let unwrapped = getGraphQLType(from: type.wrappedType)
225-
return unwrapped.map { GraphQLList($0) }
226-
} else {
227-
let unwrapped = graphQLTypeMap[AnyType(type.wrappedType)]
228-
// TODO: check if it's nullable and throw error
229-
return unwrapped.map { GraphQLList(GraphQLNonNull($0 as! GraphQLNullableType)) }
225+
return getGraphQLType(from: type.wrappedType).flatMap {
226+
SchemaBuilder.getGraphQLOptionalType(from: GraphQLList($0), isOptional: isOptional)
230227
}
231228
case .reference:
232229
let name = fixName(String(describing: type.wrappedType))
233-
return GraphQLNonNull(GraphQLTypeReference(name))
234-
}
235-
}
230+
let referenceType = GraphQLTypeReference(name)
236231

237-
return graphQLTypeMap[AnyType(type)].flatMap {
238-
guard let nullable = $0 as? GraphQLNullableType else {
239-
return nil
232+
return SchemaBuilder.getGraphQLOptionalType(from: referenceType, isOptional: isOptional)
233+
}
234+
} else {
235+
return graphQLTypeMap[AnyType(type)].flatMap {
236+
SchemaBuilder.getGraphQLOptionalType(from: $0, isOptional: isOptional)
240237
}
241-
242-
return GraphQLNonNull(nullable)
243238
}
244239
}
245240

Tests/GraphitiTests/StarWarsTests/StarWarsIntrospectionTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ class StarWarsIntrospectionTests : XCTestCase {
245245
"name": "appearsIn",
246246
"type": [
247247
"name": nil,
248-
"kind": "LIST",
248+
"kind": "NON_NULL",
249249
],
250250
],
251251
[
252252
"name": "friends",
253253
"type": [
254254
"name": nil,
255-
"kind": "LIST",
255+
"kind": "NON_NULL",
256256
],
257257
],
258258
[
@@ -319,9 +319,9 @@ class StarWarsIntrospectionTests : XCTestCase {
319319
"name": "appearsIn",
320320
"type": [
321321
"name": nil,
322-
"kind": "LIST",
322+
"kind": "NON_NULL",
323323
"ofType": [
324-
"kind": "NON_NULL",
324+
"kind": "LIST",
325325
"name": nil
326326
]
327327
],
@@ -330,9 +330,9 @@ class StarWarsIntrospectionTests : XCTestCase {
330330
"name": "friends",
331331
"type": [
332332
"name": nil,
333-
"kind": "LIST",
333+
"kind": "NON_NULL",
334334
"ofType": [
335-
"kind": "NON_NULL",
335+
"kind": "LIST",
336336
"name": nil
337337
]
338338
],

Tests/GraphitiTests/StarWarsTests/StarWarsSchema.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ extension Planet: OutputType {}
4141
* interface Character {
4242
* id: String!
4343
* name: String!
44-
* friends: [Character!]
45-
* appearsIn: [Episode!]
44+
* friends: [Character!]!
45+
* appearsIn: [Episode!]!
4646
* secretBackstory: String
4747
* }
4848
*
4949
* type Human : Character {
5050
* id: String!
5151
* name: String!
52-
* friends: [Character!]
53-
* appearsIn: [Episode!]
52+
* friends: [Character!]!
53+
* appearsIn: [Episode!]!
5454
* secretBackstory: String
5555
* homePlanet: String!
5656
* }
5757
*
5858
* type Droid : Character {
5959
* id: String!
6060
* name: String!
61-
* friends: [Character!]
62-
* appearsIn: [Episode!]
61+
* friends: [Character!]!
62+
* appearsIn: [Episode!]!
6363
* secretBackstory: String
6464
* primaryFunction: String!
6565
* }
@@ -111,8 +111,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
111111
* interface Character {
112112
* id: String!
113113
* name: String!
114-
* friends: [Character!]
115-
* appearsIn: [Episode!]
114+
* friends: [Character!]!
115+
* appearsIn: [Episode!]!
116116
* secretBackstory: String
117117
* }
118118
*/
@@ -161,7 +161,7 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
161161
* diameter: Int!
162162
* rotationPeriod: Int!
163163
* orbitalPeriod: Int!
164-
* residents: [Human!]
164+
* residents: [Human!]!
165165
* }
166166
*/
167167
try schema.object(type: Planet.self) { planet in
@@ -184,8 +184,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
184184
* type Human : Character {
185185
* id: String!
186186
* name: String!
187-
* friends: [Character!]
188-
* appearsIn: [Episode!]
187+
* friends: [Character!]!
188+
* appearsIn: [Episode!]!
189189
* secretBackstory: String
190190
* homePlanet: Planet!
191191
* }
@@ -222,8 +222,8 @@ let starWarsSchema = try! Schema<NoRoot, NoContext> { schema in
222222
* type Droid : Character {
223223
* id: String!
224224
* name: String!
225-
* friends: [Character!]
226-
* appearsIn: [Episode!]
225+
* friends: [Character!]!
226+
* appearsIn: [Episode!]!
227227
* secretBackstory: String
228228
* primaryFunction: String!
229229
* }

0 commit comments

Comments
 (0)