How to implement reverse query in GraphQL API

OR, you could fix the schema and do it all within GraphQL using the @hasInverse

type User {
    id: ID!
    name: String! @search(by: [term])
    follows: [User] 
    hasFollowers: [User] @hasInverse(field: follows)
    ...
}

Note: Adding this to the schema with existing data, does not change the underlying data, so if you have existing data, you would have to manually fix it to get a working solution for past data.

2 Likes