Meteor/apollo: how to set value in context using value in header?

I have a client which sends an id encoded into a jwt token in a request header. On my server, I’d like to decode this token and add the id to the context so that my resolvers can then use that id.

In createApolloServer, you can specify a function which takes in a context and spits out a new context, but I’m not sure how to access request headers from there. Is there a way, or am I barking up the wrong tree? How can I accomplish what I’m trying to do?


context as a variable is used a lot, but maybe something like this could work?

const addOptions = (context) => { 
  // 'context' pulls in all info data from the header of which device hit the server
  // console.log("...", context.headers);
  const forwardedFor = context.headers['x-forwarded-for'];
  const userAgent = context.headers['user-agent'];
  const forwardedPort = context.headers['x-forwarded-port'];
  return {
    context(context) { 
      const contextNew = context;
      contextNew.forwardedFor = forwardedFor;
      contextNew.userAgent = userAgent;
      contextNew.forwardedPort = forwardedPort;
      return contextNew;
    schema: executableSchema
createApolloServer(context => addOptions(context));

Checking out the process here in the server code may help too.