# @8base/apollo-links

> A collection of Apollo links for more efficient communication with the 8base API.

Latest version **1.6.2** (published 2022-03-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @8base/apollo-links
pnpm add @8base/apollo-links
yarn add @8base/apollo-links
bun add @8base/apollo-links
```

## 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.6.2 |
| Published | 2022-03-22 |
| First published | 2018-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 44 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Maintainers | 8base-bot, selnapenek, 8basetimmyers, s1owjke, eugene.antonevich, ilazarev |

## Links

- npm: https://www.npmjs.com/package/@8base/apollo-links
- Repository: https://github.com/8base/sdk
- Homepage: https://github.com/8base/sdk/tree/master/packages/apollo-links#readme
- Issues: https://github.com/8base/sdk/issues
- npm.io page: https://npm.io/package/@8base/apollo-links

## Dependencies (9)

- [ramda](https://npm.io/package/ramda.md) ^0.26.1
- [apollo-link](https://npm.io/package/apollo-link.md) 1.2.11
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.9.2
- [@8base/utils](https://npm.io/package/@8base/utils.md) ^1.6.2
- [apollo-link-error](https://npm.io/package/apollo-link-error.md) ^1.1.10
- [zen-observable-ts](https://npm.io/package/zen-observable-ts.md) ^0.8.18
- [@8base/error-codes](https://npm.io/package/@8base/error-codes.md) ^1.0.17
- [@8base/apollo-link-ws](https://npm.io/package/@8base/apollo-link-ws.md) 1.0.20
- [@8base/subscriptions-transport-ws](https://npm.io/package/@8base/subscriptions-transport-ws.md) ^0.9.16

## Recent versions

- 1.6.2 (latest) — 2022-03-22
- 3.1.0-beta.0 (beta) — 2023-11-24
- 3.0.0-beta.3 — 2023-04-12
- 3.0.0-beta.1 — 2023-04-12
- 3.0.0-beta.0 — 2022-06-22
- 2.7.0-beta.0 — 2022-06-22
- 2.6.6 — 2022-03-18
- 2.6.5 — 2021-10-29
- 2.6.3 — 2021-10-29
- 2.6.2 — 2021-10-29
- 1.6.1 — 2021-10-18
- 2.5.0 — 2021-08-23
- 2.4.0 — 2021-08-17
- 1.5.0 — 2021-08-17
- 2.3.0 — 2021-08-10
- … 219 more at https://npm.io/package/@8base/apollo-links/versions

## README

# 8base Apollo Links

A collection of Apollo links for more efficient communication with the 8base API.

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

-   [TokenRefreshLink](#tokenrefreshlink)
    -   [Parameters](#parameters)

### TokenRefreshLink

**Extends ApolloLink**

Token Refresh Link renew authentication token when it's expired.

#### Parameters

-   `options` **TokenRefreshLinkOptions** The token refresh link options.
    -   `options.getRefreshTokenParameters` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Used to refresh token parameters.
    -   `options.onAuthSuccess` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Callback which is executed when an attempt to refresh authentication is successful.
    -   `options.onAuthError` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Callback which is executed when an attempt to refresh authentication fails.

### SuccessLink

SuccessLink calls handler on every successful operation.

#### Parameters

- `successHandler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** success handler.

#### Handler

Success handler takes the following parameters:

- `operation` **Operation** apollo graphql operation.

## Usage
```js
import { ApolloLink } from 'apollo-link';
import { BatchHttpLink } from 'apollo-link-batch-http';
import { getMainDefinition } from 'apollo-utilities';
import {
  AuthLink,
  FileUploadLink,
  SuccessLink,
} from '@8base/apollo-links';

const successHandler = ({ operation }) => {
  console.log(operation.getContext().someUsefulData);
};

const getAuthState = () => ({
  workspaceId: '',
  email: '',
  token: '',
});

const getRefreshTokenParameters = () => ({
  email: '',
  refreshToken: '',
});

const authSuccessHandler = ({ token, refreshToken}) => {
  console.log({ token, refreshToken });
}

const authErrorHandler = () => {
  console.log('Auth error');
}


const links = ApolloLink.from([
  new FileUploadLink(),
  new SuccessLink(successHandler),
  new AuthLink({
    getAuthState: getAuthState,
    getRefreshTokenParameters: getRefreshTokenParameters,
    onAuthSuccess: authSuccessHandler,
    onAuthError: authErrorHandler,
  }),
  ApolloLink.split(
    ({ query }) => {
      const definition = getMainDefinition(query);

      return (
        definition.kind === 'OperationDefinition' &&
        definition.operation === 'subscription'
      );
    },
    new SubscriptionLink({
      uri: 'wss://api-ws.8base.com',
      getAuthState: getAuthState,
      onAuthError: authErrorHandler,
    }),
    new BatchHttpLink({
      uri: 'https://api.8base.com',
    }),
  ),
])

```

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