Skip to content

StevenLambion/GraphQLRouteCollection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQLRouteCollection

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

Swift License

Installation

Add GraphQLRouteCollection to your Package.swift

import PackageDescription

let package = Package(
    dependencies: [
        ...
        .package(url: "https://github.com/stevenlambion/GraphQLRouteCollection.git", .upToNextMajor(from: "0.0.1")),
    ],
    .target(
        name: "App",
        dependencies: [..., "GraphQLRouteCollection"],
    ),
)

Usage

Add the GraphQLRouteCollection to your droplet. Your schema, rootValue, and context are provided through a closure to allow per request dynamic execution. A common use case is setting up dataloaders for each request.

Basic

import Vapor
import GraphQLRouteCollection

extension Droplet {
    func setupRoutes() throws {

        // By default, the collection uses "graphql" as its path.
        // Pass a custom path as the first argument to change it.

        try collection(
            GraphQLRouteCollection() { req in (
                schema: schema,
                rootValue: [:],
                context: [:]
            )}
        )

    }
}

GraphiQL

Enables the GraphiQL IDE when a user accesses the graphql path in a web browser.

GraphQLRouteCollection(enableGraphiQL: true) { req in (
    schema: schema,
    rootValue: [:],
    context: [:]
)}

Introspection Query

The route collection enables a quick way to introspect your entire graphql schema. This is useful in development when you need to auto-generate a schema for graphql clients such as Apollo. Here's an example of how to enable for development use.

GraphQLRouteCollection(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)
  }

The Kitchen Sink

GraphQLRouteCollection("graphql", enableGraphiQL: true, enableIntrospectionQuery: true) { req in (
    schema: schema,
    rootValue: [:],
    context: ["dataLoaders": createDataLoaders(req)]
)}

License

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

About

A GraphQL based RouteCollection for Vapor

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages