# Accessing context values in Datasource with AS4

**URL:** <https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461>\
**Category:** Server\
**Tags:** server\
**Created:** [January 11, 2023, 5:31pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461 "2023-01-11T17:31:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SurajThummala](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@SurajThummala](https://community.apollographql.com/u/SurajThummala)\
**Post date:** [January 11, 2023, 5:31pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/1 "2023-01-11T17:31:34Z")

</div>

In As3, I was able to access the context using this.context in datasource files, but in AS4, since, datasource function is part of context. How should i able to access the context values in DS file? are there any examples ?

---

<div class="post-metadata">

**Author:** ![glasser](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/glasser/32/48_2.png) [@glasser](https://community.apollographql.com/u/glasser)\
**Post date:** [January 11, 2023, 7:35pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/2 "2023-01-11T19:35:51Z")

</div>

Yes, there are specific examples of how to do this in [Migrating to Apollo Server 4 - Apollo GraphQL Docs](https://www.apollographql.com/docs/apollo-server/migration/#datasources)

The easy way is to just only pass the particular parts of `context` that are actually needed by your data source to the data source’s constructor directly, which avoids creating a circular reference entirely. The second example shows how making your context into a class can make it easier to set up the circular reference (because you can reference `this` inside a constructor even though the object isn’t “done” yet).

---

<div class="post-metadata">

**Author:** ![watson](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/watson/32/2053_2.png) [@watson](https://community.apollographql.com/u/watson)\
**Post date:** [January 12, 2023, 5:27pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/3 "2023-01-12T17:27:02Z")

</div>

If you are still having any issues, we just started up an [Apollo Discord](https://discord.com/invite/graphos) for more real-time chat and livestreaming. If you come over there, feel free to ping me and we can jump on a stream to talk more about your migration.

---

<div class="post-metadata">

**Author:** ![SurajThummala](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@SurajThummala](https://community.apollographql.com/u/SurajThummala)\
**Post date:** [January 12, 2023, 7:27pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/4 "2023-01-12T19:27:11Z")

</div>

We would like to make the context available through out the server. So, in our case - second example suits us well. Just want to check if there are any performance concerns if we go with option as making a context into class.  
Thanks for your time, support, and contribution.

---

<div class="post-metadata">

**Author:** ![Kheang\_Hok\_Chin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/kheang_hok_chin/32/391_2.png) [@Kheang\_Hok\_Chin](https://community.apollographql.com/u/Kheang_Hok_Chin)\
**Post date:** [January 24, 2023, 10:03pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/5 "2023-01-24T22:03:55Z")

</div>

I was having the same issue as mentioned in this post and have been able to get access to my datasources via context by going with the Class option.

I just noticed an issue.  
I have a middleware that handles authentication and updates the context.

My datasources no longer have access to my authenticated user via the context.

Any ideas?

---

<div class="post-metadata">

**Author:** ![glasser](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/glasser/32/48_2.png) [@glasser](https://community.apollographql.com/u/glasser)\
**Post date:** [January 24, 2023, 10:40pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/6 "2023-01-24T22:40:32Z")

</div>

The docs show how to pass your context to your data sources if you’d like (the easiest way involves making your context into a class so it can refer to itself with `this` during its constructor even though it’s not “completed” yet).

---

<div class="post-metadata">

**Author:** ![Kheang\_Hok\_Chin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/kheang_hok_chin/32/391_2.png) [@Kheang\_Hok\_Chin](https://community.apollographql.com/u/Kheang_Hok_Chin)\
**Post date:** [January 24, 2023, 10:57pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/7 "2023-01-24T22:57:28Z")

</div>

Yeah that’s what I am doing though.

But for some reason, my datasource doesn’t my `context.user` variable which is only set in my middleware which runs after the context is created.

I assume its either the way I built my class context value or its because the middleware executes after?

---

<div class="post-metadata">

**Author:** ![glasser](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/glasser/32/48_2.png) [@glasser](https://community.apollographql.com/u/glasser)\
**Post date:** [January 24, 2023, 11:15pm UTC](https://community.apollographql.com/t/accessing-context-values-in-datasource-with-as4/5461/8 "2023-01-24T23:15:00Z")

</div>

Hard to figure out what’s going on here without seeing the code — perhaps you’re copying context.user into your data source class earlier than appropriate?
