Giter Club home page Giter Club logo

graphqlresponder's Introduction

GraphQLResponder

Swift License Slack Travis Codecov Codebeat

Installation

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/Zewo/GraphQLResponder.git", majorVersion: 0, minor: 14),
    ]
)

Usage

Configuration

GraphQLResponder has the following parameters:

  • schema: A Schema instance from Graphiti. A Schema must be provided.
  • graphiQL : If true, presents GraphiQL when the GraphQL endpoint is loaded in a browser. We recommend that you set graphiql to true when your app is in development because it's quite useful. You may or may not want it in production.
  • rootValue: A value to pass as the rootValue to the schema's execute function from Graphiti.
  • contextValue: A value to pass as the contextValue to the schema's execute function from Graphiti. If context is not provided, the request struct is passed as the context.

Request Parameters

Once installed as a reponder, GraphQLResponder will accept requests with the parameters:

  • query: A string GraphQL document to be executed.
  • operationName: If the provided query contains multiple named operations, this specifies which operation should be executed. If not provided, a 400 error will be returned if the query contains multiple named operations.
  • variables: The runtime values to use for any GraphQL query variables as a JSON object. (Currently not supported in the URL's query-string)
  • raw: If the graphiql option is enabled and the raw parameter is provided raw JSON will always be returned instead of GraphiQL even when loaded from a browser.

GraphQLResponder will first look for each parameter in the URL's query-string:

/graphql?query=query+getUser($id:ID){user(id:$id){name}}&variables={"id":"4"}

If not found in the query-string, it will look in the POST request body. This requires a ContentNegotiationMiddleware to be mounted in the responder chain.

Example

Example using HTTPServer.

import HTTPServer
import Graphiti
import GraphQLResponder

let schema = try Schema<Void> { schema in
    schema.query = try ObjectType(name: "RootQueryType") { query in
        try query.field(
            name: "hello",
            type: String.self,
            description: "Cliche or classic?",
            resolve: { (_, _, _, _) in
                return "world"
            }
        )
    }
}

let graphql = GraphQLResponder(schema: schema, graphiQL: true, rootValue: noRootValue)

let router = BasicRouter { route in
    route.add(methods: [.get, .post], path: "/graphql", responder: graphql)
}

let contentNegotiation = ContentNegotiationMiddleware(mediaTypes: [.json])
let server = try Server(port: 8080, middleware: [contentNegotiation], responder: router)
try server.start()

Support

If you need any help you can join our Slack and go to the #help channel. Or you can create a Github issue in our main repository. When stating your issue be sure to add enough details, specify what module is causing the problem and reproduction steps.

Community

Slack

The entire Zewo code base is licensed under MIT. By contributing to Zewo you are contributing to an open and engaged community of brilliant Swift programmers. Join us on Slack to get to know us!

License

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

graphqlresponder's People

Contributors

paulofaria avatar

Watchers

James Cloos 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.