Skip to content

Commit aab0eb9

Browse files
chore: Formatting & README
1 parent b05cb27 commit aab0eb9

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

Examples/GraphQLDotOrg/Sources/GraphQLDotOrg/GraphQLDotOrg.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Pacakge must define `GraphQLContext` type
2-
actor GraphQLContext { }
2+
actor GraphQLContext {}
33

44
// The rest of the types should be implemented:
55

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Query {
5454
### 2. Build Your Project
5555

5656
When you build, the plugin will automatically generate Swift code:
57-
- `Types.swift` - Swift protocols for your GraphQL types
57+
- `Types.swift` - Swift protocols and types for your GraphQL types. These are all namespaced within `GraphQLGenerated`.
5858
- `Schema.swift` - Defines `buildGraphQLSchema` function that builds an executable schema
5959

6060
### 3. Create required types
@@ -81,20 +81,20 @@ struct Resolvers: ResolversProtocol {
8181
As you build the `Query`, `Mutation`, and `Subscription` types and their resolution logic, you will be forced to define a concrete type for every reachable GraphQL result, according to its generated protocol:
8282

8383
```swift
84-
struct Query: QueryProtocol {
85-
// This is required by `QueryProtocol`, and used by GraphQL query resolution.
84+
struct Query: GraphQLGenerated.Query {
85+
// This is required by `GraphQLGenerated.Query`, and used by GraphQL query resolution.
8686
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
}
9090
}
9191

92-
struct User: UserProtocol {
92+
struct User: GraphQLGenerated.User {
9393
// You can define the type internals however you like
9494
let name: String
9595
let email: String
9696

97-
// These are required by `UserProtocol`, and used by GraphQL field resolution.
97+
// These are required by `GraphQLGenerated.User`, and used by GraphQL field resolution.
9898
func name(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
9999
return name
100100
}
@@ -119,6 +119,8 @@ print(result)
119119

120120
## Design
121121

122+
All generated types other than `GraphQLContext` and scalar types are namespaced inside of `GraphQLGenerated` to minimize polluting the inheriting package's type namespace.
123+
122124
### Root Types
123125
Root types (Query, Mutation, and Subscription) are modeled as Swift protocols with static method requirements for each field. The user must implement these types and provide them to the `buildGraphQLSchema` function.
124126

@@ -134,26 +136,27 @@ type A {
134136

135137
This would result in the following protocol:
136138
```swift
137-
public protocol AProtocol: Sendable {
139+
public protocol A: Sendable {
138140
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String
139141
}
140142
```
141143

142144
You could define two conforming types. To use `ATest` in tests, simply return it from the relevant resolvers.
143145
```swift
144-
struct A: AProtocol {
146+
struct A: GraphQLGenerated.A {
145147
let foo: String
146148
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
147149
return foo
148150
}
149151
}
150-
struct ATest: AProtocol {
152+
struct ATest: GraphQLGenerated.A {
151153
func foo(context: GraphQLContext, info: GraphQLResolveInfo) async throws -> String {
152154
return "test"
153155
}
154156
}
155157
```
156158

159+
Also, by not providing default resolvers based on reflection of the type's properties, we improve performance and setup the inheriting package up well for evolving the schema over time.
157160

158161
### Interface Types
159162
Interfaces are modeled as a protocol with required methods for each relevant field. Implementing objects and interfaces are marked as requiring conformance to the interface protocol.
@@ -214,10 +217,8 @@ public struct EmailAddress: Scalar {
214217

215218
## Development Roadmap
216219

217-
1. Generated Object namespacing: Especially for Input types, which are commonly suffixed with `Input`, creating `XInputInput` structs. Also, we should minimize interference with the user's namespace as much as possible.
218-
2. Directives: Directives are currently not supported
219-
3. Enhanced configuration: There should be configuration options for the build plugin itself
220-
4. Executable Schema: To work around the immutability of some Schema components, we generate Swift code to fully recreate the defined schema. Instead, we could just add resolver logic to the schema parsed from the `.graphql` file SDL.
220+
1. Directives: Directives are currently not supported
221+
2. Executable Schema: To work around the immutability of some Schema components, we generate Swift code to fully recreate the defined schema. Instead, we could just add resolver logic to the schema parsed from the `.graphql` file SDL.
221222

222223
## Contributing
223224

Sources/GraphQLGeneratorCore/Generator/TypeGenerator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ package struct TypeGenerator {
2323
protocol Resolvers: Sendable {
2424
"""
2525
if let queryType = schema.queryType {
26-
output += """
26+
try output += """
2727
28-
associatedtype Query: \(try swiftTypeDeclaration(for: queryType, includeNamespace: true, nameGenerator: nameGenerator))
28+
associatedtype Query: \(swiftTypeDeclaration(for: queryType, includeNamespace: true, nameGenerator: nameGenerator))
2929
"""
3030
}
3131
if let mutationType = schema.mutationType {
32-
output += """
32+
try output += """
3333
34-
associatedtype Mutation: \(try swiftTypeDeclaration(for: mutationType, includeNamespace: true, nameGenerator: nameGenerator))
34+
associatedtype Mutation: \(swiftTypeDeclaration(for: mutationType, includeNamespace: true, nameGenerator: nameGenerator))
3535
"""
3636
}
3737
if let subscriptionType = schema.subscriptionType {
38-
output += """
38+
try output += """
3939
40-
associatedtype Subscription: \(try swiftTypeDeclaration(for: subscriptionType, includeNamespace: true, nameGenerator: nameGenerator))
40+
associatedtype Subscription: \(swiftTypeDeclaration(for: subscriptionType, includeNamespace: true, nameGenerator: nameGenerator))
4141
"""
4242
}
4343
output += """

Tests/GraphQLGeneratorCoreTests/TypeGeneratorTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ struct TypeGeneratorTests {
287287
#expect(result == expected)
288288
}
289289

290-
291290
@Test func generateRootTypeProtocolForSubscription() async throws {
292291
let subscription = try GraphQLObjectType(
293292
name: "Subscription",

0 commit comments

Comments
 (0)