Use existing REST endpoints with GraphQL
Clone or download
Latest commit 4ee75b5 Dec 13, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github [apollo-bot] Update the Issue/PR Templates with auto label (#87) Mar 2, 2018
designs Fix HTTP Headers Semantics, improve typings Dec 17, 2017
docs docs-tweak Dec 14, 2018
examples add qs dependencie in examples Nov 26, 2018
scripts Update dependencies Sep 16, 2018
src jest-fetch-mock update means new Headers-polyfill behavior. Dup heade… Dec 14, 2018
.gitignore Ignore vscode settings from git history May 6, 2018
.npmignore convert to apollo standard repo infrastructure Dec 1, 2017
.prettierrc feat(rest-link): add support for passing query method, validate metho… Dec 3, 2017
.prettierrc.yml Add prettier config and simplify package.json Oct 2, 2018
.travis.yml Temporarily disable danger Dec 19, 2017
CHANGELOG.md v0.7.0 & Changelog Dec 14, 2018
CONTRIBUTING.md convert to apollo standard repo infrastructure Dec 1, 2017
LICENSE convert to apollo standard repo infrastructure Dec 1, 2017
README.md Add documentation link to the README.md May 14, 2018
appveyor.yml convert to apollo standard repo infrastructure Dec 1, 2017
codecov.yml convert to apollo standard repo infrastructure Dec 1, 2017
dangerfile.ts convert to apollo standard repo infrastructure Dec 1, 2017
jest.config.js Make sure jest only works in src dir Oct 5, 2018
package.json v0.7.0 & Changelog Dec 14, 2018
renovate.json convert to apollo standard repo infrastructure Dec 1, 2017
rollup.config.js rollup: upgrade config, typescript source maps! Feb 25, 2018
tsconfig.json Keep JSDoc comments for TypeScript! Mar 27, 2018
tsconfig.tests.json Add a tsconfig for specs to require valid types Dec 10, 2017

README.md

title
REST Link

⚠️ This library is under active development ⚠️

This library is under active development. For information on progress check out this issues or the design. We would love your help with writing docs, testing, anything! We would love for you, yes you, to be a part of the Apollo community!

Purpose

An Apollo Link to easily try out GraphQL without a full server. It can be used to prototype, with third-party services that don't have a GraphQL endpoint or in a transition from REST to GraphQL.

Installation

npm install apollo-link-rest apollo-link graphql graphql-anywhere --save # or `yarn add apollo-link-rest apollo-link graphql graphql-anywhere`

apollo-link, graphql and graphql-anywhere are peer dependencies needed by apollo-link-rest.

Usage

Basics

import { RestLink } from "apollo-link-rest";
// Other necessary imports...

// Create a RestLink for the REST API
// If you are using multiple link types, restLink should go before httpLink,
// as httpLink will swallow any calls that should be routed through rest!
const restLink = new RestLink({
  uri: 'https://swapi.co/api/',
});

// Configure the ApolloClient with the default cache and RestLink
const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache(),
});

// A simple query to retrieve data about the first person
const query = gql`
  query luke {
    person @rest(type: "Person", path: "people/1/") {
      name
    }
  }
`;

// Invoke the query and log the person's name
client.query({ query }).then(response => {
  console.log(response.data.name);
});

Edit Basic Example

Apollo Client & React Apollo

For an example of using REST Link with Apollo Client and React Apollo view this CodeSandbox:

Edit Advanced Example

TypeScript

For an example of using REST Link with Apollo Client, React Apollo and TypeScript view this CodeSandbox:

Edit TypeScript Example

Options

REST Link takes an object with some options on it to customize the behavior of the link. The options you can pass are outlined below:

  • uri: the URI key is a string endpoint (optional when endpoints provides a default)
  • endpoints: root endpoint (uri) to apply paths to or a map of endpoints
  • customFetch: a custom fetch to handle REST calls
  • headers: an object representing values to be sent as headers on the request
  • credentials: a string representing the credentials policy you want for the fetch call
  • fieldNameNormalizer: function that takes the response field name and converts it into a GraphQL compliant name

Context

REST Link uses the headers field on the context to allow passing headers to the HTTP request. It also supports the credentials field for defining credentials policy.

  • headers: an object representing values to be sent as headers on the request
  • credentials: a string representing the credentials policy you want for the fetch call

Documentation

For a complete apollo-link-rest reference visit the documentation website at: https://www.apollographql.com/docs/link/links/rest.html

Contributing

This project uses TypeScript to bring static types to JavaScript and uses Jest for testing. To get started, clone the repo and run the following commands:

npm install # or `yarn`

npm test # or `yarn test` to run tests
npm test -- --watch # run tests in watch mode

npm run check-types # or `yarn check-types` to check TypeScript types

To run the library locally in another project, you can do the following:

npm link

# in the project you want to run this in
npm link apollo-link-rest