-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Relay ID translation in mutation and queries
Adds a new option for queries and mutations that defines which arguments or attributes will use a global Relay ID and their type. This allows automatically decoding them before hitting their action.
- Loading branch information
Showing
9 changed files
with
502 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule AshGraphql.Graphql.IdTranslator do | ||
def translate_relay_ids(%{state: :unresolved} = resolution, relay_id_translations) do | ||
arguments = | ||
Enum.reduce(relay_id_translations, resolution.arguments, &process/2) | ||
|
||
%{resolution | arguments: arguments} | ||
end | ||
|
||
def translate_relay_ids(resolution, _relay_id_translations) do | ||
resolution | ||
end | ||
|
||
defp process({field, nested_translations}, args) when is_list(nested_translations) do | ||
case Map.get(args, field) do | ||
subtree when is_map(subtree) -> | ||
new_subtree = Enum.reduce(nested_translations, subtree, &process/2) | ||
Map.put(args, field, new_subtree) | ||
|
||
_ -> | ||
args | ||
end | ||
end | ||
|
||
defp process({field, type}, args) when is_atom(type) do | ||
case Map.get(args, field) do | ||
id when is_binary(id) -> | ||
{:ok, %{type: ^type, id: decoded_id}} = AshGraphql.Resource.decode_relay_id(id) | ||
Map.put(args, field, decoded_id) | ||
|
||
[id | _] = ids when is_binary(id) -> | ||
decoded_ids = | ||
Enum.map(ids, fn id -> | ||
{:ok, %{type: ^type, id: decoded_id}} = AshGraphql.Resource.decode_relay_id(id) | ||
decoded_id | ||
end) | ||
|
||
Map.put(args, field, decoded_ids) | ||
|
||
_ -> | ||
args | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.