0.15.1 • Published 4 years ago

graphyne-server v0.15.1

Weekly downloads
7
License
MIT
Repository
github
Last release
4 years ago

Graphyne Server

npm ci codecov PRs Welcome

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
// or
yarn 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 } = require("graphyne-server");

const GQL = new GraphQL(options);

const gqlHandle = httpHandler(GQL, handlerOptions);
// 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 = require("http");
const server = http.createServer(gqlHandle);

server.listen(3000, () => {
  console.log(`🚀  Server ready at :3000`);
});

Express

Example

const express = require('express')
const app = express()

app.all('/graphql', gqlHandle);

app.listen(3000, () => {
  console.log(`🚀  Server ready at :3000`);
});

Micro

Example

module.exports = gqlHandle;

API

new GraphQL(options)

Constructing a GraphQL instance. It accepts the following options:

optionsdescriptiondefault
schemaA GraphQLSchema instance. It can be created using makeExecutableSchema from graphql-tools.(required)
rootValueA value or function called with the parsed Document that creates the root value passed to the GraphQL executor.{}
formatErrorAn 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 if source 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:

optionsdescriptiondefault
contextAn object or function called to creates a context shared across resolvers per request. The function accepts IncomingMessage as the only argument.{}
pathSpecify 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.

License

MIT

0.15.1

4 years ago

0.15.0

4 years ago

0.14.3

4 years ago

0.14.0

4 years ago

0.14.1

4 years ago

0.13.1

4 years ago

0.13.2

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.1

4 years ago

0.7.2

4 years ago

0.8.0

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago