ash_graphql/test/alias_test.exs
Riccardo Binetti 513c1ac68f
improvement!: port AshGraphql to Ash 3.0 (#123)
Step 1: update Ash

Step 2: mass rename Api to Domain

Step 3: Ash.Query.expr -> Ash.Expr.expr

Also change ref interpolation

Step 4: remove all warnings

Step 5: remove registries from tests

Step 6: fix filter

Step 7: private? -> !public?

Step 8: Ash.Calculation -> Ash.Resource.Calculation

Step 9: use depend_on_resources/1 -> resources/1

Step 10: add Domain to all resources

Step 11: use Ash module for all actions

Step 12: add public? true all around

Step 13: remove verbose? from options passed during Domain calls

Step 14: add simple_sat

Step 15: Ash.ErrorKind is no more, so remove code from errors

Step 16: sprinkle default_accept :* around tests

Step 17: replace Ash.Changeset.new/2 with Ash.Changeset.for_*

Step 18: calculation fixups

- Context is now a struct and arguments go under the arguments key
- Function based calculations receive a list of records
- Add a select to query-based loads
- select -> load

Step 19: pass the correct name to pass the policy in tests

Step 20: Ash.Query.new/2 is no more

Step 21: add AshGraphql.Resource.embedded? utility function

Use that instead of Ash.Type.embedded_type?(resource_or_type) since resources
are not types anymore

Step 22: handle struct + instance_of: Resource in unions

Resources are not type anymore so they need to be passed this way in unions

Step 23: ensure we only check GraphQL actions for pagination

All reads are now paginated by default, so this triggered a compilation error

Step 24: swap arguments for sort on calculations

Step 25: remove unused debug? option
2024-04-01 14:03:06 -04:00

253 lines
5.2 KiB
Elixir

defmodule AliasTest do
use ExUnit.Case, async: false
require Ash.Query
setup do
on_exit(fn ->
AshGraphql.TestHelpers.stop_ets()
end)
end
test "attribute alias works correctly for nullable attribute with built-in type" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create, text: "foo", published: true, score: 9.8)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
content: text
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{data: %{"getPost" => %{"content" => "foo"}}} = result
end
test "attribute alias works correctly for non-nullable attribute with built-in type" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create, text: "foo", published: true, score: 9.8)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
text: requiredString
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{data: %{"getPost" => %{"text" => "test"}}} = result
end
test "attribute alias works correctly for nullable attribute with custom type" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
foo: %{bar: "baz"},
published: true,
score: 9.8
)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
bar: foo {
bar
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{data: %{"getPost" => %{"bar" => %{"bar" => "baz"}}}} = result
end
test "attribute alias works correctly for nullable attribute with embedded type" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
embed: %{nested_embed: %{name: "test embed"}},
published: true,
score: 9.8
)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
embedded: embed {
nested_embed {
name
}
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{
data: %{
"getPost" => %{"embedded" => %{"nested_embed" => %{"name" => "test embed"}}}
}
} = result
end
test "calculation alias works correctly" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
text1: "hello",
text2: "world",
published: true,
score: 9.8
)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
content: full_text
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{
data: %{
"getPost" => %{"content" => "helloworld"}
}
} = result
end
test "aggregate alias works correctly" do
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
published: true,
score: 9.8
)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
comments: commentCount
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
assert %{
data: %{
"getPost" => %{"comments" => 0}
}
} = result
end
test "relationship alias works correctly" do
author =
AshGraphql.Test.User
|> Ash.Changeset.for_create(:create, name: "My Name")
|> Ash.create!()
post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(:create,
text: "foo",
published: true,
score: 9.8,
author_id: author.id
)
|> Ash.create!()
resp =
"""
query Post($id: ID!) {
getPost(id: $id) {
writer: author {
name
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"id" => post.id
}
)
assert {:ok, result} = resp
refute Map.has_key?(result, :errors)
name = author.name
assert %{
data: %{
"getPost" => %{"writer" => %{"name" => ^name}}
}
} = result
end
end