Skip to content

Commit

Permalink
keep stringified integer in the output
Browse files Browse the repository at this point in the history
fix the type on Resolution.path/1

incorporating PR feedback from @benwilson512

issue absinthe-graphql#1337
  • Loading branch information
mdg committed Sep 3, 2024
1 parent ee6b536 commit ecefdfe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/absinthe/resolution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ defmodule Absinthe.Resolution do
In this case `5` is the 0 based index in the list of users the field is currently
at.
"""
@spec path(t()) :: [String.t()]
@spec path(t()) :: [String.t() | integer()]
def path(%{path: path}) do
path
|> Enum.reverse()
Expand Down Expand Up @@ -251,20 +251,22 @@ defmodule Absinthe.Resolution do
```elixir
resolve fn _, _, resolution ->
Absinthe.Resolution.path(resolution) #=> ["email", "users", "RootQueryType"]
Absinthe.Resolution.path(resolution) #=> ["email", "0", "users", "RootQueryType"]
end
```
"""
@spec path_string(t()) :: [String.t()]
def path_string(%__MODULE__{path: path}) do
path
|> Enum.reject(fn x -> is_integer(x) end)
|> Enum.map(fn
%{name: name, alias: alias} ->
alias || name

%{schema_node: schema_node} ->
schema_node.name

n when is_integer(n) ->
Integer.to_string(n)
end)
end

Expand Down
2 changes: 1 addition & 1 deletion test/absinthe/resolution/middleware_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ defmodule Absinthe.MiddlewareTest do
assert {:ok, %{data: data}} =
Absinthe.run(doc, __MODULE__.Schema, context: %{current_user: %{}})

assert %{"foo" => [%{"bar" => %{"result" => ["result", "bar", "foo", "RootQueryType"]}}]} ==
assert %{"foo" => [%{"bar" => %{"result" => ["result", "bar", "0", "foo", "RootQueryType"]}}]} ==
data
end
end

0 comments on commit ecefdfe

Please sign in to comment.