Skip to content

Commit

Permalink
Release v0.5.3 (#99)
Browse files Browse the repository at this point in the history
* Add additional tests for resolving data alongside errors

* Release v0.5.3
  • Loading branch information
kzlsakal committed Jun 17, 2024
1 parent ac9d6cd commit b5e3564
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.3

- Bug Fix: [Handle resolving `nil` for entity references](https://github.com/DivvyPayHQ/absinthe_federation/pull/98)

## 0.5.2

- Feature: [Support `@interfaceObject` directive](https://github.com/DivvyPayHQ/absinthe_federation/pull/96)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Absinthe.Federation.MixProject do
use Mix.Project

@source_url "https://github.com/DivvyPayHQ/absinthe_federation"
@version "0.5.2"
@version "0.5.3"

def project do
[
Expand Down
78 changes: 78 additions & 0 deletions test/absinthe/federation/schema/entities_field_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ defmodule Absinthe.Federation.Schema.EntitiesFieldTest do
object :product do
key_fields("upc")
field :upc, non_null(:string)

field :name, :string,
resolve: fn
%{upc: "789-erroneous-name"}, _, _ -> {:error, "arbitrary name error"}
%{upc: upc}, _, _ -> {:ok, "product #{upc}"}
end

field :foo, non_null(:string), resolve: fn _, _, _ -> {:ok, "bar"} end

field :_resolve_reference, :product do
Expand All @@ -63,6 +70,7 @@ defmodule Absinthe.Federation.Schema.EntitiesFieldTest do
case upc do
"123" -> {:ok, args}
"456" -> {:ok, args}
"789-erroneous-name" -> {:ok, args}
"nil" <> _ -> {:ok, nil}
_ -> {:error, "Couldn't find product with upc #{upc}"}
end
Expand Down Expand Up @@ -146,6 +154,43 @@ defmodule Absinthe.Federation.Schema.EntitiesFieldTest do
} = resp
end

test "handles errors alongside data" do
query = """
query {
_entities(representations: [
{
__typename: "Product",
upc: "789-erroneous-name"
},
{
__typename: "Product",
upc: "456"
}
]) {
...on Product {
upc
foo
name
}
}
}
"""

{:ok, resp} = Absinthe.run(query, ResolverSchema, variables: %{})

assert %{
data: %{
"_entities" => [
%{"foo" => "bar", "name" => nil, "upc" => "789-erroneous-name"},
%{"foo" => "bar", "name" => "product 456", "upc" => "456"}
]
},
errors: [
%{message: "arbitrary name error", path: ["_entities", 0, "name"], locations: [%{column: 9, line: 15}]}
]
} == resp
end

test "Handles missing data" do
query = """
query {
Expand Down Expand Up @@ -175,6 +220,39 @@ defmodule Absinthe.Federation.Schema.EntitiesFieldTest do
} = resp
end

test "Handles missing data alongside existing data" do
query = """
query {
_entities(representations: [
{
__typename: "Product",
upc: "nil1"
},
{
__typename: "Product",
upc: "nil22"
},
{
__typename: "Product",
upc: "456"
}
]) {
__typename
...on Product {
upc
foo
}
}
}
"""

{:ok, resp} = Absinthe.run(query, ResolverSchema, variables: %{})

assert %{
data: %{"_entities" => [nil, nil, %{"__typename" => "Product", "foo" => "bar", "upc" => "456"}]}
} = resp
end

test "falls back to default _resolve_reference implementation" do
query = """
query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ defmodule Absinthe.Federation.Schema.PersistentTermSchemaEntityFieldTest do
key_fields("name")

field :name, :string do
resolve(fn %{name: name}, _, _ ->
{:ok, name}
end)
resolve(fn %{name: name}, _, _ -> {:ok, name} end)
end

resolve_type(&resolve_type/2)

field :_resolve_reference, :named_entity do
resolve(fn
_, %{name: "John"}, _ -> {:ok, %{__typename: "Person", name: "John", age: 20}}
_, %{name: "John"}, _ -> {:ok, %{__typename: "Person", name: "John"}}
_, %{name: "error on age" <> _ = name}, _ -> {:ok, %{__typename: "Person", name: name}}
_, %{name: "Acme"}, _ -> {:ok, %{__typename: "Business", name: "Acme", employee_count: 10}}
_, %{name: "nil" <> _}, _ -> {:ok, nil}
end)
Expand All @@ -40,7 +39,13 @@ defmodule Absinthe.Federation.Schema.PersistentTermSchemaEntityFieldTest do

interface :named_entity
import_fields(:named_entity, [:name, :_resolve_reference])
field :age, :integer

field :age, :integer do
resolve(fn
%{name: "error on age" <> _ = error}, _, _ -> {:error, error}
%{name: _name}, _, _ -> {:ok, 20}
end)
end
end

object :business do
Expand Down Expand Up @@ -119,4 +124,45 @@ defmodule Absinthe.Federation.Schema.PersistentTermSchemaEntityFieldTest do
}
} = resp
end

test "Handles errors alongside data" do
query = """
query {
_entities(representations: [
{ __typename: "NamedEntity", name: "John" },
{ __typename: "NamedEntity", name: "error on age 1" },
{ __typename: "NamedEntity", name: "error on age 2" }
]) {
...on NamedEntity {
__typename
name
... on Person {
age
}
... on Business {
employeeCount
}
}
}
}
"""

{:ok, resp} = Absinthe.run(query, EntitySchemaWithPersistentTermProvider, variables: %{})

assert %{
data: %{
"_entities" => [
%{"__typename" => "Person", "age" => 20, "name" => "John"},
%{"__typename" => "Person", "age" => nil, "name" => "error on age 1"},
%{"__typename" => "Person", "age" => nil, "name" => "error on age 2"}
]
},
errors: errors
} = resp

assert errors == [
%{message: "error on age 2", path: ["_entities", 2, "age"], locations: [%{line: 11, column: 11}]},
%{message: "error on age 1", path: ["_entities", 1, "age"], locations: [%{line: 11, column: 11}]}
]
end
end

0 comments on commit b5e3564

Please sign in to comment.