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

i want to pass the params through getClient #175

Closed
VolcanoDiver opened this issue Jan 22, 2024 · 3 comments
Closed

i want to pass the params through getClient #175

VolcanoDiver opened this issue Jan 22, 2024 · 3 comments

Comments

@VolcanoDiver
Copy link

how do i pass the token data to getClient so that i can add auth headers to ApolloLink?

@phryneas
Copy link
Member

phryneas commented Jan 22, 2024

Hi @VolcanoDiver :)

Most likely, you shouldn't go about it that way.

If you want to specify your authentication headers at call site, you can do so by providing context:

getClient().query({
  query,
  context: { headers: { authentication: "..." } },
});

Or (and this is probably what you will do instead in most cases), you can just call cookies (or whatever other way of getting your auth headers you have) inside of a setContext link

export const { getClient } = registerApolloClient(() => {
  const httpLink = createHttpLink({
    uri: "/graphql",
  });

  const authLink = setContext((_, { headers }) => {
    const token = cookies().get("myCookie");
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : "",
      },
    };
  });

  return new ApolloClient({
    link: authLink.concat(httpLink),
    cache: new InMemoryCache(),
  });
});

@phryneas
Copy link
Member

I'm doing some housekeeping so I'm closing some older issues that haven't seen activity in a while.
If this is still relevant, please feel free to reopen the issue.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants