From 61a8b237d69b593dfa250b9af6fe14c2048d64b2 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 4 Nov 2024 19:01:21 -0500 Subject: [PATCH] fix: load fields after building query for action --- lib/graphql/resolver.ex | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/graphql/resolver.ex b/lib/graphql/resolver.ex index 1c1ab85b..65b32bfc 100644 --- a/lib/graphql/resolver.ex +++ b/lib/graphql/resolver.ex @@ -328,6 +328,11 @@ defmodule AshGraphql.Graphql.Resolver do |> Ash.Query.set_context(get_context(context)) |> set_query_arguments(action, args) |> select_fields(resource, resolution, type_name) + |> Ash.Query.for_read(action, %{}, + actor: opts[:actor], + domain: domain, + authorize?: AshGraphql.Domain.Info.authorize?(domain) + ) |> load_fields( [ domain: domain, @@ -343,13 +348,7 @@ defmodule AshGraphql.Graphql.Resolver do ) result = - query - |> Ash.Query.for_read(action, %{}, - actor: opts[:actor], - domain: domain, - authorize?: AshGraphql.Domain.Info.authorize?(domain) - ) - |> Ash.read_one(opts) + Ash.read_one(query, opts) result = add_read_metadata( @@ -443,15 +442,22 @@ defmodule AshGraphql.Graphql.Resolver do ), relay? ), - initial_query <- + query <- query |> Ash.Query.set_tenant(Map.get(context, :tenant)) |> Ash.Query.set_context(get_context(context)) |> set_query_arguments(action, args) |> select_fields(resource, resolution, type_name, result_fields), + query <- + query + |> Ash.Query.for_read(action, %{}, + actor: Map.get(context, :actor), + domain: domain, + authorize?: AshGraphql.Domain.Info.authorize?(domain) + ), query <- load_fields( - initial_query, + query, [ domain: domain, tenant: Map.get(context, :tenant), @@ -465,14 +471,7 @@ defmodule AshGraphql.Graphql.Resolver do context, result_fields ), - {:ok, page} <- - query - |> Ash.Query.for_read(action, %{}, - actor: Map.get(context, :actor), - domain: domain, - authorize?: AshGraphql.Domain.Info.authorize?(domain) - ) - |> Ash.read(opts) do + {:ok, page} <- Ash.read(query, opts) do result = paginate(resource, gql_query, action, page, relay?) {result, [query, result]} else