graphql-elixir/plug_graphql

Use conn as context for query execution

danielberkompas opened this issue · 3 comments

It's very important to know who the current user is when you resolve queries. It's pretty easy to assign this information to the conn using plugs before the GraphQL plug, like so:

plug :authenticate_user
plug GraphQL.Endpoint, ...

However, since the conn is not passed into the query execution function, the resolvers have no way to know who the current user is, and therefore authentication seems impossible.

I think the conn should be added as the third argument to this function:

https://github.com/joshprice/plug_graphql/blob/21e42b00cdd9d689f5f048859e66f253d1c8d3e7/lib/graphql/plug/endpoint.ex#L44

The core GraphQL Elixir library may need to be extended to pass the context to all the resolvers.

Am I missing something here?

Take a look at this PR for my attempt at solving this. #9

GraphQL definitely can't know about conn but root_value should allow custom data to be passed in from the plug.

This is such a common use case it'd be great to have an example illustrating how to set this up.

Closed by #9