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-client 3.7.0 modifies things in context? #10166

Closed
yshrsmz opened this issue Oct 6, 2022 · 1 comment · Fixed by #10215
Closed

apollo-client 3.7.0 modifies things in context? #10166

yshrsmz opened this issue Oct 6, 2022 · 1 comment · Fixed by #10215
Assignees
Labels
🔍 investigate Investigate further

Comments

@yshrsmz
Copy link

yshrsmz commented Oct 6, 2022

I was trying to update apollo-client from 3.6.10 to 3.7.0, and some tests started to fail.

Intended outcome:

Say I'm running ApolloClient in an express server, and want to forward cookies from the server request to ApolloClient's request.

module.exports = (app) => {
  app.get("/test", async (req, res, next) => {
    const client = createClient("https://example.com/graphql");

    const result = await client.query({
      query: query,
      context: {
        serverRequest: req,
        serverResponse: res
      }
    });

    //...
  });
};

and I have a following custom ApolloLink implementation to forward cookie.

const headerForwardLink = () => {
  const link = new ApolloLink((operation, forward) => {
    operation.setContext(({ headers }) => {
      const serverRequest = operation.getContext().serverRequest;

      console.log(
        "headerForwardLink#user-agent",
        serverRequest.headers["user-agent"]
      );
      console.log("headerForwardLink#cookie", serverRequest.headers.cookie);
      return {
        headers: {
          ...headers,
          "user-agent": serverRequest.headers["user-agent"],
          cookie: serverRequest.headers.cookie
        }
      };
    });

    return forward(operation).map((result) => {
      const response = operation.getContext().response;

      let setCookieHeader = response.headers.get("set-cookie");
      if (setCookieHeader) {
        const serverResponse = operation.getContext().serverResponse;
        serverResponse.setHeader("set-cookie", setCookieHeader);
      }
      return result;
    });
  });

  return link;
};

with 3.6.10, it works fine.
console logs show cookie and user-agent from serverRequest, and it actually forward cookie and user-agent to ApolloClient.

Actual outcome:

Since 3.7.0, it no longer works. console logs shows undefined, and ApolloClient does not receive cookie from server request.

How to reproduce the issue:

https://github.com/yshrsmz/polloclient-context-test

run yarn install and yarn test to see it fails, and change apollo-client version to 3.6.10 and re-run yarn test.
(sorry for the typo btw)

Versions

  System:
    OS: macOS 12.6
  Binaries:
    Node: 16.17.0 - ~/.anyenv/envs/nodenv/versions/16.17.0/bin/node
    Yarn: 1.22.19 - ~/.anyenv/envs/nodenv/versions/16.17.0/bin/yarn
    npm: 8.15.0 - ~/.anyenv/envs/nodenv/versions/16.17.0/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Safari: 16.0
  npmPackages:
    @apollo/client: 3.7.0 => 3.7.0
@yshrsmz
Copy link
Author

yshrsmz commented Oct 6, 2022

interesting note: serverRequest.headers is empty, but it seems like serverRequest.rawHeaders has raw values.

@MrDoomBringer MrDoomBringer self-assigned this Oct 11, 2022
@MrDoomBringer MrDoomBringer added the 🔍 investigate Investigate further label Oct 11, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔍 investigate Investigate further
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants