View Source AbsinthePlugCache.Plug.GraphiQL (absinthe_plug_cache v1.5.8-1)

Provides a GraphiQL interface.

examples

Examples

The examples here are shown in

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

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

Use the "simple" interface (original GraphiQL) instead:

forward "/graphiql",
  to: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    interface: :simple
  ]

Finally there is also support for GraphiQL Playground https://github.com/graphcool/graphql-playground

forward "/graphiql",
  to: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    interface: :playground
  ]

interface-selection

Interface Selection

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

See AbsinthePlugCache.Plug for the other options.

default-headers

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: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    default_headers: {__MODULE__, :graphiql_headers}
  ]

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

You can also provide a function that takes a conn argument if you need to access connection data (e.g. if you need to set an Authorization header based on the currently logged-in user).

def graphiql_headers(conn) do
  %{
    "Authorization" => "Bearer " <> conn.assigns[:token]
  }
end

default-url

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) and the GraphQL Playground.

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

This option also accepts a function:

forward "/graphiql",
  to: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    default_url: {__MODULE__, :graphiql_default_url}
  ]

def graphiql_default_url(conn) do
  conn.assigns[:graphql_url]
end

socket-url

Socket URL

You can also optionally set the default websocket URL to be used for subscriptions. This only applies to the advanced interface (GraphiQL Workspace) and the GraphQL Playground.

forward "/graphiql",
  to: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    socket_url: "wss://api.mydomain.com/socket"
  ]

This option also accepts a function:

forward "/graphiql",
  to: AbsinthePlugCache.Plug.GraphiQL,
  init_opts: [
    schema: MyAppWeb.Schema,
    socket_url: {__MODULE__, :graphiql_socket_url}
  ]

def graphiql_socket_url(conn) do
  conn.assigns[:graphql_socket_url]
end

Link to this section Summary

Link to this section Types

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