diff --git a/test/alias_test.exs b/test/alias_test.exs index 2d8a2d9..877f98a 100644 --- a/test/alias_test.exs +++ b/test/alias_test.exs @@ -171,6 +171,50 @@ defmodule AliasTest do } = result end + test "calculation alias works correctly for forbidden field" 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) { + private_calc: private_calculation { + nestedEmbed { + name + } + } + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema, + variables: %{ + "id" => post.id + } + ) + + assert {:ok, result} = resp + + assert Map.has_key?(result, :errors) + + assert %{ + errors: [ + %{ + code: "forbidden_field" + } + ] + } = + result + end + test "aggregate alias works correctly" do post = AshGraphql.Test.Post diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 90f051e..c8fc63e 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -110,6 +110,16 @@ defmodule AshGraphql.Test.Post do end end + field_policies do + field_policy :* do + authorize_if(always()) + end + + field_policy [:private_calculation, :private_attribute] do + forbid_if(always()) + end + end + graphql do type :post @@ -364,6 +374,11 @@ defmodule AshGraphql.Test.Post do attribute(:embed_union_unnested, AshGraphql.Types.EmbedUnionNewTypeUnnested, public?: true) attribute(:string_new_type, AshGraphql.Types.StringNewType, public?: true) + attribute(:private_attribute, :boolean) do + default(true) + public?(true) + end + attribute :required_string, :string do allow_nil? false default("test") @@ -375,6 +390,21 @@ defmodule AshGraphql.Test.Post do calculations do calculate(:static_calculation, :string, AshGraphql.Test.StaticCalculation, public?: true) + + calculate(:private_calculation, AshGraphql.Test.Embed, fn records, _ -> + records + |> Enum.map(fn + %{private_attribute: true} -> + %AshGraphql.Test.Embed{} + + %{private_attribute: true} -> + nil + end) + end) do + public?(true) + load(:private_attribute) + end + calculate(:full_text, :string, FullTextCalculation, public?: true) calculate(:text_1_and_2, :string, expr(text1 <> ^arg(:separator) <> text2)) do