absinthe_plug v1.4.0-rc.1 Absinthe.Plug.GraphiQL

Provides a GraphiQL interface.

Examples

Serve the GraphiQL “advanced” interface at /graphiql, but only in development:

if Mix.env == :dev do
  forward "/graphiql",
    to: Absinthe.Plug.GraphiQL,
    init_opts: [schema: MyApp.Schema]
end

Use the “simple” interface (original GraphiQL) instead:

if Mix.env == :dev do
  forward "/graphiql",
    to: Absinthe.Plug.GraphiQL,
    init_opts: [
      schema: MyApp.Schema,
      interface: :simple
    ]

Interface Selection

The GraphiQL interface can be switched using the :interface option.

  • :advanced (default) will serve the GraphiQL Workspace interface from Oleg Ilyenko.
  • :simple will serve the original GraphiQL interface from Facebook.

See Absinthe.Plug for the other options.

Default Headers

You can optionally provide default headers if the advanced interface (GraphiQL Workspace) is selected. Note that you may have to clean up your existing workspace by clicking the trashcan icon in order to see the newly set default headers.

forward "/graphiql",
  to: Absinthe.Plug.GraphiQL,
  init_opts: [
    schema: MyApp.Schema,
    default_headers: {__MODULE__, :graphiql_headers}
  ]

def graphiql_headers do
  %{
    "X-CSRF-Token" => Plug.CSRFProtection.get_csrf_token(),
    "X-Foo" => "Bar"
  }
end

Default URL

You can also optionally set the default URL to be used for sending the queries to. This only applies to the advanced interface (GraphiQL Workspace).

forward "/graphiql",
  to: Absinthe.Plug.GraphiQL,
  init_opts: [
    schema: MyApp.Schema,
    default_url: "https://api.mydomain.com/graphql"
  ]

Summary

Types

opts()
opts() :: [schema: atom, adapter: atom, path: binary, context: map, json_codec: atom | {atom, Keyword.t}, interface: :advanced | :simple, default_headers: {module, atom}, default_url: binary, assets: Keyword.t, socket: module, socket_url: binary]