Can you have a schema association that doesn't map to just one table?


Is it possible to have an association on a schema map to multiple tables?

I’m using Absinthe and I want my “tablets” object to have a field called “blocks”. Tablets map directly to the tablet schema so this is fine, however to make my life easier on the frontend “blocks” contain fields from a number of different tables (about 3 different tables). Right now I have a custom resolver for the “blocks” field that does the query against these 3 tables and creates a new map with the data I need.

This works fine enough but because “blocks” isn’t an association on the “tablets” schema (since it doesn’t map cleanly to a particular table) I find myself with a few annoyances:

  • I’m not sure how to make this work with dataloader (since I don’t think you can have an association on the tablets schema for blocks since it doesn’t map to a table).
  • I can’t preload “blocks” (Instead if I want the blocks I have to run the query and create the map)
  • I have to pass around the blocks seperately to the tablet (since the tablet is a struct and the blocks are an adhoc map)

Ideally I want:

  • blocks to somehow be a part of the “tablets” schema
  • The ability to use preload for blocks, where preload somehow knows to run a specific query and create a block
  • Have “blocks” be a schema or struct (instead of just a plain map)
  • Be able to use dataloader to fetch blocks instead of needing a custom resolver

I realise that since “blocks” don’t map to a specific table then the concept of a schema may not apply. I’m happy to just use a plain struct, as long as I can somehow associate that struct with the tablet schema.

Thanks in advance for the help!

A schema may be nor related to a table. Sometimes I use schema just for validation.
If I don’t mistaken myself you’re looking for embedded_schema, There is an example there:
Regarding preload, you can pass it query to customize how the preload is done:

So I would have an embedded schema for “blocks” and can I associate that with the “tablets” schema? I think associations work at the DB level and assume that there is a foreign key so can you have a virtual association?

And as for the preload, it means you need to specify the query every time you preload it, do you know if there is a way to say - “here is the query that should always be used for preloading”?

Pretty much, yes.

Hello @Francesco, Any luck with solving this issue?

I am facing the same schema relationships where I have an embedded schema (not backed with table in the DB) associated to a schema that is backed by a table in the DB.

I was trying to preload the associations but it is not quite working for me.