Skip to content

Commit

Permalink
fix path_string crash when path includes list
Browse files Browse the repository at this point in the history
and add documentation for path_string/1

this fix is absinthe-graphql#1337
absinthe-graphql#1337
  • Loading branch information
mdg committed Sep 1, 2024
1 parent 3d0823b commit 28c79fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 22 additions & 1 deletion lib/absinthe/resolution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,29 @@ defmodule Absinthe.Resolution do

def call(res, _), do: res

@doc """
Get a list of strings representing the path to this field resolution.
The index of list items is dropped and the schema type is included.
## Examples
Given some query:
```graphql
query {users { email }}
```
If you called this function inside a resolver on the users email field it
returns a value like:
```elixir
resolve fn _, _, resolution ->
Absinthe.Resolution.path(resolution) #=> ["email", "users", "query"]
end
```
"""
def path_string(%__MODULE__{path: path}) do
Enum.map(path, fn
path
|> Enum.reject(fn x -> is_integer(x) end)
|> Enum.map(fn
%{name: name, alias: alias} ->
alias || name

Expand Down
8 changes: 6 additions & 2 deletions test/absinthe/resolution/middleware_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ defmodule Absinthe.MiddlewareTest do
field :path, :path do
resolve fn _, _ -> {:ok, %{}} end
end

field :list_of_paths, list_of(:path) do
resolve fn _, _ -> {:ok, [%{}]} end
end
end

object :path do
Expand Down Expand Up @@ -188,13 +192,13 @@ defmodule Absinthe.MiddlewareTest do

test "it gets the path of the current field" do
doc = """
{foo: path { bar: path { result }}}
{foo: listOfPaths { bar: path { result }}}
"""

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", "foo", "RootQueryType"]}}]} ==
data
end
end

0 comments on commit 28c79fb

Please sign in to comment.