https://github.com/GraphQLSwift/graphql-vapor
Easily expose GraphQL APIs in Vapor
https://github.com/GraphQLSwift/graphql-vapor
graphql server swift vapor
Last synced: 2 days ago
JSON representation
Easily expose GraphQL APIs in Vapor
- Host: GitHub
- URL: https://github.com/GraphQLSwift/graphql-vapor
- Owner: GraphQLSwift
- License: mit
- Created: 2026-01-18T20:56:57.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2026-05-17T23:54:30.000Z (about 1 month ago)
- Last Synced: 2026-05-18T01:46:49.005Z (about 1 month ago)
- Topics: graphql, server, swift, vapor
- Language: Swift
- Homepage: https://swiftpackageindex.com/GraphQLSwift/graphql-vapor
- Size: 112 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQLVapor
[](https://swiftpackageindex.com/GraphQLSwift/graphql-vapor)
[](https://swiftpackageindex.com/GraphQLSwift/graphql-vapor)
A Swift library for integrating [GraphQL](https://github.com/GraphQLSwift/GraphQL) with [Vapor](https://github.com/vapor/vapor), enabling you to easily expose GraphQL APIs in your Vapor applications.
## Features
- Simple integration of GraphQL schemas with Vapor routing
- Compatibility with the [GraphQL over HTTP spec](https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md)
- Subscription support using WebSockets, with support for [`graphql-transport-ws`](https://github.com/GraphQLSwift/GraphQLTransportWS) and [`graphql-ws`](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md) subprotocols
- Built-in [GraphiQL](https://github.com/graphql/graphiql) IDE
## Installation
Add GraphQLVapor as a dependency in your `Package.swift`:
```swift
dependencies: [
.package(url: "https://github.com/NeedleInAJayStack/graphql-vapor.git", from: "1.0.0"),
]
```
Then add it to your target:
```swift
.target(
name: "YourTarget",
dependencies: [
.product(name: "GraphQLVapor", package: "graphql-vapor"),
]
)
```
## Usage
To use this package, you must already have a GraphQL schema. You can use [graphql-generator](https://github.com/GraphQLSwift/graphql-generator), [Graphiti](https://github.com/GraphQLSwift/Graphiti), or [GraphQL](https://github.com/GraphQLSwift/GraphQL) to construct one.
See [the HelloWorld project](https://github.com/GraphQLSwift/graphql-vapor/tree/main/Examples/HelloWorld) for a full working example.
### Basic Example
```swift
import GraphQL
import GraphQLVapor
import Vapor
// Define your GraphQL schema
let schema = try GraphQLSchema(
query: GraphQLObjectType(
name: "Query",
fields: [
"hello": GraphQLField(
type: GraphQLString,
resolve: { _, _, _, _ in
"World"
}
)
]
)
)
// Define your Context
struct GraphQLContext: Sendable {}
// Register GraphQL to the Vapor Application
app.graphql(schema: schema) { _ in
return GraphQLContext()
}
```
Now just run the application! You can view the GraphiQL IDE at `/graphql`, or query directly using `GET` or `POST`:
```bash
curl -X POST http://localhost:8080/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ hello }"}'
```
Response:
```json
{
"data": {
"hello": "World"
}
}
```
See the `RouteBuilder.graphql` function documentation for advanced configuration options.
### Computing GraphQL Context
The required closure in the `graphql` function is used to compute the `GraphQLContext` object, which is injected into each GraphQL resolver. The `inputs` argument passes in data from the request so that the Context can be created dynamically:
```swift
app.graphql(schema: schema) { inputs in
return GraphQLContext(
userID: inputs.vaporRequest.auth.userID,
logger: inputs.vaporRequest.logger,
debug: inputs.vaporRequest.headers[.init("debug")!] != nil,
operationName: inputs.graphQLRequest.operationName
)
}
```
### WebSockets
Subscription support via WebSockets is provided, and can be enabled by in the `subscriptionProtocols` configuration:
```swift
app.graphql(schema: schema, config: .init(subscriptionProtocols: [.websocket])) { _ in
GraphQLContext()
}
```
[`graphql-ws`](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md) and [`graphql-transport-ws`](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) subprotocols are supported.
### Graphiti
If using Graphiti to build your GraphQL schema, you must provide an instance of the `Resolver` to the `rootValue` argument. For example:
```swift
let graphqlSchema: Graphiti.Schema = try graphqlSchema()
app.graphql(
schema: graphqlSchema.schema,
rootValue: Resolver() // This must be included
) { _ in
Context()
}
```