import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/Zewo/GraphQLResponder.git", majorVersion: 0, minor: 14),
]
)
GraphQLResponder
has the following parameters:
schema
: ASchema
instance fromGraphiti
. ASchema
must be provided.graphiQL
: Iftrue
, presents GraphiQL when the GraphQL endpoint is loaded in a browser. We recommend that you setgraphiql
totrue
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 therootValue
to the schema'sexecute
function fromGraphiti
.contextValue
: A value to pass as thecontextValue
to the schema'sexecute
function fromGraphiti
. Ifcontext
is not provided, therequest
struct is passed as the context.
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 thegraphiql
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 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()
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.
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!
This project is released under the MIT license. See LICENSE for details.