BryanJBryce

BryanJBryce

Aggregates causing error in AshGraphQL

Orders have many OrderItems have many OrderItemModifiers

OrderItem and OrderItemModifier both have attributes of quantity and price

I want Order to be able to aggregate/calculate the total for the order.

First I looked at Aggregates, specifically at sum, but since I need to do multiplication I thought maybe a Calculation would be needed. I’d like to do it as an expression so that Postgres is doing the calculation/aggregation and not Elixir code.

Update: I think I figured this out, Calculations and Aggregates are not an either or thing where if Aggregates don’t work use Calculations, you can also build up complex logic by combing simple calculations and Aggregates, I think…

I still have an error in GraphQL with one of the Aggregates, so I’m going to put my solution here and see if there’s an issue with it:

OrderItemModifier:

  calculations do
    calculate :total, :decimal, expr(price * quantity)
  end

OrderItem:

  calculations do
    calculate :subtotal, :decimal, expr(price * quantity + modifiers_total)
  end

  aggregates do
    sum :modifiers_total, :modifiers, :total
  end

Order:

  aggregates do
    sum :subtotal, :order_items, :sub_total
    sum :tax_total, :order_taxes, :percentage
  end

  calculations do
    calculate :total, :decimal, expr(subtotal * tax_total)
  end

It could just be a typo somewhere (GraphQL errors like this are typically basic Ash errors that don’t get caught), but I’m getting this error from AshGraphQL after trying to compile this new code:

== Compilation error in file lib/point_of_sale/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.26.9) lib/resource/resource.ex:2473: AshGraphql.Resource.filterable?/2
(elixir 1.16.1) lib/enum.ex:4277: Enum.filter_list/2
(ash_graphql 0.26.9) lib/resource/resource.ex:2386: AshGraphql.Resource.aggregate_filter_fields/2
(ash_graphql 0.26.9) lib/resource/resource.ex:2352: AshGraphql.Resource.resource_filter_fields/2
(ash_graphql 0.26.9) lib/resource/resource.ex:1283: AshGraphql.Resource.args/5
(ash_graphql 0.26.9) lib/resource/resource.ex:473: anonymous fn/6 in AshGraphql.Resource.queries/5
(elixir 1.16.1) lib/enum.ex:1700: Enum.“-map/2-lists^map/1-1-”/2

Marked As Solved

BryanJBryce

BryanJBryce

LOL, indeed it was a typo:

:sub_total in the Order aggregate :subtotal should be :subtotal

Now to test if this chain of calculations and aggregates works.

Also Liked

BryanJBryce

BryanJBryce

Where Next?

Popular in Questions Top

New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement