drumusician
drumusician
AEAsh Elixir
Created by drumusician on 9/22/2023 in #support
Cannot return null for non-nullable field
Hi Guys, I have an attribute in a resource that is a jsonb object in postgres and am defaulting it to an empty object in the db and also want to return it as an emoty object in GraphQL. However it seems to be returning null if the the object is empty... Is this something AshGraphQL is doing under the hood?
15 replies
AEAsh Elixir
Created by drumusician on 9/13/2023 in #support
sort many_to_many
I thought I had asked this question before, but can't seem to find it... It seems like a very common use case, but not sure how to go about this. Is it possible to sort a many_to_many relationship based on a field in the join table. ie. if I have a Playlist:
defmodule Playlist do
relationships do
has_many :playlist_entries, PlaylistEntry
many_to_many :items, Item
end
end
defmodule Playlist do
relationships do
has_many :playlist_entries, PlaylistEntry
many_to_many :items, Item
end
end
and an Item:
defmodule Item do
...
end
defmodule Item do
...
end
And a PlaylistEntry:
defmodule PlaylistEntry
relationships do
belongs_to :item, Item
belongs_to :playlist, Playlist
end

attributes do
attribute :position, :integer
end
end
defmodule PlaylistEntry
relationships do
belongs_to :item, Item
belongs_to :playlist, Playlist
end

attributes do
attribute :position, :integer
end
end
And I want to simply load this in graphql by just adding the items:
query {
playlist {
items {
name
//ordered by position in playlist_entry
}
}
}
query {
playlist {
items {
name
//ordered by position in playlist_entry
}
}
}
I know that in graphql not all values that are not needed in the read are selected, but apart from graphQL is there a way to get this sorting happening. Thanks all!
9 replies
AEAsh Elixir
Created by drumusician on 9/11/2023 in #support
ManualRead not resolving in graphQL
Hi All, I have created a ManualRead module to implement a tricky relationship which has defaults that can be overridden. Before going too much into details, this ManualRead is working fine when I use the code interface, but when I call the same action through graphQL the values I expect are all nil. It is a bit too long to paste here, but was wondering if there is something general I need to think of when doing this?
146 replies
AEAsh Elixir
Created by drumusician on 9/8/2023 in #support
InvalidFilterValue for read action
I have this relatively simple read action, but it keeps on failing with a %Ash.Error.Query.InvalidFilterValue
read :for_organisation do
argument(:organisation_id, :integer) do
allow_nil?(false)
end

prepare(fn query, _context ->
query
|> Ash.Query.load([:game_meta])
|> Ash.Query.filter(game_meta.organisation_id == ^arg(:organisation_id))
end)
end
read :for_organisation do
argument(:organisation_id, :integer) do
allow_nil?(false)
end

prepare(fn query, _context ->
query
|> Ash.Query.load([:game_meta])
|> Ash.Query.filter(game_meta.organisation_id == ^arg(:organisation_id))
end)
end
This is the relationship in game_meta:
belongs_to(:organisation, McFun.Games.Organisation,
attribute_type: :integer,
attribute_writable?: true
)
belongs_to(:organisation, McFun.Games.Organisation,
attribute_type: :integer,
attribute_writable?: true
)
Any ideas why this is happening?
7 replies
AEAsh Elixir
Created by drumusician on 6/29/2023 in #support
many_to_many with non-default attribute not preloading...
So, I have a many to many relationship I'm strugling with. It must be something silly, but I just can't make it work. I have a resource called Game, which is actually for a table called assets, so in the join table one of the attributes is asset_id and not game_id. The join table is called playlist_entries Any help would be greatly appreciated. Basically it is not loading the games correctly for the playlist. Addng the full resources made the post too long, so hopefully this will shed enough light... Playlist
defmodule McFun.Games.Playlist do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [
AshGraphql.Resource
]

alias McFun.Games.PlaylistEntry
alias McFun.Games.Game

postgres do
schema("games")
repo(McCore.Repo)
table("playlists")
end

relationships do
many_to_many :games, Game do
through(PlaylistEntry)
source_attribute_on_join_resource(:playlist_id)
destination_attribute_on_join_resource(:asset_id)
end
end
end
defmodule McFun.Games.Playlist do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [
AshGraphql.Resource
]

alias McFun.Games.PlaylistEntry
alias McFun.Games.Game

postgres do
schema("games")
repo(McCore.Repo)
table("playlists")
end

relationships do
many_to_many :games, Game do
through(PlaylistEntry)
source_attribute_on_join_resource(:playlist_id)
destination_attribute_on_join_resource(:asset_id)
end
end
end
PlaylistEntry
postgres do
schema("games")
repo(McCore.Repo)
table("playlist_entries")
end

relationships do
belongs_to :playlist, Playlist do
source_attribute(:playlist_id)
primary_key?(true)
allow_nil?(false)
attribute_type(:integer)
end

belongs_to :game, Game do
source_attribute(:asset_id)
primary_key?(true)
allow_nil?(false)
attribute_type(:integer)
end
postgres do
schema("games")
repo(McCore.Repo)
table("playlist_entries")
end

relationships do
belongs_to :playlist, Playlist do
source_attribute(:playlist_id)
primary_key?(true)
allow_nil?(false)
attribute_type(:integer)
end

belongs_to :game, Game do
source_attribute(:asset_id)
primary_key?(true)
allow_nil?(false)
attribute_type(:integer)
end
Game
postgres do
schema("games")
repo(McCore.Repo)
table("assets")
end
end
postgres do
schema("games")
repo(McCore.Repo)
table("assets")
end
end
33 replies
AEAsh Elixir
Created by drumusician on 6/11/2023 in #support
Custom / virtual fields on read
Hi All, I am creating an API based on an old system and can't modify the original, but I want to change the way the resources are read. More specifically I want to set a certain virtual field based on the value of an existing database field. Normally this is something I would achieve with a virtual field and then modify the output in the view layer. But I understand the idiomatic way in Ash is to push as much as possible into the resource. I read in another post that arguments are basically the equivalent of virtual fields, so looked into setting those based on an attribute value. What would be the best approach to achieve something like this. I already experimented with Ash.Query a bit, but not sure how to achieve reading an attribute from the fetched resource and setting an argument based on that. Any advise and/or guidance is much appreciated. I tried to find it in the docs but am a bit lost where to look... Thanks!
17 replies
AEAsh Elixir
Created by drumusician on 5/30/2023 in #support
@derive Jason.Encoder
Hi All, I have an embedded Json object in Ash which is consumed by GraphQL. I have configured graphQL to use json instead of json_string like described here => https://hexdocs.pm/ash_graphql/use-json-with-graphql.html But now I get a Jason.Encoder error. In Ecto this can be solved by adding @derive Jason.Encoder in the module. Is there a way in Ash to do this?
8 replies