# http-link-dataloader

> [![CircleCI](https://circleci.com/gh/graphcool/http-link-dataloader.svg?style=shield)](https://circleci.com/gh/graphcool/http-link-dataloader) [![npm version](https://badge.fury.io/js/http-link-dataloader.svg)](https://badge.fury.io/js/http-link-dataloade

Latest version **0.1.6** (published 2018-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-link-dataloader
pnpm add http-link-dataloader
yarn add http-link-dataloader
bun add http-link-dataloader
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2018-10-24 |
| First published | 2018-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 30 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 77 |
| Author | Tim Suchanek |
| Maintainers | schickling, timsuchanek |
| Keywords | graphql, request, fetch, graphql-client, apollo |

## Links

- npm: https://www.npmjs.com/package/http-link-dataloader
- Repository: https://github.com/prisma/http-link-dataloader
- Homepage: https://github.com/graphcool/http-link-dataloader
- Issues: https://github.com/graphcool/http-link-dataloader/issues
- npm.io page: https://npm.io/package/http-link-dataloader

## Dependencies (3)

- [dataloader](https://npm.io/package/dataloader.md) ^1.4.0
- [apollo-link](https://npm.io/package/apollo-link.md) ^1.2.3
- [cross-fetch](https://npm.io/package/cross-fetch.md) 2.2.2

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.1.6 (latest) — 2018-10-24
- 0.1.5 — 2018-08-30
- 0.1.4 — 2018-08-13
- 0.1.3 — 2018-03-04
- 0.1.2 — 2018-03-01
- 0.1.1 — 2018-02-08
- 0.1.0 — 2018-02-02
- 0.0.1 — 2018-02-02
- 0.0.0 — 2018-02-02

## README

# http-link-dataloader

[![CircleCI](https://circleci.com/gh/graphcool/http-link-dataloader.svg?style=shield)](https://circleci.com/gh/graphcool/http-link-dataloader) [![npm version](https://badge.fury.io/js/http-link-dataloader.svg)](https://badge.fury.io/js/http-link-dataloader)

📚📡 HTTP Apollo Link with batching & caching provided by dataloader.

## Idea

A Apollo Link that batches requests both in Node and the Browser.
You may ask what's the difference to [apollo-link-batch-http](https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-batch-http).
Instead of having a time-frame/fixed cache size based batching approach like in `apollo-link-batch-http`, this library uses [dataloader](https://github.com/facebook/dataloader) for batching requests. It is a more generic approach just depending on the Node.JS event loop that batches all consecutive queries directly.
The main use-case for this library is the usage from a [`graphql-yoga`](https://github.com/graphcool/graphql-yoga) server using [`prisma-binding`](https://github.com/graphcool/prisma-binding), but it can be used in any environment, even the browser as the latest `dataloader` version also runs in browser environments.

## Usage

```ts
import { HTTPLinkDataloader } from 'http-link-dataloader'

const link = new HTTPLinkDataloader()

const token = 'Auth Token'

const httpLink = new HTTPLinkDataloader({
  uri: `api endpoint`,
  headers: { Authorization: `Bearer ${token}` },
})
```

## Caching behavior

Note that the dataloader cache aggressively caches everything! That means if you don't want to cache anymore, just create a new instance of `BatchedHTTPLink`.
A good fit for this is every incoming HTTP request in a server environment - on each new HTTP request a new `BatchedHTTPLink` instance is created.

## Batching

This library uses array-based batching. Querying 2 queries like this creates the following payload:

```graphql
query {
  Item(id: "1") {
    id
    name
    text
  }
}
```

```graphql
query {
  Item(id: "2") {
    id
    name
    text
  }
}
```

Instead of sending 2 separate http requests, it gets combined into one:

```js
;[
  {
    query: `query {
      Item(id: "1") {
        id
        name
        text
      }
    }`,
  },
  {
    query: `query {
      Item(id: "2") {
        id
        name
        text
      }
    }`,
  },
]
```

**Note that the GraphQL Server needs to support the array-based batching!**
(Prisma supports this out of the box)

## Even better batching

A batching that would even be faster is alias-based batching. Instead of creating the array described above, it would generate something like this:

```js
{
  query: `
    query {
      item_1: Item(id: "1") {
        id
        name
        text
      }
      item_2: Item(id: "2") {
        id
        name
        text
      }
    }`
}
```

This requires a lot more logic and resolution magic for aliases, but would be a lot faster than the array based batching as our tests have shown!
Anyone intersted in working on this is more than welcome to do so!
You can either create an issue or just reach out to us in slack and join our #contributors channel.

## Help & Community [![Slack Status](https://slack.graph.cool/badge.svg)](https://slack.graph.cool)

Join our [Slack community](http://slack.graph.cool/) if you run into issues or have questions. We love talking to you!

![](http://i.imgur.com/5RHR6Ku.png)

---
_Source: https://npm.io/package/http-link-dataloader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
