Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use existing REST endpoints with GraphQL

License

NotificationsYou must be signed in to change notification settings

apollographql/apollo-link-rest

title
REST Link

TheApollo Link Rest library is maintained by Apollo community members and not an Apollo GraphQL maintained library. For information on progress check outthe issues orthe 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/client graphql qs --saveoryarn add apollo-link-rest @apollo/client graphql qs

@apollo/client,graphql, andqs are peer dependencies needed byapollo-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!constrestLink=newRestLink({uri:'https://swapi.co/api/',});// Configure the ApolloClient with the default cache and RestLinkconstclient=newApolloClient({link:restLink,cache:newInMemoryCache(),});// A simple query to retrieve data about the first personconstquery=gql`  query luke {    person @rest(type: "Person", path: "people/1/") {      name    }  }`;// Invoke the query and log the person's nameclient.query({ query}).then(response=>{console.log(response.data.person.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 whenendpoints provides a default)
  • endpoints: root endpoint (uri) to apply paths to or a map of endpoints
  • customFetch: a customfetch 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 theheaders field on the context to allow passing headers to the HTTP request. It also supports thecredentials 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 completeapollo-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`npmtest# or `yarn test` to run testsnpmtest -- --watch# run tests in watch modenpm 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 innpm link apollo-link-rest

Related Libraries

  • JSON API Link providestooling for using GraphQL with JSON API compliant APIs.
  • apollo-type-patcher declarative type definitions for your REST API with zero dependencies.

About

Use existing REST endpoints with GraphQL

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors52


[8]ページ先頭

©2009-2025 Movatter.jp