apollo-server icon indicating copy to clipboard operation
apollo-server copied to clipboard

Passing context to datasource loses it's reference, meaning, we cannot mutate the context between the resolver and datasources.

Open christo8989 opened this issue 10 months ago • 2 comments

Issue Description

This worked in apollo server 3.x and I think it should still work in apollo server 4.x.

The context was mutable between the resolvers and data sources. And, if the same object is passed to Apollo Server and the Data Sources, then I would expect the reference to be the same for that request.

Link to Reproduction

https://codesandbox.io/p/devbox/green-dew-s6pz7j?file=%2Fsrc%2Findex.ts%3A31%2C6&workspaceId=9fba286e-b0a2-40d4-9a10-3ed17d6fc084

Reproduction Steps

  1. Start the application.
  2. Query "hello"
  3. Check the terminal logs.

EXPECT: console.log("context in api", this.context) to log a context with the user property filled.

ACTUAL: console.log("context in api", this.context)'s user property is null.

It should get populated from the authenticate directive. You can see that the resolver has it (which makes sense since we literally pass it though).

This worked in apollo server 3.x. And I don't see why it shouldn't work in apollo server 4.x.

christo8989 avatar Apr 02 '24 19:04 christo8989

ISSUE: https://github1s.com/apollographql/apollo-server/blob/main/packages/server/src/ApolloServer.ts#L1306-L1307

Does this have to do with httpBatching?

Can we clone here instead? https://github1s.com/apollographql/apollo-server/blob/main/packages/server/src/httpBatching.ts#L50-L51

christo8989 avatar Apr 02 '24 20:04 christo8989

That is a good point. We could consider just doing the clone in the http batching codepath rather than always. This might qualify as a backwards-incompatible change, though.

glasser avatar Apr 19 '24 01:04 glasser