Many to many relations with absinthe

Hey folks, fine? Hope so.

I started to play with Absinthe few weeks ago and I’m justing learning it. As I came from REST world, I was used to make custom routes for my specific joins relations, where I can manipulate it as I want, or just embed it in a custom JSON field on the return. But in the graphql world that’s not the case, I think. That’s my problem.

Currently I have 3 schemas: EducationalResource, Factsheet, a join schema ErFactsheet and through the assoc ErFactsheet I also got a description text field. That is: my two schemas EducationalResource and Factsheet are M:N connected through ErFactsheet association, where the foreign_key for EducationalResource and Factsheet arre er_id and factsheet respectively. I just want to be able to retrieve through the ErFactsheet only the descriptions related to both EducationalResource and Factsheet. To exemplify, I wish I could query graphql in this way:

...
    factsheet{
      id
      educationalResources{
        id
        ersFactsheets {
          description
        }
      }
    }
...

Therefore I would take all the descriptions from ersFactsheets that have both factsheet_id and er_id, depending on what factsheet we are at that moment. The steps in REST would be something like:

Get the factsheet you want → let’s call it X

Take all educationalResources from it → let’s call it {Y0, Y1, Y2… YN}

Search for all ersFactsheets with where er_id=Y0 OR er_id=Y1 OR er_id=Y2 ... OR er_id=YN AND factsheet_id=X

I tried to get this work by making the following:

Ecto schema relation:

...
has_many :ers_factsheet, ErFactsheet, foreign_key: :er_id
has_many :factsheets, through: [:ers_factsheets, :factsheet]
...

Absinthe schema:

...
object :educational_resource do
    ...
    field :ers_factsheets, list_of(:ers_factsheet), resolve: dataloader(Resources)
end

object :ers_factsheet do
    ffield :description, :string
end
...

But the result I got in the query is incorrect, it’s taking all ersFactsheets from educationalResource, not filtering with factsheet_id. It means I finish the query with all ersFactsheets for the specific educationalResource regardless it’s factsheet.

The workaround by now was to query one by one ersFactsheet by it’s factsheet_id and er_id. Is there any way I could implement it through GQL like I could in REST?

thx in advance.

This kind of resolvers are very hard to manage and debug.
I’d use this query

factsheet {
  id

  ersFactsheets {
    description
  }

  educationalResources {
    id
  }
}

It simplifies resolvers a lot.

But with this query I can’t get ersFsctsheets filtered by er_id. Is there any query I can make to simplify the resolvers and filter by both fields?

How do you provide er_id?
If you already know id, then

  ersFactsheets(erId: ID_HERE) {
    description
  }

if you want to grab all the data, then

factsheet {
  id

  ersFactsheets {
    description
    erId
  }

  educationalResources {
    id
  }
}

and group results on client side.