I have a self-referencing model, and I was able to get it to work with the following. However, this leads to 2 different cached objects in Apollo. Ex, ChildCategory:[id] and Category:[Id]. I would like to have a single source of truth so it makes optimistic updates more manageable.
object :child_category do
field :id, :id
field :name, :string
field :category_id, :id
end
object :category do
field :id, :id
field :name, :string
field :categories, list_of(:child_category), resolve: dataloader(DataloaderQueries, :categories)
end
I tried this, but it leads to an error saying it can’t query the categoryId on category. I also don’t want there to be infinite recursion. I want the second level of categories to exclude the key categories.
object :category do
field :id, :id
field :name, :string
field :categories, list_of(:category), resolve: dataloader(DataloaderQueries, :categories)
end
I get the error when I try to query with this document (I removed parts of it for brevity):
Cannot query field \"categoryId\" on type \"Category\"."
query posts($id: ID!) {
categories {
...CategoryFields
__typename
}
}
fragment CategoryFields on Category {
id
name
categories {
id
name
categoryId
__typename
}
__typename
}
I figured it out. I needed to add category_id here:
object :category do
field :id, :id
field :name, :string
field :category_id, :id
field :categories, list_of(:category), resolve: dataloader(DataloaderQueries, :categories)
end