absinthe_plug v1.5.0-alpha.0 Absinthe.Plug.DocumentProvider.Compiled View Source
Provide pre-compiled documents for retrieval by request parameter key.
Important: This module shouldn't be used as a document provider itself, but as a toolkit to build one. See the examples below.
Examples
Define a new module and use Absinthe.Plug.DocumentProvider.Compiled
:
defmodule MyAppWeb.Schema.Documents do
use Absinthe.Plug.DocumentProvider.Compiled
# ... Configure here
end
You can provide documents as literals within the module, by key, using the
provide/2
macro:
provide "item", "query Item($id: ID!) { item(id: $id) { name } }"
You can also load a map of key value pairs using provide/1
.
provide %{
"item" => "query Item($id: ID!) { item(id: $id) { name } }",
"time" => "{ currentTime }"
}
This can be used to support loading documents extracted using Apollo's persistgraphql tool by parsing the file and inverting the key/value pairs.
provide File.read!("/path/to/extracted_queries.json")
|> Jason.decode!
|> Map.new(fn {k, v} -> {v, k} end)
By default, the request parameter that will be used to lookup documents is
"id"
. You can change this by passing a :key_param
option to use
, e.g.:
use Absinthe.Plug.DocumentProvider.Compiled, key_param: "lookup_key"
Configuring
You need to configure Absinthe.Plug
to use any document providers that you create.
(Only Absinthe.Plug.DocumentProviders.Default
is configured by default.)
See the documentation on Absinthe.Plug.init/1
for more details. Look for the
:document_providers
option.
Link to this section Summary
Functions
Lookup a document by id.
Provide multiple GraphQL documents by key.
Provide a GraphQL document for a given key.
Link to this section Functions
get(dp, id, format \\ :compiled)
View Source
get(module(), String.t(), :compiled | :source) :: nil | Absinthe.Blueprint.t()
get(module(), String.t(), :compiled | :source) :: nil | Absinthe.Blueprint.t()
Lookup a document by id.
Examples
Get a compiled document:
iex> get(CompiledProvider, "provided")
#Absinthe.Blueprint<>
With an explicit :compiled
flag:
iex> get(CompiledProvider, "provided", :compiled)
#Absinthe.Blueprint<>
Get the source:
iex> get(CompiledProvider, "provided", :source)
"query Item { item { name } }"
When a value isn't present:
iex> get(CompiledProvider, "not-provided")
nil
provide(documents) View Source (macro)
Provide multiple GraphQL documents by key.
Note that keys will be coerced to strings to ensure compatibility with the expected request parameter.
For more information, see the module-level documentation.
Examples
provide %{
"item" => "query Item($id: ID!) { item(id: $id) { name } }",
"time" => "{ currentTime }"
}
provide(document_key, document_source) View Source (macro)
Provide a GraphQL document for a given key.
Note that the key will be coerced to a string to ensure compatibility with the expected request parameter.
For more information, see the module-level documentation.
Examples
provide "foo", """
query ShowItem($id: ID!) {
item(id: $id) { name }
}
"""