Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo-link is called every time when "setLink" method is called #8182

Closed
Moniv9 opened this issue May 10, 2021 · 1 comment
Closed

apollo-link is called every time when "setLink" method is called #8182

Moniv9 opened this issue May 10, 2021 · 1 comment

Comments

@Moniv9
Copy link

Moniv9 commented May 10, 2021

Intended outcome:
Trying to set new link after apollo client initialisation by using the public method setLink. With this method, any new link
should be invoked only once.

Actual outcome:
By using the above method, can see new apollo-link is being called every time setLink is called/set.

How to reproduce the issue:
Here is the code to reproduce it.

Test component

import * as React from "react";
import gql from "graphql-tag";
import { ApolloLink, useApolloClient } from "@apollo/client";

export const Test = () => {
  const OtherLink = new ApolloLink((operation, forward) => {
    console.log("custom link is called");

    return forward(operation);
  });

  const client = useApolloClient();

  /* by calling it twice, we will observe that "custom link is called" is called twice.
     Ideally it should be called only once */
  client.setLink(ApolloLink.from([OtherLink, client.link]));
  client.setLink(ApolloLink.from([OtherLink, client.link]));

  client
    .query({
      query: gql`
        {
          widgets
        }
      `
    })
    .then((data) => {
      //console.log(data);
    });

  return <>Test</>;
};

Parent component

import "./styles.css";
import {
  ApolloClient,
  ApolloProvider,
  ApolloLink,
  InMemoryCache,
  Observable
} from "@apollo/client";
import { Test } from "./Test";

export default function App() {
  const link = new ApolloLink((operation) => {
    return new Observable((observer) => {
      observer.next({
        data: {
          widgets: [{ name: "Widget 1" }, { name: "Widget 2" }]
        }
      });
      observer.complete();
    });
  });

  const apolloClient = new ApolloClient({
    cache: new InMemoryCache(),
    link: ApolloLink.from([link])
  });

  return (
    <ApolloProvider client={apolloClient}>
      <div className="App">
        <h1>Hello CodeSandbox</h1>
        <Test />
      </div>
    </ApolloProvider>
  );
}

Codesandbox

https://codesandbox.io/s/wizardly-tu-94i5f?file=/src/App.js

Versions

  System:
    OS: macOS 11.2.3
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    npm: 6.14.11 - ~/.nvm/versions/node/v12.13.1/bin/npm
  Browsers:
    Chrome: 90.0.4430.93
    Firefox: 83.0
    Safari: 14.0.3
  npmPackages:
    @apollo/client: 3.3.15 => 3.3.15 
    @apollo/react-common: ^3.1.4 => 3.1.4 
    @apollo/react-hooks: 4.0.0 => 4.0.0 
    apollo-link-timeout: 4.0.0 => 4.0.0 
@hwillson
Copy link
Member

@Moniv9 for now this is something you'll need to manage in your code. setLink will add whatever link you pass it to the link chain; it won't try to deduplicate links. If you think it should, please open a feature request in http://github.com/apollographql/apollo-feature-requests/issues. Thanks!

@brainkim brainkim self-assigned this Sep 17, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants