Socket
Book a DemoInstallSign in
Socket

apollo-client-mux

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-client-mux

multiple GraphQL endpoints within a single Apollo Client instance

latest
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

Apollo Client Mux

apollo-client-mux is a library designed to handle multiple GraphQL endpoints within a single Apollo Client instance. It allows you to set different caches and links for different endpoints.

Usage

Step 1: add @endpoint directive to operation documents

// codegen.<yourEndpointName>.ts
import type { CodegenConfig } from "@graphql-codegen/cli";
import { addEndpointDirectiveForCodegen } from "apollo-client-mux/transform";

const config: CodegenConfig = {
  // ...
  generates: {
    "src/gql/<yourEndpoint>/": { // <- use your endopint name
      preset: "client",
      documentTransforms: [
        {
          transform: addEndpointDirectiveForCodegen({
            endpointName: "yourEndpoint", // <- use your endopint name
          }),
        },
      ],
    },
  },
};

export default config;
import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";
import { ApolloCacheMux, withCacheMux } from "apollo-client-mux";

// create ApolloCache
const InMemoryCacheMux = withCacheMux(InMemoryCache);
const yourEndpointCache = new InMemoryCache();
const cache = new InMemoryCacheMux({
  mux:{
    caches: {
      yourEndpoint: yourEndpointCache, // <-- use your endopint name
    },
  }
  // ... options for default cache
});

// create ApolloLink
const yourEndpointHttpLinnk = new HttpLink({ /* ... */ });
const defaultHttpLinnk = new HttpLink({ /* ... */ });
const link = createApolloLinkMux({
  links: {
    yourEndpoint: yourEndpointHttpLink, // <-- use your endopint name
  },
  defaultLink: defaultHttpLink;
});

// create Apollo Client
const client = new ApolloClient({
  cache,
  link,
});

Keywords

graphql

FAQs

Package last updated on 27 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts