Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# GraphQLVapor

***WARNING***: This package is in v0.x beta. It's API is still evolving and is subject to breaking changes in minor version bumps.

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
Expand Down
3 changes: 3 additions & 0 deletions Sources/GraphQLVapor/GraphQLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ struct GraphQLHandler<
WebSocketInit: Equatable & Codable & Sendable
>: Sendable {
let schema: GraphQLSchema
let rootValue: any Sendable
let config: GraphQLConfig<WebSocketInit>
let computeContext: @Sendable (Request) async throws -> Context

init(
schema: GraphQLSchema,
rootValue: any Sendable,
config: GraphQLConfig<WebSocketInit>,
computeContext: @Sendable @escaping (Request) async throws -> Context
) {
self.schema = schema
self.rootValue = rootValue
self.config = config
self.computeContext = computeContext
}
Expand Down
1 change: 1 addition & 0 deletions Sources/GraphQLVapor/HTTP/GraphQLHandler+HTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension GraphQLHandler {
result = try await graphql(
schema: schema,
request: graphQLRequest.query,
rootValue: rootValue,
context: context,
variableValues: graphQLRequest.variables,
operationName: graphQLRequest.operationName,
Expand Down
4 changes: 3 additions & 1 deletion Sources/GraphQLVapor/RoutesBuilder+graphql.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public extension RoutesBuilder {
/// - Parameters:
/// - path: The route that should respond to GraphQL requests. Both `GET` and `POST` routes are registered.
/// - schema: The GraphQL schema that should be used to respond to requests.
/// - rootValue: The `rootValue` GraphQL execution arg. This is the object passed to the root resolvers.
/// - config: GraphQL Handler configuration options. See type documentation for details.
/// - computeContext: A closure used to compute the GraphQL context from incoming requests. This must be provided.
func graphql<
Expand All @@ -23,14 +24,15 @@ public extension RoutesBuilder {
>(
_ path: [PathComponent] = ["graphql"],
schema: GraphQLSchema,
rootValue: any Sendable = (),
config: GraphQLConfig<WebSocketInit> = GraphQLConfig<EmptyWebsocketInit>(),
computeContext: @Sendable @escaping (Request) async throws -> Context
) {
ContentConfiguration.global.use(encoder: GraphQLJSONEncoder(), for: .jsonGraphQL)
ContentConfiguration.global.use(decoder: JSONDecoder(), for: .jsonGraphQL)

// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#request
let handler = GraphQLHandler<Context, WebSocketInit>(schema: schema, config: config, computeContext: computeContext)
let handler = GraphQLHandler<Context, WebSocketInit>(schema: schema, rootValue: rootValue, config: config, computeContext: computeContext)
get(path) { request in
// WebSocket handling
if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extension GraphQLHandler {
try await graphql(
schema: schema,
request: graphQLRequest.query,
rootValue: rootValue,
context: context,
variableValues: graphQLRequest.variables,
operationName: graphQLRequest.operationName
Expand All @@ -56,6 +57,7 @@ extension GraphQLHandler {
try await graphqlSubscribe(
schema: schema,
request: graphQLRequest.query,
rootValue: rootValue,
context: context,
variableValues: graphQLRequest.variables,
operationName: graphQLRequest.operationName
Expand All @@ -71,6 +73,7 @@ extension GraphQLHandler {
try await graphql(
schema: schema,
request: graphQLRequest.query,
rootValue: rootValue,
context: context,
variableValues: graphQLRequest.variables,
operationName: graphQLRequest.operationName
Expand All @@ -80,6 +83,7 @@ extension GraphQLHandler {
try await graphqlSubscribe(
schema: schema,
request: graphQLRequest.query,
rootValue: rootValue,
context: context,
variableValues: graphQLRequest.variables,
operationName: graphQLRequest.operationName
Expand Down