# apollo-link

> Flexible, lightweight transport layer for GraphQL

Latest version **1.2.14** (published 2020-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install apollo-link
pnpm add apollo-link
yarn add apollo-link
bun add apollo-link
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.14 |
| Published | 2020-04-09 |
| First published | 2017-06-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 98 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1423 |
| Author | Evans Hauser |
| Maintainers | apollo-bot, evanshauser, jbaxleyiii, peggyrayzis |

## Links

- npm: https://www.npmjs.com/package/apollo-link
- Repository: https://github.com/apollographql/apollo-link
- Homepage: https://github.com/apollographql/apollo-link#readme
- Issues: https://github.com/apollographql/apollo-link/issues
- npm.io page: https://npm.io/package/apollo-link

## Dependencies (4)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [ts-invariant](https://npm.io/package/ts-invariant.md) ^0.4.0
- [apollo-utilities](https://npm.io/package/apollo-utilities.md) ^1.3.0
- [zen-observable-ts](https://npm.io/package/zen-observable-ts.md) ^0.8.21

## Recent versions

- 1.2.14 (latest) — 2020-04-09
- 1.2.9-alpha.1 (alpha) — 2019-02-01
- 1.0.2 (prerelease) — 2017-11-13
- 0.6.1-beta.6 (beta) — 2017-09-28
- 1.2.13 — 2019-09-06
- 1.2.12 — 2019-06-14
- 1.2.11 — 2019-03-14
- 1.2.10 — 2019-03-14
- 1.2.9 — 2019-03-05
- 1.2.9-alpha.0 — 2019-02-01
- 1.2.8 — 2019-02-01
- 1.2.7 — 2019-02-01
- 1.2.6 — 2018-12-15
- 1.2.5 — 2018-12-13
- 1.2.4 — 2018-11-21
- … 33 more at https://npm.io/package/apollo-link/versions

## README

# apollo-link

## Purpose

`apollo-link` is a standard interface for modifying control flow of GraphQL requests and fetching GraphQL results, designed to provide a simple GraphQL client that is capable of extensions.
The targeted use cases of `apollo-link` are highlighted below:

* fetch queries directly without normalized cache
* network interface for Apollo Client
* network interface for Relay Modern
* fetcher for

Apollo Link is the interface for creating new links in your application.

The client sends a request as a method call to a link and can recieve one or more (in the case of subscriptions) responses from the server. The responses are returned using the Observer pattern.

![Apollo Link Chain](https://cdn-images-1.medium.com/max/1600/1*62VLGUaU-9ULCoBCGvgdkQ.png)

Results from the server can be provided by calling `next(result)` on the observer. In the case of a network/transport error (not a GraphQL Error) the `error(err)` method can be used to indicate a response will not be recieved. If multiple responses are not supported by the link, `complete()` should be called to inform the client no further data will be provided.

In the case of an intermediate link, a second argument to `request(operation, forward)` is the link to `forward(operation)` to. `forward` returns an observable and it can be returned directly or subscribed to.

```js
forward(operation).subscribe({
  next: result => {
    handleTheResult(result)
  },
  error: error => {
    handleTheNetworkError(error)
  },
});
```

## Implementing a basic custom transport

```js
import { ApolloLink, Observable } from 'apollo-link';

export class CustomApolloLink extends ApolloLink {
  request(operation /*, forward*/) {
    //Whether no one is listening anymore
    let unsubscribed = false;

    return new Observable(observer => {
      somehowGetOperationToServer(operation, (error, result) => {
        if (unsubscribed) return;
        if (error) {
          //Network error
          observer.error(error);
        } else {
          observer.next(result);
          observer.complete(); //If subscriptions not supported
        }
      });

      function unsubscribe() {
        unsubscribed = true;
      }

      return unsubscribe;
    });
  }
}
```

## Installation

`npm install apollo-link --save`

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