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

setting query headers fails on server #352

Closed
atarek12 opened this issue Aug 22, 2024 · 3 comments
Closed

setting query headers fails on server #352

atarek12 opened this issue Aug 22, 2024 · 3 comments

Comments

@atarek12
Copy link

I am trying to set some headers on the query inside RSC, I have tried two methods but with no luck.

  • I have tried setting the locale header while firing the query itself
  const { data } = await query(
    {
      query: Document,
      variables: { id },
      context: { headers: { locale } },
    }
  );
  • I have also tried to set a common headers link
import { registerApolloClient, ApolloClient } from "@apollo/experimental-nextjs-app-support";
import { ApolloLink, from } from "@apollo/client";
import { cookies } from "next/headers";

const localeLink = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => ({
    headers: {
      ...headers,
      locale: cookies().get(CookieEnum.LANG)?.value,
    },
  }));

  return forward(operation);
});

const link = from([localeLink, httpLink]);

export const { getClient, query, PreloadQuery } = registerApolloClient(() => {
  return new ApolloClient({
    cache,
    link,
  });
});

What I expect is to set the locale headers properly

@phryneas
Copy link
Member

Both of these should work - are you sure that cookies().get(CookieEnum.LANG)?.value returns something? Could you log that?

Also, generally I'd recommend to move the links into registerApolloClient, and you have to do the new InMemoryCache call inside of registerApolloClient, or you will leak potentially sensitive data between users.

@atarek12
Copy link
Author

Thank you, it works now after moving links and memory cache initialization inside the registerApolloClient function 🙏

Copy link

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