Users who have contributed to this file

The Context and Authentication

Absinthe context exists to provide shared values to a given document execution.
A common use would be to pass in the current user of a given request. The context
is set at the call to Absinthe.run, and cannot be modified over the course of
a given execution.

Basic Usage

As a basic example let's think about a profile page, where we want the current user
to be able to access basic information about themselves, but not other users.

Context and Plugs

When using Absinthe.Plug you don't have direct access to the Absinthe.run call.
Instead, we can use Absinthe.Plug.put_options/2 to set context values which
Absinthe.Plug will use to pass it along to Absinthe.run.

Setting up your GraphQL context is as simple as writing a plug that inserts the
appropriate values into the connection.

Let's use this mechanism to set our current_user from the previous example via
an authentication header. We will use the same Schema as before.

First, our plug. We'll be checking the for the authorization header, and calling
out to some unspecified authentication mechanism.

This plug will use the authorization header to lookup the current user. If one
is found, it correctly sets the absinthe context. If you're using Guardian or
some other library that provides utilities for authenticating users you can use
those here too, and just add their output to the context.

If there is no current user it's better to simply not have the :current_user
key inside the map, instead of doing %{current_user: nil}. This way you an
just pattern match for %{current_user: user} in your code and not need to
worry about the nil case.

Using this plug is very simple. If we're just in a normal plug context we can
just make sure it's plugged prior to Absinthe.Plug