How to represent an embedded schema in AshGraphql

I have an existing Ecto schema that has an embedded_field, which itself is backed by an Ecto schema. How do I use the attributes DSL to expose this field in my AshGraphql query? Here's the field in Ecto: embeds_one :details, Details I tried this in the Ash attributes: attribute :details, :map, but the GraphQL response had this error: Field "details" must not have a selection since type "JsonString" has no subfields.
4 Replies
ZachDaniel
ZachDaniel3y ago
Ash has embedded resources, that function in much the same way
defmodule Foo do
use Ash.Resource,
data_layer: :embedded,
extensions: [AshGraphql.Resource]

graphql do
type :foo
end

attributes do
attribute :stuff, :string
end
end
defmodule Foo do
use Ash.Resource,
data_layer: :embedded,
extensions: [AshGraphql.Resource]

graphql do
type :foo
end

attributes do
attribute :stuff, :string
end
end
That will derive a type for your embedded resource, and when you use that in a resource, i.e
attribute :detauls, Foo
attribute :detauls, Foo
it will assume the proper type
moxley
moxleyOP3y ago
Ah, okay. Thank you. How do I know when to use attribute for this case, and not has_one? In Ecto, it would be an embedsone Does Ash use attribute in the cases where Ecto uses `embeds*`?
ZachDaniel
ZachDaniel3y ago
Yes, in ash there is no embeds concept Just resources that can behave the same as a type so data_layer: :embedded just means "this is an Ash.Type resource" and so you can use it in attributes
moxley
moxleyOP3y ago
Perfect, thanks!

Did you find this page helpful?