You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Swift package plugin that generates server-side GraphQL API code from GraphQL schema files, inspired by [swift-openapi-generator](https://github.com/apple/swift-openapi-generator).
3
+
A Swift package plugin that generates server-side GraphQL API code from GraphQL schema files, inspired by [GraphQL Tools' makeExecutableSchema](https://the-guild.dev/graphql/tools/docs/generate-schema).
4
4
5
-
This tool uses [GraphQL Swift](https://github.com/GraphQLSwift/GraphQL) to generate type-safe Swift code from your GraphQL schemas, eliminating boilerplate while maintaining full control over your business logic.
6
-
7
-
## Status
8
-
9
-
🚧 **Phase 1 Complete** - Foundation is in place with basic code generation
10
-
11
-
Currently implemented:
12
-
- ✅ Build plugin for SPM integration
13
-
- ✅ GraphQL schema parsing using GraphQL Swift's `buildSchema`
14
-
- ✅ Type generation (Swift structs from GraphQL types)
15
-
- ✅ Resolver protocol generation
16
-
- ✅ Basic runtime library with ResolverContext
17
-
- ✅ CLI tool for code generation
18
-
19
-
Still in development (see [plan.md](plan.md)):
20
-
- ⏳ Complete schema builder generation (Phase 5)
21
-
- ⏳ Mutations and subscriptions support
22
-
- ⏳ Custom scalar mappings
23
-
- ⏳ Configuration file support
24
-
- ⏳ Complete test coverage
25
-
- ⏳ Working end-to-end examples
5
+
This tool uses [GraphQL Swift](https://github.com/GraphQLSwift/GraphQL) to generate type-safe Swift code and protocol stubs from your GraphQL schema files, eliminating boilerplate while maintaining full control over your business logic.
26
6
27
7
## Features
28
8
@@ -32,20 +12,14 @@ Still in development (see [plan.md](plan.md)):
32
12
-**Modern Swift**: Uses async/await for all resolver functions
33
13
-**Minimal boilerplate**: Generates only ceremony code - you write the business logic
│ ├── GraphQLGeneratorCore/ # Parsing and generation logic
141
-
│ │ ├── Parser/
142
-
│ │ │ └── SchemaParser.swift
143
-
│ │ └── Generator/
144
-
│ │ ├── CodeGenerator.swift
145
-
│ │ ├── TypeGenerator.swift
146
-
│ │ ├── ResolverGenerator.swift
147
-
│ │ └── SchemaGenerator.swift
148
-
│ └── GraphQLGeneratorRuntime/ # Runtime support library
149
-
│ └── ResolverContext.swift
150
-
├── Tests/
151
-
│ └── GraphQLGeneratorTests/
152
-
└── Examples/
153
-
└── HelloWorldServer/
77
+
structDateTime: Scalar {}
154
78
```
155
79
156
-
## Generated Code Examples
157
-
158
-
### From this GraphQL schema:
159
-
160
-
```graphql
161
-
typeUser {
162
-
id: ID!
163
-
name: String!
164
-
email: String!
80
+
Create a resolvers struct with the required typealiases:
81
+
```swift
82
+
structResolvers: ResolversProtocol {
83
+
typealiasQuery= ExamplePackage.Query
84
+
typealiasMutation= ExamplePackage.Mutation
165
85
}
166
86
```
167
87
168
-
### Generates this Swift code:
88
+
As you build the `Query` and `Mutation` types and their resolution logic, you will be forced to define a concrete type for every reachable GraphQL result, according to its generated protocol.
89
+
Here's a small example of a schema that allows querying for the current user, who is only identified by an email address:
169
90
170
91
```swift
171
-
// Types.swift
172
-
publicstructUser: Codable {
173
-
publicletid: String
174
-
publicletname: String
175
-
publicletemail: String
176
-
177
-
publicinit(
178
-
id: String,
179
-
name: String,
180
-
email: String
181
-
) {
182
-
self.id = id
183
-
self.name = name
184
-
self.email = email
92
+
structQuery: QueryProtocol {
93
+
// This is required by `QueryProtocol`, and used by GraphQL query resolution.
1. Default values: Default values are currently ignored
127
+
2. Directives: Directives are currently not supported
128
+
3. Subscription: Subscription definitions are currently ignored
129
+
4. Improved testing: Generator tests should cover much more of the functionality
130
+
5. Additional examples: Ideally large ones that cover significant GraphQL features
131
+
6. 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.
221
132
222
-
## Related Projects
133
+
## Contributing
223
134
224
-
-[GraphQL Swift](https://github.com/GraphQLSwift/GraphQL) - The Swift GraphQL implementation
225
-
-[Vapor](https://github.com/vapor/vapor) - Server-side Swift framework
226
-
-[Hummingbird](https://github.com/hummingbird-project/hummingbird) - Lightweight server framework
135
+
This project is in active development. Contributions are welcome!
0 commit comments