Swift Client¶
A fully typed GraphQL client generated from your schema — idiomatic Swift with async/await,
Sendable structs, and no third-party dependency. URLSession (Foundation) is the only transport
used, so the generated code compiles unchanged on Apple platforms and on Linux
(swift-corelibs-foundation).
Configuration¶
Set "mode": "client" and add a "swift" key under clientConfig:
{
"schemaPaths": ["schema/*.graphql"],
"mode": "client",
"typeMappings": {
"ID": "String",
"String": "String",
"Float": "Double",
"Int": "Int",
"Boolean": "Bool"
},
"outputDir": "Sources/GraphLinkGenerated",
"clientConfig": {
"swift": {
"moduleName": "GraphLinkGenerated",
"generateAllFieldsFragments": true,
"autoGenerateQueries": true,
"immutableTypeFields": true,
"wsAdapter": "urlsession"
}
}
}
schemaPaths:
- schema/*.graphql
mode: client
typeMappings:
ID: String
String: String
Float: Double
Int: Int
Boolean: Bool
outputDir: Sources/GraphLinkGenerated
clientConfig:
swift:
moduleName: GraphLinkGenerated
generateAllFieldsFragments: true
autoGenerateQueries: true
immutableTypeFields: true
wsAdapter: urlsession
Generated output¶
For a schema with User, Post, CreateUserInput, and a UserStatus enum, GraphLink generates:
Sources/GraphLinkGenerated/
Client/
GraphLinkClient.swift ← main client (queries / mutations / subscriptions)
GraphLinkQueries.swift
GraphLinkMutations.swift
GraphLinkSubscriptions.swift
GraphLinkClientAdapter.swift ← HTTP adapter typealias
DefaultGraphLinkURLSessionWebSocketAdapter.swift
GraphLinkMultipartAdapter.swift ← file-upload adapter typealias
GraphLinkCacheStore.swift ← cache store protocol
InMemoryGraphLinkCacheStore.swift ← actor-based in-memory cache
GraphLinkCacheEntry.swift
GraphLinkTagEntry.swift
GraphLinkPartialQuery.swift
GraphLinkException.swift
GLUpload.swift ← file upload type
...
Types/
User.swift
Post.swift
CreateUserResponse.swift
...
Inputs/
CreateUserInput.swift
Enums/
UserStatus.swift
Interfaces/
SearchResult.swift ← generated union/interface protocol
Structs¶
Types and inputs are generated as Sendable structs with an explicit memberwise init and
toJson()/fromJson():
// generated
public struct User: Sendable, Identifiable {
public let id: String
public let name: String
public let email: String?
public let status: UserStatus
public let address: Address
public init(
id: String,
name: String,
email: String? = nil,
status: UserStatus,
address: Address
) {
self.id = id
self.name = name
self.email = email
self.status = status
self.address = address
}
public func toJson() -> [String: Any?] {
return [
"id": id,
"name": name,
"email": email,
"status": status.toJson(),
"address": address.toJson(),
]
}
public static func fromJson(_ map: [String: Any?]) throws -> User {
return User(
id: try glString(map["id"], "User.id"),
name: try glString(map["name"], "User.name"),
email: try glStringOrNil(map["email"], "User.email"),
status: try UserStatus.fromJson(glString(map["status"], "User.status")),
address: try Address.fromJson(glMap(map["address"], "User.address"))
)
}
}
fromJson never traps on bad data. A missing or null non-null field, a value of the wrong type, or an
Int that is a fraction, out of range, a string or a boolean throws a GraphLinkDecodeError whose message
names the field (User.id: missing or null, User.age: expected Int, got Bool). A nullable field accepts
null or an absent key, and throws on a wrong type. The checks are the gl* helpers in the generated
GraphLinkDecode.swift.
Breaking change in v6.0.0
fromJson on every generated type, input and strict enum is now throws. It used to be non-throwing and
crashed the process on a bad value (as!, a force unwrap, the NSNumber bridge). The generated client
already calls it with try. If you call fromJson yourself, add try and handle the
GraphLinkDecodeError. A nullable field with the wrong type used to decode as nil and now throws.
A type is additionally Identifiable whenever it has a non-null id field — ready to drop into
a SwiftUI List without extra glue.
Enums¶
// generated
public enum UserStatus: String, Sendable {
case active = "ACTIVE"
case inactive = "INACTIVE"
case suspended = "SUSPENDED"
case unknownValue__ = "unknownValue__"
public func toJson() -> String {
return rawValue
}
public static func fromJson(_ value: String) -> UserStatus {
return UserStatus(rawValue: value) ?? .unknownValue__
}
}
Unknown enum values¶
A server can add an enum value after your app has shipped. By default, unknownValue__ is
added to every generated enum and fromJson returns it for any value the schema did not
declare, so one new value does not fail the whole response. Handle it in your own switch
statements.
Sending the fallback back to the server
In Dart, Java, Kotlin and TypeScript, encoding unknownValue__ throws, so it can never
be sent. Swift cannot do this: toJson() is non-throwing across all generated code, and
making it throws would force a try at every call site. In Swift, toJson() returns
the literal string "unknownValue__", so if you pass the fallback as a query argument
or input field it is sent, and the server rejects it with a GraphQL validation
error. The failure happens on the server instead of before the request leaves the
client. Do not pass unknownValue__ as an argument.
Set "strictEnums": true to remove the fallback. fromJson then throws a GraphLinkDecodeError
for an unrecognized wire value — treat that as a contract error.
Queries¶
All query, mutation, and subscription methods are async throws — call them from an async
context:
import GraphLinkGenerated
let user = try await client.queries.getUser(id: "user-42").getUser
print("\(user.name) — \(user.status)")
Compound queries that resolve multiple fields in a single network call return a typed response with one property per top-level resolver:
let result = try await client.queries.fetchUserAndPost(userId: "user-1", postId: "post-1")
let user = result.user // User
let post = result.post // Post
One HTTP call — two fully typed results.
Mutations¶
Input structs are plain memberwise-initialized Sendable structs — construct them directly, no
builder boilerplate:
let input = CreateUserInput(
name: "Alice Smith",
email: "alice@example.com",
status: .active,
address: AddressInput(street: "123 Main St", city: "Springfield", country: "US", zip: "12345")
)
let response = try await client.mutations.createUser(input: input)
print(response.createUser.id) // "user-1"
Nullable input fields default to nil — omit them when you don't need them:
let patch = UpdateUserInput(status: .inactive) // only sets status
try await client.mutations.updateUser(id: "user-1", input: patch)
Subscriptions¶
Subscriptions return an AsyncThrowingStream backed by a WebSocket connection. Iterate it with
for try await:
let task = Task {
for try await event in client.subscriptions.userCreated() {
print("New user: \(event.userCreated.name)")
}
}
// cancel when done
task.cancel()
Events are deserialized into the same typed response structs used by queries.
Projected types¶
When a query selects only a subset of a type's fields, GraphLink generates a dedicated projected struct instead of reusing the full type, so the Swift compiler catches field misuse at compile time:
// schema query that selects only id, name, status
let summary = try await client.queries.fetchUserSummary(id: "user-1").getUser
// summary is User_IdNameStatus — only .id, .name, .status are available
print(summary.status) // UserStatus.active
Union and interface types¶
Union members are dispatched via __typename. Each member type conforms to the generated
protocol:
let results: [SearchResult] = try await client.queries.runSearch(query: "alice").search
for result in results {
switch result {
case let user as UserResult: print("User: \(user.name)")
case let post as PostResult: print("Post: \(post.title)")
default: break
}
}
Wiring the client¶
HTTP adapter¶
GraphLinkClientAdapter is a plain closure typealias — implement it with URLSession or any
HTTP client of your choice:
import GraphLinkGenerated
import Foundation
let adapter: GraphLinkClientAdapter = { payload in
var request = URLRequest(url: URL(string: "https://api.example.com/graphql")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.httpBody = payload
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse, 200..<300 ~= http.statusCode else {
throw GraphLinkException.http
}
return data
}
WebSocket adapter (URLSession)¶
When wsAdapter: "urlsession" is set (the default), GraphLink generates a default
DefaultGraphLinkURLSessionWebSocketAdapter built on URLSessionWebSocketTask. Pass your server
URL and optional headers:
import GraphLinkGenerated
let wsAdapter = DefaultGraphLinkURLSessionWebSocketAdapter(
url: URL(string: "wss://api.example.com/graphql")!,
headers: ["Authorization": "Bearer \(token)"]
)
Assembling the client¶
import GraphLinkGenerated
let client = GraphLinkClient(
adapter: adapter,
wsAdapter: wsAdapter,
multipartAdapter: DefaultGraphLinkURLSessionMultipartAdapter()
)
// Use it
let user = try await client.queries.getUser(id: "42").getUser
Caching¶
Cache behaviour is declared in the schema, not in code:
type Query {
getUser(id: ID!): User! @glCache(ttl: "5m", tags: ["users"])
}
type Mutation {
updateUser(id: ID!, input: UpdateUserInput!): User! @glCacheInvalidate(tags: ["users"])
}
The generated client handles caching transparently through an actor-based
InMemoryGraphLinkCacheStore — the second call to getUser with the same arguments is served
from the cache; updateUser evicts it:
let first = try await client.queries.getUser(id: "1") // hits the network
let second = try await client.queries.getUser(id: "1") // served from cache — no HTTP call
try await client.mutations.updateUser(id: "1", input: UpdateUserInput(status: .inactive))
let third = try await client.queries.getUser(id: "1") // cache evicted — hits the network again
Bring your own cache store by conforming to GraphLinkCacheStore (a Sendable protocol) and
passing it to the GraphLinkClient initializer.
Transport errors¶
Behavior change in v6.0.0
A GraphQL call whose response has no data and no errors, or whose HTTP status is not 2xx and carries no
GraphQL body (a gateway page, {"error": "unauthorized"}), now throws a GraphLinkException, like a
server-side GraphQL error. It used to crash the process. GraphLinkError has two new optional fields,
status and body, set for HTTP failures.
Inline error handling (@glCaptureErrors)¶
Enable per-operation inline errors with the directive or globally via config:
// returns FindUserFullResponse with an optional data + optional errors
let result = try await client.queries.findUser(id: "user-99")
if let errors = result.errors {
errors.forEach { print($0.message) }
} else {
print(result.data?.findUser?.name ?? "")
}
Enable globally in config with "captureErrors": true under clientConfig.swift.
File uploads¶
Upload mutations accept a GLUpload and are sent through DefaultGraphLinkURLSessionMultipartAdapter per the GraphQL
multipart request spec. The body is streamed: a file is read in 64 KiB chunks as URLSession has room to send them, never
loaded whole.
// from memory
let upload = GLUpload(data: fileData, mimeType: "image/png", filename: "avatar.png")
// from disk: streamed, the length comes from the file system
let upload = try GLUpload(fileURL: url, mimeType: "image/png")
// from any async sequence of Data; `length: nil` sends the body chunked
let upload = GLUpload(length: size, mimeType: "video/mp4", filename: "clip.mp4") { makeChunks() }
try await client.mutations.uploadAvatar(userId: "1", file: upload)
Pass an UploadProgressCallback to the generated method to observe upload progress. It reports bytes written to the
socket and is only called when the total size is known (GLUpload.length is not nil for every file); an upload of
unknown size is sent with chunked transfer encoding and reports no progress. URLSession coalesces the callbacks, so
expect a few reports for a small body, not one per chunk.
GLUpload no longer exposes a data property; call try await upload.readAll() if you need the bytes (for example in a
custom GraphLinkMultipartAdapter).
Custom scalar codecs¶
A scalar can map to a Swift type with fromJson and toJson expressions, where {} stands for the value:
"typeMappings": {
"Opaque": {
"type": "String",
"jsonType": "String",
"fromJson": "\"opaque:\" + {}",
"toJson": "String({}.dropFirst(7))"
}
}
jsonType names the JSON type the server sends: String, Int, Double, Bool, Map or List. It is
different from type, which is the Swift type your code uses. With jsonType, the generated decoder reads the
field through the same checked helper as a built-in scalar and passes a value of that type to fromJson, so the
expression needs no cast. A null, missing or wrong-typed value throws a GraphLinkDecodeError naming the field
and the expression never runs; on a nullable field null stays nil.
Without jsonType, {} is an untyped Any?. Swift cannot catch a failed as!, a force unwrap or try!, and the
expression runs inside the generated decoder, so a trap in it terminates the process. Set jsonType and keep
fromJson free of !.
jsonType only checks the value that arrives. An expression that can fail on a valid wire value (a date string
that does not parse) still has to handle that itself. jsonType is read by every client generator (Dart, Java, Kotlin, TypeScript,
Python, Swift and Go).
Known limitations¶
- Encoding
unknownValue__does not throw. Dart, Java, Kotlin, TypeScript, Python and Go all refuse to encode the fallback enum member, so it can never reach the server. Swift'stoJson()is non-throwing across all generated code, so it returns the literal string"unknownValue__"and the server rejects the request with a GraphQL validation error. See Unknown enum values.
Persisted queries¶
Set persistedQueries: "apq" under clientConfig.swift to send long operations as a SHA-256 hash and retry once with the query text on PersistedQueryNotFound. persistedQueriesUseGet: true sends hash-only queries over GET. Both are off by default. See Persisted queries.
REST operations¶
Fields of Query and Mutation annotated with @glRest are served over HTTP/JSON through this same client, with the same call syntax as GraphQL operations. See REST operations for the directive, argument roles, responses, caching and the adapter.
Configuration reference¶
| Option | Type | Default | Description |
|---|---|---|---|
moduleName |
string |
— | Required. Used in doc comments / generated Package.swift (if emitted) — generated files have no module declaration of their own. |
generateAllFieldsFragments |
boolean |
true |
Generate _all_fields_* fragments for every type. |
autoGenerateQueries |
boolean |
true |
Auto-generate query strings for all operations. |
nullableFieldsRequired |
boolean |
false |
When true, nullable initializer parameters have no default. |
immutableTypeFields |
boolean |
true |
Generate let fields (immutable) instead of var. |
operationNameAsParameter |
boolean |
false |
Append the operation name as a ?operationName= query parameter in the request URL. |
captureErrors |
boolean |
false |
Return FullResponse (data + errors) for all operations instead of throwing. |
strictEnums |
boolean |
false |
When false, enums get an unknownValue__ case and undeclared wire values decode to it. When true, they crash on decode instead. See Unknown enum values. |
wsAdapter |
"urlsession" | "none" |
"urlsession" |
WebSocket adapter to generate. "urlsession" emits DefaultGraphLinkURLSessionWebSocketAdapter; "none" emits only the adapter typealias. |
defaultAlias |
string? |
null |
Default alias prefix for auto-generated query fields. |
autoGenerateQueriesFor |
object? |
null |
Restrict auto-generated operations to an explicit allow-list per operation type. |
autoGenerateQueriesArgumentLimit |
int? |
200 |
Caps propagated field arguments per auto-generated operation before it's skipped with a warning. |
maxFragmentBodySize |
int? |
8192 |
Skips oversized auto-generated _all_fields fragments (and dependent auto-queries). Set to null to disable. |