Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Request in Apollo Server 2 #1066

Closed
tadeolinco opened this issue May 14, 2018 · 10 comments
Closed

Accessing Request in Apollo Server 2 #1066

tadeolinco opened this issue May 14, 2018 · 10 comments
Labels
📝 documentation Focuses on changes to the documentation (docs)
Milestone

Comments

@tadeolinco
Copy link

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

@ghost ghost added the 📝 documentation Focuses on changes to the documentation (docs) label May 14, 2018
@eugene1g
Copy link

eugene1g commented May 14, 2018

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 
        } 
    }
  });

@evans
Copy link
Contributor

evans commented May 22, 2018

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

@evans evans closed this as completed May 22, 2018
@evans evans added this to the Release 2.0 milestone May 22, 2018
@darkadept
Copy link

@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.

@eugene1g
Copy link

@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

@eugene1g
Copy link

eugene1g commented May 31, 2018

@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!

@darkadept
Copy link

@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?

@eugene1g
Copy link

eugene1g commented Jun 1, 2018

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

@evans
Copy link
Contributor

evans commented Jun 1, 2018

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

@eugene1g
Copy link

eugene1g commented Jun 4, 2018

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

@Lahori-Jawan
Copy link

Lahori-Jawan commented Oct 10, 2018

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
📝 documentation Focuses on changes to the documentation (docs)
Projects
None yet
Development

No branches or pull requests

5 participants