Graphyne Server
This package is highly experimental and may be changed or removed at any time!
Lightning-fast GraphQL Server for JavaScript servers. Supports express
, micro
, Node HTTP server
and more.
Install
Install Graphyne Server
and graphql
dependencies using:
npm i graphyne-server graphql// oryarn add graphyne-server graphql
Usage
Start out by creating an GraphQL
instance from graphyne-server
and create a HTTP handler using that instance.
const GraphQL httpHandler = ; const GQL = options; const gqlHandle = ;// Define `handlerOptions.path` if you want `gqlHandle` to run on specific path and respond with 404 otherwise
See more examples here.
Node HTTP Server
const http = ;const server = http; server;
Express
const express = const app = appall'/graphql' gqlHandle; app;
Micro
moduleexports = gqlHandle;
API
new GraphQL(options)
Constructing a GraphQL
instance. It accepts the following options:
options | description | default |
---|---|---|
schema | A GraphQLSchema instance. It can be created using makeExecutableSchema from graphql-tools. |
(required) |
rootValue | A value or function called with the parsed Document that creates the root value passed to the GraphQL executor. |
{} |
formatError | An optional function which will be used to format any errors from GraphQL execution result. | formatError |
Looking for options.context
? It is in httpHandler
or GraphQL#graphql
.
GraphQL#graphql({ source, contextValue, variableValues, operationName })
Execute the GraphQL query with:
source
(string): The request query string to be executed.contextValue
(object): the context value that will get passed to resolve functions.variablesValues
(object): the variables object that will be used in the executor.operationName
(string): The operation to be run ifsource
contains multiple operations.
The function returns a never-rejected promise of the execution result, which is an object of data
and errors
.
httpHandler(GQL, handlerOptions)
Create a handling function for incoming HTTP requests.
GQL
is an instance of GraphQL
.
handlerOptions
accepts the following:
options | description | default |
---|---|---|
context | An object or function called to creates a context shared across resolvers per request. The function accepts IncomingMessage as the only argument. | {} |
path | Specify a path for the GraphQL endpoint, and graphyne-server will response with 404 elsewhere. You should not set this when using with frameworks with built-in routers (such as express ). |
undefined (run on all paths) |
Note: In frameworks like express
, context
function will accept express
's Request instead.
Additional features
Since some features are not used by everyone, they are not included by default to keep the package light-weight.
Subscriptions
GraphQL subscriptions support is provided by graphyne-ws package. Check out the documentation here.
File uploads
To enable file upload, use graphql-upload and add the Upload
scaler. See #10.
GraphQL Playground
You can use the packages from graphql-playground.
Dataloader and N+1 Problem
To integrate dataloader to solve GraphQL N+1 Problem, see this example.
Contributing
Please see my contributing.md.