Provides a simple route collection to integrate GraphQLSwift into a Vapor application.
Add VaporGraphQL to your Package.swift
import PackageDescription
let package = Package(
dependencies: [
...
.package(url: "https://github.com/stevenlambion/GraphQLRouteCollection.git", .upToNextMajor(from: "0.1.0")),
],
.target(
name: "App",
dependencies: [..., "VaporGraphQL"],
),
)
Create a new HTTPGraphQL service with your schema, then register it:
services.register(HTTPGraphQL() { req in (
schema: schema,
rootValue: [:],
context: req
)})
Then route it in your app's config.swift
file:
let router = EngineRouter.default()
let graphQLRouteCollection = GraphQLRouteCollection(enableGraphiQL: true)
try graphQLRouteCollection.boot(router: router)
try routes(router)
services.register(router, as: Router.self)
HTTPGraphQL provides a way to introspect the entire graphql schema. This is useful in development when you need to auto-generate a schema for graphql clients.
let graphql = HTTPGraphQL(enableIntrospectionQuery: true) { req in (
schema: schema,
rootValue: [:],
context: [:]
)}
To retrieve the introspected schema simply hit the graphql endpoint without a provided query. It will instead use an internal introspection query.
fetch("localhost:8080/graphql") { resp in
let schema = parseSchema(resp.json)
}
HTTPGraphQL will use the request's method to determine how to retrieve the graphql query. For GET requests, it uses the query items in the URL. For POSTs, it uses the payload.
Provides a simple means to setup typical GraphQL endpoints for both GET and POST requests.
let graphql = app.make(GraphQLService.self)
let graphQLRoutes = GraphQLRouteCollection()
HTTPGraphQL provides a method to return a GraphiQL HTML view.
router.get("/graphiql") { req in
return graphql.renderGraphiQL(pathToGraphQL: "/graphql")
}
You can also enable it for the route collection:
GraphQLRouteCollection(enableGraphiQL: true)
You can see a live example here. The source app is located in the Sources/Example
directory, and there is a web.Dockerfile
used by the host, Vapor Cloud v2.
This project is released under the MIT license. See LICENSE for details.