Create a GraphQL HTTP server with Kitura web framework.
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/lgaches/Kitura-GraphQL.git", majorVersion: 0, minor: 0),
]
)
GraphQLMiddleware
has the following parameters:
schema
: ASchema
instance fromGraphiti
. ASchema
must be provided.showGraphiQL
: Iftrue
, presentss GraphiQL when the GraphQL endpoint is loaded in a browser. We recommend that you setshowGraphiQL
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
.context
: A value to pass as thecontext
to the schema'sexecute
function fromGraphiti
. Ifcontext
is not provided, theRouterRequest
struct is passed as the context.
Once installed as a middleware at a path, GraphQLMiddleware
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.raw
: If theshowGraphiQL
option is enabled and the raw parameter is provided raw JSON will always be returned instead of GraphiQL even when loaded from a browser.
import Graphiti
import Kitura
import GraphQLMiddleware
let schema = try Schema<NoRoot, NoContext> { schema in
try schema.query { query in
try query.field(name: "hello",
type: String.self,
description: "Totally Awesome",
deprecationReason: nil,
resolve: {
(type, arguments, context, resolveInfo) -> String in
return "world"
})
}
}
let router = Router()
let graphQL = GraphQLMiddleware(schema: schema,
showGraphiQL: true,
rootValue: noRootValue)
router.all("/graphql", middleware: graphQL)
Kitura.addHTTPServer(onPort: 8090, with: router)
Kitura.run()
This project is released under the MIT license. See LICENSE for details.