Skip to content

Commit c6d872a

Browse files
test: Makes HelloWorldServer example testable
1 parent a389c8c commit c6d872a

3 files changed

Lines changed: 97 additions & 48 deletions

File tree

Examples/HelloWorldServer/Package.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.0.0"),
1313
],
1414
targets: [
15-
.executableTarget(
15+
.target(
1616
name: "HelloWorldServer",
1717
dependencies: [
1818
.product(name: "GraphQL", package: "GraphQL"),
@@ -22,5 +22,12 @@ let package = Package(
2222
.plugin(name: "GraphQLGeneratorPlugin", package: "graphql-generator")
2323
]
2424
),
25+
.testTarget(
26+
name: "HelloWorldServerTests",
27+
dependencies: [
28+
"HelloWorldServer",
29+
.product(name: "GraphQL", package: "GraphQL"),
30+
]
31+
)
2532
]
2633
)

Examples/HelloWorldServer/Sources/HelloWorldServer/main.swift renamed to Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -113,50 +113,3 @@ struct Mutation: MutationProtocol {
113113
return user
114114
}
115115
}
116-
117-
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
118-
119-
let context = Context(
120-
users: ["1" : .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
121-
posts: ["1" : .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
122-
)
123-
print(
124-
try await graphql(
125-
schema: schema,
126-
request: """
127-
{
128-
posts {
129-
id
130-
title
131-
content
132-
author {
133-
id
134-
name
135-
email
136-
age
137-
role
138-
}
139-
}
140-
}
141-
""",
142-
context: context
143-
)
144-
)
145-
146-
print(
147-
try await graphql(
148-
schema: schema,
149-
request: """
150-
mutation {
151-
upsertUser(userInfo: {id: "2", name: "Jane", email: "jane@example.com"}) {
152-
id
153-
name
154-
email
155-
age
156-
role
157-
}
158-
}
159-
""",
160-
context: context
161-
)
162-
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import GraphQL
2+
import Testing
3+
4+
@testable import HelloWorldServer
5+
6+
@Suite
7+
struct HelloWorldServerTests {
8+
@Test func query() async throws {
9+
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
10+
let context = Context(
11+
users: ["1" : .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
12+
posts: ["1" : .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
13+
)
14+
#expect(
15+
try await graphql(
16+
schema: schema,
17+
request: """
18+
{
19+
posts {
20+
id
21+
title
22+
content
23+
author {
24+
id
25+
name
26+
email
27+
age
28+
role
29+
}
30+
}
31+
}
32+
""",
33+
context: context
34+
) == .init(
35+
data: [
36+
"posts": [
37+
[
38+
"id": "1",
39+
"title": "Foo",
40+
"content": "bar",
41+
"author": [
42+
"id": "1",
43+
"name": "John",
44+
"email": "john@example.com",
45+
"age": 18,
46+
"role": "USER"
47+
]
48+
]
49+
]
50+
]
51+
)
52+
)
53+
}
54+
55+
@Test func mutation() async throws {
56+
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
57+
let context = Context(
58+
users: [:],
59+
posts: [:]
60+
)
61+
#expect(
62+
try await graphql(
63+
schema: schema,
64+
request: """
65+
mutation {
66+
upsertUser(userInfo: {id: "2", name: "Jane", email: "jane@example.com"}) {
67+
id
68+
name
69+
email
70+
age
71+
role
72+
}
73+
}
74+
""",
75+
context: context
76+
) == .init(
77+
data: [
78+
"upsertUser": [
79+
"id": "2",
80+
"name": "Jane",
81+
"email": "jane@example.com",
82+
"age": nil,
83+
"role": nil
84+
]
85+
]
86+
)
87+
)
88+
}
89+
}

0 commit comments

Comments
 (0)