I found the solution. Totally forgot about @cascade
. I can implement a Node
interface to all types (or at least the types I want to be referencable by Sorting
). Sorting then references to Node and I a combination of @cascade
on Sorting and @filter
on contextRef
does the job.
Additionally, introducing the Node
interface and applying it to all types has additional advantages in case you use Relay. I’ve wrote a post about this in case someone is interested
Full example
Schema
interface Node {
id: ID!
}
interface Sortable {
id: ID!
sortings: [Sorting!]!
}
type Sorting {
id: ID!
sequence: Int!
contextRef: Node!
}
type Course implements Node {
id: ID!
title: String!
chapters: [Chapter!]
}
type Chapter implements Sortable {
id: ID!
title: String!
# from Sortable
# sortings: [Sorting!]!
}
Filtered query
query CourseQuery($id: ID!) {
queryCourse(filter: { id: [$id] }) {
chapters {
sortings @cascade(fields: ["contextRef"]) {
sequence
contextRef(filter: { id: [$id] }) {
id
}
}
}
}
}