Skip to content

Commit b2221fe

Browse files
refactor: Rename Context to GraphQLContext
1 parent 7fe554a commit b2221fe

7 files changed

Lines changed: 59 additions & 59 deletions

File tree

Examples/HelloWorldServer/Sources/HelloWorldServer/Resolvers.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Foundation
22
import GraphQL
33
import GraphQLGeneratorRuntime
44

5-
// Must be created by user and named `Context`.
6-
public class Context: @unchecked Sendable {
5+
// Must be created by user and named `GraphQLContext`.
6+
public class GraphQLContext: @unchecked Sendable {
77
// User can choose structure
88
var users: [String: User]
99
var posts: [String: Post]
@@ -80,23 +80,23 @@ struct User: UserProtocol {
8080
let role: Role?
8181

8282
// Required implementations
83-
func id(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
83+
func id(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
8484
return id
8585
}
8686

87-
func name(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
87+
func name(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
8888
return name
8989
}
9090

91-
func email(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
91+
func email(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
9292
return EmailAddress(email: email)
9393
}
9494

95-
func age(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> Int? {
95+
func age(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> Int? {
9696
return age
9797
}
9898

99-
func role(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> Role? {
99+
func role(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> Role? {
100100
return role
101101
}
102102
}
@@ -106,7 +106,7 @@ struct Contact: ContactProtocol {
106106
let email: String
107107

108108
// Required implementations
109-
func email(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
109+
func email(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> EmailAddress {
110110
return EmailAddress(email: email)
111111
}
112112
}
@@ -119,49 +119,49 @@ struct Post: PostProtocol {
119119
let authorId: String
120120

121121
// Required implementations
122-
func id(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
122+
func id(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
123123
return id
124124
}
125125

126-
func title(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
126+
func title(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
127127
return title
128128
}
129129

130-
func content(context _: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
130+
func content(context _: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> String {
131131
return content
132132
}
133133

134-
func author(context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> any UserProtocol {
134+
func author(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> any UserProtocol {
135135
return context.users[authorId]!
136136
}
137137
}
138138

139139
struct Query: QueryProtocol {
140140
// Required implementations
141-
static func user(id: String, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> (any UserProtocol)? {
141+
static func user(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any UserProtocol)? {
142142
return context.users[id]
143143
}
144144

145-
static func users(context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> [any UserProtocol] {
145+
static func users(context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any UserProtocol] {
146146
return context.users.values.map { $0 as any UserProtocol }
147147
}
148148

149-
static func post(id: String, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> (any PostProtocol)? {
149+
static func post(id: String, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> (any PostProtocol)? {
150150
return context.posts[id]
151151
}
152152

153-
static func posts(limit _: Int?, context: Context, info _: GraphQL.GraphQLResolveInfo) async throws -> [any PostProtocol] {
153+
static func posts(limit _: Int?, context: GraphQLContext, info _: GraphQL.GraphQLResolveInfo) async throws -> [any PostProtocol] {
154154
return context.posts.values.map { $0 as any PostProtocol }
155155
}
156156

157-
static func userOrPost(id: String, context: Context, info _: GraphQLResolveInfo) async throws -> (any UserOrPostUnion)? {
157+
static func userOrPost(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> (any UserOrPostUnion)? {
158158
return context.users[id] ?? context.posts[id]
159159
}
160160
}
161161

162162
struct Mutation: MutationProtocol {
163163
// Required implementations
164-
static func upsertUser(userInfo: UserInfoInput, context: Context, info _: GraphQLResolveInfo) -> any UserProtocol {
164+
static func upsertUser(userInfo: UserInfoInput, context: GraphQLContext, info _: GraphQLResolveInfo) -> any UserProtocol {
165165
let user = User(
166166
id: userInfo.id,
167167
name: userInfo.name,
@@ -176,7 +176,7 @@ struct Mutation: MutationProtocol {
176176

177177
struct Subscription: SubscriptionProtocol {
178178
// Required implementations
179-
static func watchUser(id: String, context: Context, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any UserProtocol)?> {
179+
static func watchUser(id: String, context: GraphQLContext, info _: GraphQLResolveInfo) async throws -> AnyAsyncSequence<(any UserProtocol)?> {
180180
return AsyncStream<(any UserProtocol)?> { continuation in
181181
context.onTriggerWatch = { [weak context] in
182182
continuation.yield(context?.users[id])

Examples/HelloWorldServer/Tests/HelloWorldServerTests/HelloWorldServerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Testing
77
struct HelloWorldServerTests {
88
@Test func query() async throws {
99
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
10-
let context = Context(
10+
let context = GraphQLContext(
1111
users: ["1": .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
1212
posts: ["1": .init(id: "1", title: "Foo", content: "bar", authorId: "1")]
1313
)
@@ -54,7 +54,7 @@ struct HelloWorldServerTests {
5454

5555
@Test func mutation() async throws {
5656
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
57-
let context = Context(
57+
let context = GraphQLContext(
5858
users: [:],
5959
posts: [:]
6060
)
@@ -89,7 +89,7 @@ struct HelloWorldServerTests {
8989

9090
@Test func subscription() async throws {
9191
let schema = try buildGraphQLSchema(resolvers: Resolvers.self)
92-
let context = Context(
92+
let context = GraphQLContext(
9393
users: ["1": .init(id: "1", name: "John", email: "john@example.com", age: 18, role: .user)],
9494
posts: [:]
9595
)

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ When you build, the plugin will automatically generate Swift code:
5959

6060
### 3. Create required types
6161

62-
Create a type named `Context`:
62+
Create a type named `GraphQLContext`:
6363

6464
```swift
65-
public actor Context {
65+
public actor GraphQLContext {
6666
// Add any features you like
6767
}
6868
```
@@ -83,7 +83,7 @@ As you build the `Query`, `Mutation`, and `Subscription` types and their resolut
8383
```swift
8484
struct Query: QueryProtocol {
8585
// This is required by `QueryProtocol`, and used by GraphQL query resolution.
86-
static func user(context: Context, info: GraphQLResolveInfo) async throws -> (any UserProtocol)? {
86+
static func user(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> (any UserProtocol)? {
8787
// You can implement resolution logic however you like.
8888
return context.user
8989
}
@@ -95,10 +95,10 @@ struct User: UserProtocol {
9595
let email: String
9696

9797
// These are required by `UserProtocol`, and used by GraphQL field resolution.
98-
func name(context: Context, info: GraphQLResolveInfo) async throws -> String {
98+
func name(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
9999
return name
100100
}
101-
func email(context: Context, info: GraphQLResolveInfo) async throws -> EmailAddress {
101+
func email(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> EmailAddress {
102102
// You can implement resolution logic however you like.
103103
return EmailAddress(email: self.email)
104104
}
@@ -135,20 +135,20 @@ type A {
135135
This would result in the following protocol:
136136
```swift
137137
public protocol AProtocol: Sendable {
138-
func foo(context: Context, info: GraphQLResolveInfo) async throws -> String
138+
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String
139139
}
140140
```
141141

142142
You could define two conforming types. To use `ATest` in tests, simply return it from the relevant resolvers.
143143
```swift
144144
struct A: AProtocol {
145145
let foo: String
146-
func foo(context: Context, info: GraphQLResolveInfo) async throws -> String {
146+
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
147147
return foo
148148
}
149149
}
150150
struct ATest: AProtocol {
151-
func foo(context: Context, info: GraphQLResolveInfo) async throws -> String {
151+
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
152152
return "test"
153153
}
154154
}

Sources/GraphQLGeneratorCore/Generator/SchemaGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ package struct SchemaGenerator {
739739
// Add context
740740
output += """
741741
742-
let context = try cast(context, to: Context.self)
742+
let context = try cast(context, to: GraphQLContext.self)
743743
"""
744744
argsList.append("context: context")
745745

Sources/GraphQLGeneratorCore/Generator/TypeGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ package struct TypeGenerator {
280280
}
281281

282282
// Add context parameter
283-
params.append("context: Context")
283+
params.append("context: GraphQLContext")
284284

285285
// Add resolve info parameter
286286
params.append("info: GraphQLResolveInfo")
@@ -348,7 +348,7 @@ package struct TypeGenerator {
348348
}
349349

350350
// Add context parameter
351-
params.append("context: Context")
351+
params.append("context: GraphQLContext")
352352

353353
// Add resolve info parameter
354354
params.append("info: GraphQLResolveInfo")
@@ -413,7 +413,7 @@ package struct TypeGenerator {
413413
}
414414

415415
// Add context parameter
416-
params.append("context: Context")
416+
params.append("context: GraphQLContext")
417417

418418
// Add resolve info parameter
419419
params.append("info: GraphQLResolveInfo")

0 commit comments

Comments
 (0)