ZewoGraveyard / GraphQLResponder

Swift GraphQL HTTP Middleware/Responder for macOS and Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

Swift GraphQL HTTP Middleware/Responder for macOS and Linux

License:MIT License


Languages

Language:Swift 100.0%