0.6.1 • Published 4 years ago

graphyne-worker v0.6.1

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

Graphyne Worker

npm ci codecov PRs Welcome

This package is highly experimental and may be changed or removed at any time!

GraphQL execution layer in the browser and at the edge.

Service Worker Example

Why GraphQL in the browser

Depending on the requirement, you can lighten the load of your server by moving GraphQL execution layer to the browsers' Web Workers API.

  • Not only that you can query your backend, you can also query 3rd parties' APIs without making a redundant round to and from the backend.
  • It enables query deduplication so that you do not waste server resources for identical 3rd parties' requests, while improving speed/performance.

Install

Install Graphyne Worker and graphql dependencies using:

npm i graphyne-worker graphql
// or
yarn add graphyne-worker graphql

Usage

This assumes basic understanding of service worker. If not, you can learn how to register the service worker here.

import { GraphQL, handleRequest } from 'graphyne-worker';

// Creating an instance of GraphQL
const GQL = new GraphQL(options);

addEventListener('fetch', (event) => {
  const url = new URL(event.request.url);
  if (url.pathname === '/graphql')
    return event.respondWith(
      handleRequest(GQL, event.request, handlerOptions)
    );
});

Fetch requests to /graphql will now be intercepted by the registered worker.

See Using Web Workers for more info.

Note: graphyne-worker can be large in size for use in browser. Consider lazy loading it and implement Offline/Progressive Web Apps.

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 handleRequest 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.

handleRequest(GQL, request, handlerOptions)

GQL is an instance of GraphQL.

Handles the FetchEvent.request (request) and returns a promise of Response to be used in event.respondWith.

handlerOptions accepts the following:

optionsdescriptiondefault
contextAn object or function called to creates a context shared across resolvers per request. The function accepts Request as the only argument.{}

Contributing

Please see my contributing.md.

License

MIT

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.0

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago