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

apollo-datasource-configurable-rest

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-datasource-configurable-rest

configurable rest datasource

  • 1.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

Apollo Configurable REST Data Source

This rest datasource is an extension of apollo's rest data source. So you have all the options which come from apollo's rest data source and even more:

This extension aims to simplify data source definitions in your apollo server by configuring them via class parameters.

Installation

yarn add apollo-datasource-configurable-rest
# or
npm i apollo-datasource-configurable-rest

Usage

One key thought, besides the configuration via class parameter, is that all keywords which are prefixed with a $ are replaced by the query argument with the same name.

For example if you have a query argument named language and you use $language in your definitions it will be replaced by the query argument.

Simple example

The following example shows how to configure a get request with url params and headers:

import { ConfigurableRESTDataSource } from 'apollo-datasource-configurable-rest'

export class MyRestDatasource extends ConfigurableRESTDataSource {

  // $book will be replaced by the query argument named "book"
  baseURL = 'https://api.com/texts/$book'

  // if your query contains optional arguments 
  // you can set a default value for these arguments here
  defaultArgs = {
    book: 'title-of-book'
    language: 'de',
    chapter: 1,
  }

  // url parameter definitions
  // with the default arguments this would result in:
  // https://api.com/texts/title-of-book?lng=de&chapter=1
  params = {
    lng: "$language",
    chapter: "$chapter",
  }

  // header definitions
  // the $accessToken keyword would replaced by
  // the query argument named "accessToken"
  headers = {
    Authorization: 'Bearer $accessToken',
    'Content-Type': 'application/json',
  }

  // this is for your resolver
  async fetchChapters(args: QueryArgs) {
    // the configuredGET command now replaces the arguments
    // in the url, parameters and headers and fetches the api
    return this.configuredGET(args)
  }
}

Configured fetchers

There are fetchers GET, DELETE, POST, PUT and PATCH.

async configuredGET<TResult = any>(args: Partial<TArgs>, options?: RequestInitOptions): Promise<TResult>

async configuredDELETE<TResult = any>(args: Partial<TArgs>, options?: RequestInitOptions): Promise<TResult>

async configuredPOST<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

async configuredPUT<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

async configuredPATCH<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

Caching

You can configure a cache time for the GET and DELETE fetcher:

public fetchChapters(args: QueryArgs) {
  this.configuredGET(args, { cacheTime: 3000 })
}

Use Generics

You can define typings for your query arguments, url parameters, headers and body definitions:

import { ConfigurableRESTDataSource } from 'apollo-datasource-configurable-rest'

type QueryArgs = {
  book?: string,
  language?: string,
  chapter?: number,
  accessToken: string,
}

type Params = {
  lng: string,
  chapter: number,
}

type Headers = {
  Authorization: string 
}

type Body = {
  myBody: {
    structure: string,
    num: number,
  }
}

export class MyRestDatasource extends ConfigurableRESTDataSource<
  QueryArgs, Params, Headers, Body
> {
  // ...
}


Keywords

FAQs

Package last updated on 14 Sep 2020

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