Skip to content

Commit

Permalink
fix: properly generate first aggregates with correct destination type
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 14, 2023
1 parent 2cf5b60 commit e66a3ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3670,26 +3670,42 @@ defmodule AshGraphql.Resource do
|> Enum.map(fn aggregate ->
name = field_names[aggregate.name] || aggregate.name

{field_type, constraints} =
{field, field_type, constraints} =
with field when not is_nil(field) <- aggregate.field,
related when not is_nil(related) <-
Ash.Resource.Info.related(resource, aggregate.relationship_path),
attr when not is_nil(attr) <- Ash.Resource.Info.field(related, aggregate.field) do
{attr.type, attr.constraints}
{attr, attr.type, attr.constraints}
else
_ ->
{nil, []}
{nil, nil, []}
end

{:ok, agg_type, constraints} =
Aggregate.kind_to_type(aggregate.kind, field_type, constraints)

attribute = field || Map.put(aggregate, :constraints, constraints)

type =
if is_nil(Ash.Query.Aggregate.default_value(aggregate.kind)) do
field_type(agg_type, Map.put(aggregate, :constraints, constraints), resource)
resource =
if field do
Ash.Resource.Info.related(resource, aggregate.relationship_path)
else
resource
end

field_type(agg_type, attribute, resource)
else
resource =
if field && aggregate.type in [:first, :list] do
Ash.Resource.Info.related(resource, aggregate.relationship_path)
else
resource
end

%Absinthe.Blueprint.TypeReference.NonNull{
of_type: field_type(agg_type, Map.put(aggregate, :constraints, constraints), resource)
of_type: field_type(agg_type, attribute, resource)
}
end

Expand Down
1 change: 1 addition & 0 deletions test/support/resources/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule AshGraphql.Test.Comment do
attribute :type, :atom do
writable?(false)
default(:comment)
constraints(one_of: [:comment, :reply])
end

create_timestamp(:created_at)
Expand Down
4 changes: 4 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ defmodule AshGraphql.Test.Post do
aggregates do
count(:comment_count, :comments)
max(:latest_comment_at, [:comments], :timestamp)

first :latest_comment_type, [:comments], :type do
sort(timestamp: :desc)
end
end

relationships do
Expand Down

0 comments on commit e66a3ef

Please sign in to comment.