First, install Absinthe.Plug and a JSON codec of your choice, eg, Poison:
To use, simply
plug Absinthe.Plug in your pipeline.
If you are going to support content types other than simply
you should plug Absinthe.Plug after Plug.Parsers.
For more information on how the content types work, see General Usage.
If your entire API is going to be based on GraphQL, we recommend simply plugging Absinthe.Plug in at the bottom of your endpoint, and removing your router altogether.
If you want only Absinthe.Plug to serve a particular route, configure your router like:
Now Absinthe.Plug will only serve GraphQL from the
Absinthe.Plug will pass any values found inside
Absinthe.run as the context. This is how you should handle logic that
uses headers – most notably, Authentication.
For more information, see the Context guide.
See the Using GraphiQL
section of the Introspection guide
to learn how to use the built-in
This plug supports requests in a number of ways:
Via a GET
With a query string:
Due to varying limits on the maximum size of URLs,
we recommend using one of the POST options below instead, putting the
query into the body of the request.
With a POST body:
(We could also pull either
variables out to the query string, just
as in the GET example.)
With a query string:
And a POST body:
How clients interact with the plug over HTTP is designed to closely match that of the official express-graphql middleware.
In the example above, we went over the various ways to make a request, but here are the details:
Once installed at a path, the plug will accept requests with the following parameters:
query- A string GraphQL document to be executed.
variables- The runtime values to use for any GraphQL query variables as a JSON object.
operationName- If the provided
querycontains multiple named operations, this specifies which operation should be executed. If not provided, a 400 error will be returned if the
querycontains multiple named operations.
The plug will first look for each parameter in the query string, eg:
If not found in the query string, it will look in the POST request body, using
a strategy based on the
For content types
Plug.Parsers (or equivalent) to parse the request body before
application/graphql, the POST body will be parsed as GraphQL query string,
which provides the
query parameter. If
needed, they should be passed as part of the
As a plug,
Absinthe.Plug requires very little configuration. If you want to support
application/json you’ll need to plug
Absinthe.Plug requires a
It also takes several options. See the documentation for the full listing.