menu

JS Everywhere

A community for the JavaScript Everywhere book by Adam Scott, published by O'Reilly.

Channels
Team

How does the resolver know which context variable is which?

February 7, 2021 at 8:56am

How does the resolver know which context variable is which?

February 7, 2021 at 8:56am
Hi all. I'm new to JavaScript, so bear with me if this question is dumb.
In src/index.js we pass "models" and "user" to ApollowServer context, i.e.: context: ({ req }) => { const token = req.headers.authorization; const user = getUser(token); return { models, user }; //----ref(A) }.
Then all resolvers could use this context to resolve queries, e.g., newNote: async (parent, args, { models, user }) => { //----ref(B) if (!user) ... }
My question is, how does the resolver, here "newNote", determine the "user" at ref(B) is the "user" passed at ref(A)? I tried changing the ref(A) "user" to "user2", and then changing the order of the two variables at ref(A), neither would cause any error.
Could anyone give me a hint? Thanks!

February 7, 2021 at 3:37pm
Hi (hao-wang), welcome and thanks for reading! The Apollo Server documentation on context might be helpful for your question https://www.apollographql.com/docs/apollo-server/data/resolvers/#the-context-argument
  • reply
  • like

February 8, 2021 at 2:05am
Hi (hao-wang), welcome and thanks for reading! The Apollo Server documentation on context might be helpful for your question https://www.apollographql.com/docs/apollo-server/data/resolvers/#the-context-argument
I see. Thanks, Adam!
  • reply
  • like