Socket
Book a DemoInstallSign in
Socket

rest-graphql

Package Overview
Dependencies
Maintainers
4
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-graphql

Middleware for Express to adapt REST requests to GraphQL queries

1.0.6
latest
Source
npmnpm
Version published
Weekly downloads
6
50%
Maintainers
4
Weekly downloads
 
Created
Source

rest-graphql

Middleware for Express to adapt REST requests to GraphQL queries

NPM

Motivation

  • You've built a GraphQL server, and it's ready to use.
  • Not all your clients speak GraphQL. At the very least, legacy mobile clients can't make GraphQL requests.
  • You don't want to support both legacy and GraphQL client/server contracts simultaneously

rest-graphql provides middleware that lets you define mappers from REST requests to graphql queries that fetch the same data, letting you normalize all client queries into something your GraphQL server can handle.

Quick Start

Install the package

npm install --save rest-graphql

Let's say you're building out a new profile page and have defined a GraphQL schema for it.
You can fetch the necessary data via:

query PresidentQuery {
  presidents {
    name
  }
}

Create a new RestAdapter and add the middleware to your express server:

import RestAdapter from 'rest-graphql';

/* Build a new adapter
 *
 * isError        - Detect is the graphql query has failed.
 * transformError - Transform the failed query response into a RestAdapterResponse.
 *
 */
const adapter = new RestAdapter({
  isError:         (response) => !!response.errors,
  transformError:  (response) => response.errors[0].__http_secret__,
});

/* Add endpoints to the adapter:
 *
 * path             - The REST endpoint.
 * getQuery         - Function returning a Graphql query as a String.
 * transformSuccess - Transform the successful query response into a RestAdapterResponse.
 */
adapter.addEndpoint({
  path: '/presidents',
  getQuery: (request) => PRESIDENTS_QUERY,
  transformSuccess: response => ({ status: 200, body: response.presidents }),
});

const graphql = express();

graphql.use(adapter.app);
graphql.use('/graphql', graphqlExpress(/* ... */));

Which would result in:

1. HTTP Request:

GET https://api.test.com/presidents

2. Graphql Query:

query PresidentQuery {
  presidents {
    name
  }
}

3. Graphql Response:

{
  presidents: [
    { name: "Jacques Chirac" },
    { name: "George Washington" }
  ]
}

4. HTTP Response:

["Jacques Chirac", "George Washington"]

Error Handling

To transform Graphql query errors into the REST responses we recommend using something similar to apollo-server formatError options. In the above example we format the HTTP errors like the following:

{
  "presidents": null,
  "errors": [
    {
      "message": "Internal server error",
      "__http_secret__": {
        "status": 500,
        "body": {
          "message": "Internal server error"
        }
      },
    },
  ]
}

Keywords

graphql

FAQs

Package last updated on 24 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.