Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-link-rest

Package Overview
Dependencies
Maintainers
4
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-link-rest

Query existing REST services with GraphQL

  • 0.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
128K
increased by1.21%
Maintainers
4
Weekly downloads
 
Created

What is apollo-link-rest?

The apollo-link-rest package allows you to integrate REST endpoints into your Apollo Client setup. It provides a way to fetch data from RESTful APIs and use it within your GraphQL queries, making it easier to transition from REST to GraphQL or to use both in a hybrid approach.

What are apollo-link-rest's main functionalities?

Basic REST Query

This code demonstrates how to set up a basic Apollo Client with a RestLink to fetch data from a REST endpoint. The query fetches user data from the '/user' endpoint.

const { ApolloClient, InMemoryCache } = require('@apollo/client');
const { RestLink } = require('apollo-link-rest');

const restLink = new RestLink({ uri: 'https://api.example.com/' });

const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache()
});

client.query({
  query: gql`
    query GetUser {
      user @rest(type: "User", path: "/user") {
        id
        name
      }
    }
  `
}).then(response => console.log(response));

Handling POST Requests

This code demonstrates how to handle POST requests using apollo-link-rest. It sends a POST request to the '/user' endpoint to create a new user.

client.mutate({
  mutation: gql`
    mutation CreateUser($input: CreateUserInput!) {
      createUser(input: $input) @rest(type: "User", path: "/user", method: "POST") {
        id
        name
      }
    }
  `,
  variables: {
    input: {
      name: 'New User'
    }
  }
}).then(response => console.log(response));

Custom Headers

This code shows how to set custom headers for your REST requests, such as an Authorization header for authenticated requests.

const restLink = new RestLink({
  uri: 'https://api.example.com/',
  headers: {
    Authorization: 'Bearer YOUR_TOKEN'
  }
});

Other packages similar to apollo-link-rest

FAQs

Package last updated on 15 Nov 2022

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc