GQLSwift is a SwiftPM build-tool plugin that generates Swift code from your GraphQL operation files at build time — no scripts, no manual steps, no network calls.
Every time you build, GQLPlugin invokes GQLMain, which:
- Recursively scans for all
*.graphqlfiles under the project root. - Parses
fragmentdefinitions andquery/mutationoperations, inlining all fragment spreads into each operation's document. - Writes a single
GQLOperations.swiftfile to the plugin output directory.
The generated file contains a root struct GQLOperations with one nested struct per operation. Each nested struct exposes three static String properties:
| Property | Description |
|---|---|
name |
The operation name (e.g. "HelloWorldQuery") |
type |
Either "query" or "mutation" |
document |
The full, self-contained GraphQL document string |
Duplicate operation names are automatically disambiguated so the project always compiles cleanly.
| Dependency | Minimum Version |
|---|---|
| Xcode | 15.3+ |
| Swift | 5.10+ |
| iOS | 13+ |
- In Xcode, go to File → Add Package Dependencies… and enter:
https://github.com/BaherTamer/GQLSwift - In your Target settings, open Build Phases → Run Build Tool Plug-ins and add GQLPlugin (not Link Binary With Libraries).
- Add your
*.graphqlfiles anywhere under the.xcodeprojdirectory and build —GQLOperationswill be available in that target with no import required.
The scanner searches from the
.xcodeprojdirectory downward. Keep all.graphqlfiles within that tree.
HelloWorld.graphql
query HelloWorld {
__typename
}Generated GQLOperations.swift
struct GQLOperations {
struct HelloWorldQuery {
static let name = "HelloWorldQuery"
static let type = "query"
static let document = """
query HelloWorld {
__typename
}
"""
}
}Then use it anywhere in your target:
let document = GQLOperations.HelloWorldQuery.documentIf operation names collide, the generated struct names are disambiguated automatically while the
nameproperty always reflects the original GraphQL name.