Skip to content

Latest commit

 

History

History
112 lines (81 loc) · 3.27 KB

README.md

File metadata and controls

112 lines (81 loc) · 3.27 KB

GraphQLRouteCollection

Provides a simple route collection to integrate GraphQLSwift into a Vapor application.

Travis Swift License

Installation

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"],
    ),
)

Usage

Basic

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)

Introspection

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.

GraphQLRouteCollection

Provides a simple means to setup typical GraphQL endpoints for both GET and POST requests.

let graphql = app.make(GraphQLService.self)
let graphQLRoutes = GraphQLRouteCollection()

GraphiQL

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)

Example

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.

License

This project is released under the MIT license. See LICENSE for details.