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 description
s 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 whereer_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.