Swiftpack.co - StevenLambion/GraphQLRouteCollection as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by StevenLambion.
StevenLambion/GraphQLRouteCollection v0.0.1
A GraphQL based RouteCollection for Vapor
⭐️ 18
🕓 4 years ago
linux macOS iOS
.package(url: "https://github.com/StevenLambion/GraphQLRouteCollection.git", from: "v0.0.1")

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.

GitHub

link
Stars: 18
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics