@8base/apollo-links 中文文档教程

发布于 3年前 浏览 28 项目主页 更新于 3年前

8base Apollo Links

一组 Apollo 链接,用于与 8base API 进行更高效的通信。

API

Table of Contents

Extends ApolloLink

Token Refresh Link 在过期时更新身份验证令牌。

Parameters

  • options TokenRefreshLinkOptions The token refresh link options.
    • options.getRefreshTokenParameters Function Used to refresh token parameters.
    • options.onAuthSuccess Function Callback which is executed when an attempt to refresh authentication is successful.
    • options.onAuthError Function? Callback which is executed when an attempt to refresh authentication fails.

SuccessLink 在每次成功操作时调用处理程序。

Parameters

  • successHandler Function success handler.

Handler

成功处理程序采用以下参数:

  • operation Operation apollo graphql operation.

Usage

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',
    }),
  ),
])

8base Apollo Links

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

API

Table of Contents

Extends ApolloLink

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

Parameters

  • options TokenRefreshLinkOptions The token refresh link options.
    • options.getRefreshTokenParameters Function Used to refresh token parameters.
    • options.onAuthSuccess Function Callback which is executed when an attempt to refresh authentication is successful.
    • options.onAuthError Function? Callback which is executed when an attempt to refresh authentication fails.

SuccessLink calls handler on every successful operation.

Parameters

  • successHandler Function success handler.

Handler

Success handler takes the following parameters:

  • operation Operation apollo graphql operation.

Usage

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',
    }),
  ),
])
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文