apollographql / apollographql/apollo-server

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

Open
#7,863 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
14k
Forks
2k
Avg merge
2d 14h
Merged PRs (30d)
2

Description

### 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.

Contributor guide

Open the contributing guide

Research direction

Start with the linked CodeSandbox reproduction and follow the `hello` query through the resolver, authentication directive, and data source. Compare the context logged in the resolver with `console.log("context in api", this.context)` and verify whether the same request context reference is preserved and its `user` property is populated.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.