absinthe_relay v1.3.0 Absinthe.Relay.Node.ParseIDs

Parse node (global) ID arguments before they are passed to a resolver, checking the arguments against acceptable types.

For each argument:

  • If a single node type is provided, the node ID in the argument map will be replaced by the ID specific to your application.
  • If multiple node types are provided (as a list), the node ID in the argument map will be replaced by a map with the node ID specific to your application as :id and the parsed node type as :type.

Examples

Parse a node (global) ID argument :item_id as an :item type. This replaces the node ID in the argument map (key :item_id) with your application-specific ID. For example, "123".

field :item, :item do
  arg :item_id, non_null(:id)

  middleware Absinthe.Relay.Node.ParseIDs, item_id: :item
  resolve &item_resolver/3
end

Parse a node (global) ID argument :interface_id into one of multiple node types. This replaces the node ID in the argument map (key :interface_id) with map of the parsed node type and your application-specific ID. For example, %{type: :thing, id: "123"}.

field :foo, :foo do
  arg :interface_id, non_null(:id)

  middleware Absinthe.Relay.Node.ParseIDs, interface_id: [:item, :thing]
  resolve &foo_resolver/3
end

As with any piece of middleware, this can configured schema-wide using the middleware/3 function in your schema. In this example all top level query fields are made to support node IDs with the associated criteria in @node_id_rules:

defmodule MyApp.Schema do

  # Schema ...

  @node_id_rules %{
    item_id: :item,
    interface_id: [:item, :thing],
  }
  def middleware(middleware, _, %Absinthe.Type.Object{identifier: :query}) do
    [{Absinthe.Relay.Node.ParseIDs, @node_id_rules} | middleware]
  end
  def middleware(middleware, _, _) do
    middleware
  end

end

See the documentation for Absinthe.Middleware for more details.

Summary

Types

The rules used to parse node ID arguments

Types

rules :: %{optional(atom) => atom | [atom]}

The rules used to parse node ID arguments.

Examples

Declare :item_id as only valid with the :item node type:

%{
  item_id: :item
}

Declare :item_id be valid as either :foo or :bar types:

%{
  item_id: [:foo, :bar]
}

Note that using these two different forms will result in different argument values being passed for :item_id (the former, as a binary, the latter as a map). See the module documentation for more details.