ash-project / ash-project/ash_graphql

Better error for typos in Aggregates when using AshGraphQL

Open
#115 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Elixir
Stars
97
Forks
101
Avg merge
12h 51m
Merged PRs (30d)
5

Description

**Is your feature request related to a problem? Please describe.**
The AshGraphQL library gives an unhelpful error when there is a typo in an Aggregate definition where if AshGraphQL is not used Spark gives a helpful error.

**Describe the solution you'd like**
Somehow the Spark error should be surfaced or the AshGraphQL error should give as good of information as the Spark error

**Additional context**
I've created an example repo which has a simple project with two branches which contain the typo error with the only difference being that one branch has AshGraphQL installed and the other does not.

https://github.com/BryceLabs/ash_issues_examples/tree/aggregate-typo-issue
https://github.com/BryceLabs/ash_issues_examples/tree/aggregate-typo-issue-graphql

In the project there is an `Order` resource which has_many `OrderItem` resources. `Order` aggregates the subtotal calculation of `OrderItem` via a sum. However, there is a typo:

` aggregates do
sum :subtotal, :order_items, :sub_total
end`

`sub_total` should be `subtotal`

The error given by the plain Ash project is this:

> ** (EXIT from #PID<0.98.0>) an exception was raised:
** (Spark.Error.DslError) [App.Store.Order]
aggregates -> sub_total:
All aggregates fields must be attributes or calculations. Got: :sub_total
(ash 2.19.14) lib/ash/resource/verifiers/ensure_aggregate_field_is_attribute_or_calculation.ex:23: anonymous fn/3 in Ash.Resource.Verifiers.EnsureAggregateFieldIsAttributeOrCalculation.verify/1
(elixir 1.16.1) lib/enum.ex:2528: Enum."-reduce/3-lists^foldl/2-0-"/3
(ash 2.19.14) lib/ash/resource/verifiers/ensure_aggregate_field_is_attribute_or_calculation.ex:10: Ash.Resource.Verifiers.EnsureAggregateFieldIsAttributeOrCalculation.verify/1
lib/app/store/resources/order.ex:1: anonymous fn/1 in App.Store.Order.__verify_spark_dsl__/1
(elixir 1.16.1) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
lib/app/store/resources/order.ex:1: App.Store.Order.__verify_spark_dsl__/1
(elixir 1.16.1) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
(elixir 1.16.1) lib/module/parallel_checker.ex:271: Module.ParallelChecker.check_module/3

The error given when AshGraphQL is installed is this:

>== Compilation error in file lib/app/schema.ex ==
** (UndefinedFunctionError) function nil.embedded?/0 is undefined. If you are using the dot syntax, such as module.function(), make sure the left-hand side of the dot is a module atom
nil.embedded?()
(ash_graphql 0.27.0) lib/resource/resource.ex:2619: AshGraphql.Resource.filterable?/2
(elixir 1.16.1) lib/enum.ex:4277: Enum.filter_list/2
(ash_graphql 0.27.0) lib/resource/resource.ex:2532: AshGraphql.Resource.aggregate_filter_fields/2
(ash_graphql 0.27.0) lib/resource/resource.ex:2498: AshGraphql.Resource.resource_filter_fields/2
(ash_graphql 0.27.0) lib/resource/resource.ex:1395: AshGraphql.Resource.args/5
(ash_graphql 0.27.0) lib/resource/resource.ex:528: anonymous fn/7 in AshGraphql.Resource.queries/6
(elixir 1.16.1) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2

Associated Elixir Forum post: https://elixirforum.com/t/aggregates-causing-error-in-ashgraphql/62112

Contributor guide

Open the contributing guide

Research direction

Start with the two example branches, aggregate-typo-issue and aggregate-typo-issue-graphql, and compare the plain Spark error with the AshGraphQL failure. Read lib/resource/resource.ex around filterable?/2, aggregate_filter_fields/2, resource_filter_fields/2, and args/5. Done means a typo in an aggregate produces the useful Spark-style error instead of nil.embedded?/0.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir, graphql
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.