Skip to content

Commit 08db70f

Browse files
Tests for Partial schema
1 parent 35c399f commit 08db70f

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import Graphiti
2+
import GraphQL
3+
import NIO
4+
import XCTest
5+
6+
class PartialSchemaTests: XCTestCase {
7+
class BaseSchema: PartialSchema<StarWarsResolver, StarWarsContext> {
8+
@TypeDefinitions
9+
override var types: Types {
10+
Interface(Character.self) {
11+
Field("id", at: \.id)
12+
.description("The id of the character.")
13+
Field("name", at: \.name)
14+
.description("The name of the character.")
15+
Field("friends", at: \.friends, as: [TypeReference<Character>].self)
16+
.description(
17+
"The friends of the character, or an empty list if they have none."
18+
)
19+
Field("appearsIn", at: \.appearsIn)
20+
.description("Which movies they appear in.")
21+
Field("secretBackstory", at: \.secretBackstory)
22+
.description("All secrets about their past.")
23+
}
24+
25+
Enum(Episode.self) {
26+
Value(.newHope)
27+
.description("Released in 1977.")
28+
Value(.empire)
29+
.description("Released in 1980.")
30+
Value(.jedi)
31+
.description("Released in 1983.")
32+
}.description("One of the films in the Star Wars Trilogy.")
33+
}
34+
35+
@FieldDefinitions
36+
override var query: Fields {
37+
Field("hero", at: StarWarsResolver.hero, as: Character.self) {
38+
Argument("episode", at: \.episode)
39+
.description(
40+
"If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode."
41+
)
42+
}.description("Returns a hero based on the given episode.")
43+
}
44+
}
45+
46+
class SearchSchema: PartialSchema<StarWarsResolver, StarWarsContext> {
47+
@TypeDefinitions
48+
override var types: Types {
49+
Type(Planet.self) {
50+
Field("id", at: \.id)
51+
Field("name", at: \.name)
52+
Field("diameter", at: \.diameter)
53+
Field("rotationPeriod", at: \.rotationPeriod)
54+
Field("orbitalPeriod", at: \.orbitalPeriod)
55+
Field("residents", at: \.residents)
56+
}.description(
57+
"A large mass, planet or planetoid in the Star Wars Universe, at the time of 0 ABY."
58+
)
59+
Type(Human.self, interfaces: [Character.self]) {
60+
Field("id", at: \.id)
61+
Field("name", at: \.name)
62+
Field("appearsIn", at: \.appearsIn)
63+
Field("homePlanet", at: \.homePlanet)
64+
Field("friends", at: Human.getFriends, as: [Character].self)
65+
.description("The friends of the human, or an empty list if they have none.")
66+
Field("secretBackstory", at: Human.getSecretBackstory)
67+
.description("Where are they from and how they came to be who they are.")
68+
}.description("A humanoid creature in the Star Wars universe.")
69+
Type(Droid.self, interfaces: [Character.self]) {
70+
Field("id", at: \.id)
71+
Field("name", at: \.name)
72+
Field("appearsIn", at: \.appearsIn)
73+
Field("primaryFunction", at: \.primaryFunction)
74+
Field("friends", at: Droid.getFriends, as: [Character].self)
75+
.description("The friends of the droid, or an empty list if they have none.")
76+
Field("secretBackstory", at: Droid.getSecretBackstory)
77+
.description("Where are they from and how they came to be who they are.")
78+
}.description("A mechanical creature in the Star Wars universe.")
79+
Union(SearchResult.self, members: Planet.self, Human.self, Droid.self)
80+
}
81+
82+
@FieldDefinitions
83+
override var query: Fields {
84+
Field("human", at: StarWarsResolver.human) {
85+
Argument("id", at: \.id)
86+
.description("Id of the human.")
87+
}
88+
Field("droid", at: StarWarsResolver.droid) {
89+
Argument("id", at: \.id)
90+
.description("Id of the droid.")
91+
}
92+
Field("search", at: StarWarsResolver.search, as: [SearchResult].self) {
93+
Argument("query", at: \.query)
94+
.defaultValue("R2-D2")
95+
}
96+
}
97+
}
98+
99+
func testPartialSchemaWithBuilder() throws {
100+
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
101+
102+
let builder = SchemaBuilder(StarWarsResolver.self, StarWarsContext.self)
103+
104+
builder.use(partials: [BaseSchema(), SearchSchema()])
105+
106+
let schema = try builder.build()
107+
108+
struct PartialSchemaTestAPI: API {
109+
let resolver: StarWarsResolver
110+
let schema: Schema<StarWarsResolver, StarWarsContext>
111+
}
112+
113+
let api = PartialSchemaTestAPI(resolver: StarWarsResolver(), schema: schema)
114+
115+
XCTAssertEqual(
116+
try api.execute(
117+
request: """
118+
query {
119+
human(id: "1000") {
120+
name
121+
}
122+
}
123+
""",
124+
context: StarWarsContext(),
125+
on: group
126+
).wait(),
127+
GraphQLResult(data: [
128+
"human": [
129+
"name": "Luke Skywalker",
130+
],
131+
])
132+
)
133+
}
134+
135+
func testPartialSchema() throws {
136+
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
137+
138+
/// Double check if static func works and the types are inferred properly
139+
let schema = try Schema.create(from: [BaseSchema(), SearchSchema()])
140+
141+
struct PartialSchemaTestAPI: API {
142+
let resolver: StarWarsResolver
143+
let schema: Schema<StarWarsResolver, StarWarsContext>
144+
}
145+
146+
let api = PartialSchemaTestAPI(resolver: StarWarsResolver(), schema: schema)
147+
148+
XCTAssertEqual(
149+
try api.execute(
150+
request: """
151+
query {
152+
human(id: "1000") {
153+
name
154+
}
155+
}
156+
""",
157+
context: StarWarsContext(),
158+
on: group
159+
).wait(),
160+
GraphQLResult(data: [
161+
"human": [
162+
"name": "Luke Skywalker",
163+
],
164+
])
165+
)
166+
}
167+
}

0 commit comments

Comments
 (0)