AF
Ash Framework•19h ago
Geril

Flattening Polymorphic Relationships in Ash GraphQL

Hey folks 👋 I’m working with polymorphic relationships in Ash and followed the guide here: https://hexdocs.pm/ash/polymorphic-relationships.html. I tried exposing it through the GraphQL API, and I’m a bit unsure if there’s a way to shape the schema the way I want. Right now, when I query bankAccounts, I get something like this:
query {
bankAccounts {
id
name
implementation {
... on CheckingAccount {
overdraftLimit
}
... on SavingsAccount {
interestRate
}
}
}
}
query {
bankAccounts {
id
name
implementation {
... on CheckingAccount {
overdraftLimit
}
... on SavingsAccount {
interestRate
}
}
}
}
So BankAccount gives me the shared fields, and the account-specific fields are nested under implementation. What I’d like instead is something more “flattened,” where the bankAccounts field itself is the union/interface, so I can query like this:
query {
bankAccounts {
id
name
... on CheckingAccount {
overdraftLimit
}
... on SavingsAccount {
interestRate
}
}
}
query {
bankAccounts {
id
name
... on CheckingAccount {
overdraftLimit
}
... on SavingsAccount {
interestRate
}
}
}
That way, the shared fields (id, name) and the resource-specific fields (overdraftLimit or interestRate) all come back directly, without the extra implementation wrapper. In my mind, this seems like it could involve creating a custom type and resolver, but I wasn’t sure if I should dive down that path or if there’s a simpler/standard way in Ash to achieve this. Is there a way to configure Ash GraphQL to expose polymorphic relationships like this, or is the nested implementation structure the standard/only approach? Thanks a lot! 🙏
1 Reply
barnabasj
barnabasj•19h ago
no that would be graphql interfaces IIRC and we don't really have anything for this at the moment but what you could do is wrap the relationship in a calculation that returns a typed struct or a map with constraints with the shared fields

Did you find this page helpful?