graphql-elixir / plug_graphql

Plug (Phoenix) integration for GraphQL Elixir
Other
126 stars 7 forks source link

Use conn as context for query execution #8

Closed danielberkompas closed 8 years ago

danielberkompas commented 8 years ago

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?

aweiker commented 8 years ago

Take a look at this PR for my attempt at solving this. https://github.com/joshprice/plug_graphql/pull/9

joshprice commented 8 years ago

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.

joshprice commented 8 years ago

Closed by #9