Skip to content

Commit

Permalink
test: add tests for relationships filtered by actor
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 5, 2024
1 parent 61a8b23 commit 7946216
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/read_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,66 @@ defmodule AshGraphql.ReadTest do
}} == Absinthe.run(doc, AshGraphql.Test.Schema, context: %{actor: user})
end

test "a read with a relationship filtered by an actor" do
user =
AshGraphql.Test.User
|> Ash.Changeset.for_create(:create,
name: "My Name"
)
|> Ash.create!()

user2 =
AshGraphql.Test.User
|> Ash.Changeset.for_create(:create,
name: "My Name"
)
|> Ash.create!()

user_id = user.id

post =
AshGraphql.Test.Post
|> Ash.Changeset.for_create(
:create,
%{
author_id: user.id,
text: "a",
published: true
}
)
|> Ash.create!()

doc = """
query {
getPost(id: "#{post.id}") {
authorThatIsActor{
id
}
}
}
"""

assert {:ok,
%{
data: %{
"getPost" => %{
"authorThatIsActor" => %{
"id" => ^user_id
}
}
}
}} = Absinthe.run(doc, AshGraphql.Test.Schema, context: %{actor: user})

assert {:ok,
%{
data: %{
"getPost" => %{
"authorThatIsActor" => nil
}
}
}} = Absinthe.run(doc, AshGraphql.Test.Schema, context: %{actor: user2})
end

test "a multitenant object can be read if tenant is set" do
tenant = "Some Tenant"

Expand Down
7 changes: 7 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,13 @@ defmodule AshGraphql.Test.Post do
attribute_writable?(true)
end

belongs_to(:author_that_is_actor, AshGraphql.Test.User) do
source_attribute(:author_id)
define_attribute?(false)
public?(true)
filter(expr(id == ^actor(:id)))
end

has_many(:comments, AshGraphql.Test.Comment, public?: true)
has_many(:sponsored_comments, AshGraphql.Test.SponsoredComment, public?: true)
has_many(:paginated_comments, AshGraphql.Test.Comment, read_action: :paginated, public?: true)
Expand Down

0 comments on commit 7946216

Please sign in to comment.