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

apollo-ds-rest

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-ds-rest

[![CI](https://github.com/StarpTech/apollo-datasource-rest/actions/workflows/ci.yml/badge.svg)](https://github.com/StarpTech/apollo-datasource-rest/actions/workflows/ci.yml)

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Apollo REST Data Source

CI

Optimized REST Data Source for Apollo Server

  • Optimized for JSON REST
  • HTTP/1 Keep-alive agents for socket reuse
  • HTTP/2 support (requires Node.js 15.10.0 or newer)
  • Uses Got a modern HTTP Client shipped with:
    • Retry mechanism
    • Request cancellation
    • Timeout handling
    • RFC 7234 compliant HTTP caching
  • LRU Cache with ttl to memoize GET requests within the same graphql request
  • AbortController to cancel all running requests
  • Support for Apollo Cache Storage backend

Documentation

View the Apollo Server documentation for data sources for more details.

Usage

To get started, install the apollo-ds-rest package:

npm install apollo-ds-rest

To define a data source, extend the RESTDataSource class and implement the data fetching methods that your resolvers require. Data sources can then be provided via the dataSources property to the ApolloServer constructor, as demonstrated in the section below.

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      moviesAPI: new MoviesAPI()
    };
  },
});

Your implementation of these methods can call on convenience methods built into the RESTDataSource class to perform HTTP requests, while making it easy to pass different options and handle errors.

const { RESTDataSource } = require("apollo-ds-rest");

class MoviesAPI extends RESTDataSource {
  constructor() {
    // global client options
    super({
      timeout: 2000,
      http2: true,
      headers: {
        "X-Client": "client",
      },
    });
    this.baseURL = "https://movies-api.example.com";
  }

  cacheKey() {}

  // lifecycle hooks for logging, tracing and request manipulation
  didEncounterError() {}
  async willSendRequest() {}
  async didReceiveResponse() {}

  async getMovie(id) {
    return this.get(`/movies/${id}`, {
      headers: {
        "X-Foo": "bar",
      },
      timeout: 3000,
    });
  }
}

FAQs

Package last updated on 10 Jun 2021

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