{"id":22489872,"url":"https://github.com/habx/apollo-multi-endpoint-link","last_synced_at":"2025-04-05T06:04:31.590Z","repository":{"id":37049373,"uuid":"245132992","full_name":"habx/apollo-multi-endpoint-link","owner":"habx","description":"⛓ Apollo link which add an api directive to fetch data from multi endpoints","archived":false,"fork":false,"pushed_at":"2023-12-15T11:23:03.000Z","size":3572,"stargazers_count":135,"open_issues_count":9,"forks_count":14,"subscribers_count":3,"default_branch":"dev","last_synced_at":"2025-03-29T05:04:06.239Z","etag":null,"topics":["apollo-client","apollo-link","apollographql","graphql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/habx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-05T10:27:59.000Z","updated_at":"2025-03-13T11:59:56.000Z","dependencies_parsed_at":"2024-06-18T18:24:42.544Z","dependency_job_id":"0e05686b-7876-4c84-bbc0-8b9d61e1d5fe","html_url":"https://github.com/habx/apollo-multi-endpoint-link","commit_stats":{"total_commits":535,"total_committers":31,"mean_commits":"17.258064516129032","dds":0.6242990654205607,"last_synced_commit":"552ad6bdde5ebfbcd4f732d818dafdbe2a73cb29"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":"habx/lib-ts-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habx%2Fapollo-multi-endpoint-link","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habx%2Fapollo-multi-endpoint-link/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habx%2Fapollo-multi-endpoint-link/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habx%2Fapollo-multi-endpoint-link/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/habx","download_url":"https://codeload.github.com/habx/apollo-multi-endpoint-link/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294537,"owners_count":20915340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo-client","apollo-link","apollographql","graphql"],"created_at":"2024-12-06T17:21:00.894Z","updated_at":"2025-04-05T06:04:31.574Z","avatar_url":"https://github.com/habx.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003c!-- @format --\u003e\n\n## Apollo link which add an api directive to fetch data from multi endpoints\n\n\u003cimg src=\"https://res.cloudinary.com/habx/image/upload/v1597742839/blog/tech/apollo-multi-link.jpg\" /\u003e\n\n[![CircleCI](https://img.shields.io/circleci/build/github/habx/apollo-multi-endpoint-link)](https://app.circleci.com/pipelines/github/habx/apollo-multi-endpoint-link)\n[![Version](https://img.shields.io/npm/v/@habx/apollo-multi-endpoint-link)](https://www.npmjs.com/package/@habx/apollo-multi-endpoint-link)\n[![Size](https://img.shields.io/bundlephobia/min/@habx/apollo-multi-endpoint-link)](https://bundlephobia.com/result?p=@habx/apollo-multi-endpoint-link)\n[![License](https://img.shields.io/github/license/habx/apollo-multi-endpoint-link)](/LICENSE)\n\n### Why ?\n\nWe wrote [an article](https://www.habx.com/tech/micro-graphql-schema) about why and how we did this link if you want more details.\n\n### Install\n\n```bash\n   npm i @habx/apollo-multi-endpoint-link\n```\n\n### Setup\n\n```typescript\nimport { createHttpLink } from \"apollo-link-http\";\n\nnew ApolloClient({\n link: ApolloLink.from([\n   new MultiAPILink({\n       endpoints: {\n           housings: 'https://housings.api',\n           projects: 'https://projects.api',\n           ...\n       },\n       createHttpLink: () =\u003e createHttpLink(),\n     }),\n ])\n})\n```\n\nNB: Since default value of `httpSuffix` is `/graphql`, endpoints above will be transformed to `https://housings.api/graphql` and `https://projects.api/graphql`.\nIf you do not have common suffix, you can pass an empty string as `httpSuffix` to avoid this transformation.\n\nNB 2: Supports [apollo-link-rest](https://github.com/apollographql/apollo-link-rest) library\n\n##### API\n\n```typescript\nnew MultiAPILink(config, request);\n```\n\n###### config\n\n| Parameter       | Description                                                                                                                | Default                | Required |\n| --------------- | -------------------------------------------------------------------------------------------------------------------------- | ---------------------- | -------- |\n| endpoints       | Dictionary of endpoints                                                                                                    |                        | Yes      |\n| defaultEndpoint      | Default endpoint                                                                                                    |                        | No      |\n| createHttpLink  | Function to generate http link like [apollo-link-http](https://www.apollographql.com/docs/link/links/http/)                |                        | Yes      |\n| createWsLink    | Function to generate wsLink like [apollo-link-ws](https://www.apollographql.com/docs/link/links/ws/)                       |                        | No       |\n| wsSuffix        | Suffix added to endpoint for subscriptions queries                                                                         | /graphql/subscriptions | No       |\n| httpSuffix      | Suffix added to endpoint for http queries                                                                                  | /graphql               | No       |\n| getContext      | Callback function called to set custom [context](https://www.apollographql.com/docs/link/links/http/#context) like headers |                        | No       |\n| prefixTypenames | Add name argument passed in `@api` directive to every \\_\\_typename contained in network data response                      |                        | No       |\n\n### Queries\n\n#### Query with static api name :\n\n```graphql\nquery projectList($params: Params) @api(name: projects) {\n  projects(params: $params) {\n    nodes {\n      id\n      name\n    }\n  }\n}\n```\n\n```ts\nconst response = useQuery(myQuery);\n```\n\n#### Query with dynamic api name\n\n```graphql\nquery projectList($params: Params) @api(contextKey: \"apiName\") {\n  projects(params: $params) {\n    nodes {\n      id\n      name\n    }\n  }\n}\n```\n\n```ts\nconst response = useQuery(myQuery, { context: { apiName: \"projects\" } });\n```\n\n#### Setting custom context\n\nSometimes you might need to set custom [apollo link context](https://www.apollographql.com/docs/link/links/http/#context) like `headers` for authentication purpose.\nThis link allows it by doing as following.\n\n```typescript\nnew MultiAPILink({\n    getContext: (endpoint) =\u003e {\n      if (endpoint === 'yourendpoint-with-auth') {\n        return ({\n          headers: {\n            'Authorization': 'xxxx',\n          }\n        })\n      }\n      return {}\n    },\n    ...\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabx%2Fapollo-multi-endpoint-link","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhabx%2Fapollo-multi-endpoint-link","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabx%2Fapollo-multi-endpoint-link/lists"}