Apollo-server: Accessing Request in Apollo Server 2

Created on 14 May 2018  路  10Comments  路  Source: apollographql/apollo-server


Is there any way of accessing the request object from the underlying express app in Apollo Server 2 beta? Much like this one #420.

documentation

Most helpful comment

The context configuration parameter can be either an object, a function that returns the object, or a function that returns a promise to return the object. This function would get the HTTP request as a parameter, and could be defined like so -

const myServer =  new ApolloServer({
    schema: executableSchema,
    context: async ({req}) => { 
        const data = await someStuff(); 
        return {
            token: data.stuff, 
            user: req.session.user 
        } 
    }
  });

All 10 comments

The context configuration parameter can be either an object, a function that returns the object, or a function that returns a promise to return the object. This function would get the HTTP request as a parameter, and could be defined like so -

const myServer =  new ApolloServer({
    schema: executableSchema,
    context: async ({req}) => { 
        const data = await someStuff(); 
        return {
            token: data.stuff, 
            user: req.session.user 
        } 
    }
  });

The context as a function is a perfect way to do this. Thanks @eugene1g for the code snippet!

It would be awesome if you could open a PR to the version-2 branch and add the context to the api reference

@eugene1g Can context be a function that returns a Promise like you show above?

I'm running Apollo Server 2.0.0-beta.3 and my resolvers are receiving an unresolved Promise in context param.

My context function looks just like yours.

@darkadept Nope! I just tested 2.0.0-beta.3, and it no longer supports async context. Have to stay to beta.2 for now

/cc @evans

@darkadept I looked through the code and looks like there is regression in how context gets passed through the layers. I can't tell if it's been corrected in master as there is quite a bit of refactoring going on.

However, there seems to be a hacky workaround for beta.3: you can wrap your actual async function in another sync function like so -

const generateContext = async ({req}) => { 
        const data = await someStuff(); 
        return {
            token: data.stuff, 
            user: req.session.user 
        } 
}
const myServer =  new ApolloServer({
    schema: executableSchema,
    context: params => () => generateContext(params);
  });

This "works" in a sense is that it gives you async context generators in beta.3 but this behavior is not standard and well may break in the next beta!

@eugene1g Oh wow. That works! Thanks.

What's the roadmap for this context function though? Is it supposed to support async context functions in the future? I just realized this should probably be it's own issue. Should I open an issue for this?

I expect the context option will support async functions in the final release and in the future. The current situation is most likely a temporary regression.

async is core to GraphQL, and if Apollo decided not to support async context generation I would be shocked enough to send a PR :D

@eugene1g Definitely a regression! I'll add a test and publish another beta

Confirming this has been fixed 2.0.0-beta.4 and context handles async functions. Thanks!

@eugene1g can you help me on this issue? your approach seems a better alternative but is not working for me.

Was this page helpful?
0 / 5 - 0 ratings