@habx/apollo-multi-endpoint-link

中文文档
2.9.0发布于 2022/11/14ISCTypeScriptESM + CJS零依赖近 30 天下载 5.8万

Apollo link which add an api directive to fetch data from multi endpoints

原始使用文档
文档目录5 节
  1. Apollo link which add an api directive to fetch data from m…
  2. Why ?
  3. Install
  4. Setup
  5. Queries

CircleCI Version Size License

Why ?

We wrote an article about why and how we did this link if you want more details.

Install

   npm i @habx/apollo-multi-endpoint-link

Setup

import { createHttpLink } from "apollo-link-http";

new ApolloClient({
 link: ApolloLink.from([
   new MultiAPILink({
       endpoints: {
           housings: 'https://housings.api',
           projects: 'https://projects.api',
           ...
       },
       createHttpLink: () => createHttpLink(),
     }),
 ])
})

NB: Since default value of httpSuffix is /graphql, endpoints above will be transformed to https://housings.api/graphql and https://projects.api/graphql. If you do not have common suffix, you can pass an empty string as httpSuffix to avoid this transformation.

NB 2: Supports apollo-link-rest library

API
new MultiAPILink(config, request);
config
ParameterDescriptionDefaultRequired
endpointsDictionary of endpointsYes
defaultEndpointDefault endpointNo
createHttpLinkFunction to generate http link like apollo-link-httpYes
createWsLinkFunction to generate wsLink like apollo-link-wsNo
wsSuffixSuffix added to endpoint for subscriptions queries/graphql/subscriptionsNo
httpSuffixSuffix added to endpoint for http queries/graphqlNo
getContextCallback function called to set custom context like headersNo
prefixTypenamesAdd name argument passed in @api directive to every __typename contained in network data responseNo

Queries

Query with static api name :

query projectList($params: Params) @api(name: projects) {
  projects(params: $params) {
    nodes {
      id
      name
    }
  }
}
const response = useQuery(myQuery);

Query with dynamic api name

query projectList($params: Params) @api(contextKey: "apiName") {
  projects(params: $params) {
    nodes {
      id
      name
    }
  }
}
const response = useQuery(myQuery, { context: { apiName: "projects" } });

Setting custom context

Sometimes you might need to set custom apollo link context like headers for authentication purpose. This link allows it by doing as following.

new MultiAPILink({
    getContext: (endpoint) => {
      if (endpoint === 'yourendpoint-with-auth') {
        return ({
          headers: {
            'Authorization': 'xxxx',
          }
        })
      }
      return {}
    },
    ...
})

版本与发布记录

@habx/apollo-multi-endpoint-link 共发布 13 个版本,首次发布于 2020/03/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.9.0latest2022/11/144 年前
2.8.02022/09/284 年前
2.7.02022/03/184 年前
2.6.02022/01/214 年前
2.5.02021/06/215 年前
2.4.02021/01/275 年前

关于 @habx/apollo-multi-endpoint-link 的常见问题

@habx/apollo-multi-endpoint-link 是什么?

Apollo link which add an api directive to fetch data from multi endpoints

如何安装 @habx/apollo-multi-endpoint-link?

在项目目录运行 npm install @habx/apollo-multi-endpoint-link,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@apollo/client(^3.7.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@habx/apollo-multi-endpoint-link 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@habx/apollo-multi-endpoint-link 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@habx/apollo-multi-endpoint-link 可以商用吗?

@habx/apollo-multi-endpoint-link 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@habx/apollo-multi-endpoint-link 的下载量有多少?

根据 npm 官方下载统计,@habx/apollo-multi-endpoint-link 在 2026/10/17 – 2026/11/15 这 30 天内被下载 57,722 次,日均约 1,924 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@habx/apollo-multi-endpoint-link 还在维护吗?

最新版本 2.9.0 发布于 2022/11/14(4 年前);至今共发布 13 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。