Absinthe.Plug.GraphiQL (absinthe_plug v1.5.9) View Source
Provides a GraphiQL interface.
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: Absinthe.Plug.GraphiQL,
init_opts: [schema: MyAppWeb.Schema]
endUse the "simple" interface (original GraphiQL) instead:
forward "/graphiql",
to: Absinthe.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: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
interface: :playground
]Interface Selection
The GraphiQL interface can be switched using the :interface option.
:advanced(default) will serve the GraphiQL Workspace interface from Oleg Ilyenko.:simplewill serve the original GraphiQL interface from Facebook.:playgroundwill serve the GraphQL Playground interface from Graphcool.
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: MyAppWeb.Schema,
default_headers: {__MODULE__, :graphiql_headers}
]
def graphiql_headers do
%{
"X-CSRF-Token" => Plug.CSRFProtection.get_csrf_token(),
"X-Foo" => "Bar"
}
endYou 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]
}
endDefault 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: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
default_url: "https://api.mydomain.com/graphql"
]This option also accepts a function:
forward "/graphiql",
to: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
default_url: {__MODULE__, :graphiql_default_url}
]
def graphiql_default_url(conn) do
conn.assigns[:graphql_url]
endSocket 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: Absinthe.Plug.GraphiQL,
init_opts: [
schema: MyAppWeb.Schema,
socket_url: "wss://api.mydomain.com/socket"
]This option also accepts a function:
forward "/graphiql",
to: Absinthe.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