0
votes

I'm currently following this guide (https://www.youtube.com/watch?time_continue=1614&v=_kYUzNVyj-0) to implement authentication in my react, graphql and apollo application. In the guide they have this function:

async function context(headers, constants) {
  console.log('calling context') // This never prints, so it's not being called.
  const user = await getUser(headers['authorization'])
  return {
    headers,
    user
  }
}

Below that function I have my resolvers and mutation for the GraphQL. One of the queries is supoosed to get the current user and that looks like this:

export default {
  Date: GraphQLDate,
  Query: {
    currentUser: (root, args, { user }) => {
      console.log(user) // undefined 
      console.log('currentUser')
      return hello
    },
    ...

In the video they later access the values via object destructuring, or just importing the whole object in their arguments:

When I'm trying to access anything from the return object from the context function I get undefined. It's due to the fact that the function is never being called, and I don't know why. It's probably something dumb I'm missing here.

I made a Gist of the code that I want to get working if that might help my shining knight that will hopefully save my day.

https://gist.github.com/Martinnord/6d48132db9af1f9e7b41f1266444011c

I can add more context to my question if anyone like! Thanks so much for reading!

1

1 Answers

1
votes

You need to export your context function for Launchpad to pick it up. So

export async function context(headers, constants) {
  console.log('calling context') // This never prints, so it's not being called.
  const user = await getUser(headers['authorization'])
  return {
    headers,
    user
  }
}

It's not fully clear IMO that launchpad is looking for that context function. This is a tricky topic as well since the context function isn't required to make things work it doesn't notify you that it isn't there.