Giter Site home page Giter Site logo

happyswifter / graphqlroutecollection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from noahemmet/graphqlroutecollection

1.0 2.0 0.0 52 KB

A GraphQL based RouteCollection for Vapor

License: MIT License

Ruby 0.04% Swift 97.00% Dockerfile 2.96%

graphqlroutecollection's Introduction

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)

License

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

graphqlroutecollection's People

Contributors

happyswifter avatar noahemmet avatar stevenlambion avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.