Introducing Nested Filtering for Dgraph GraphQL API with Optimized Query Support

I’ve added nested filtering to the generated GraphQL API for Dgraph and would like to request for feedback and help with user or unit testing.

How Does It Work?

This feature leverages Dgraph’s var block to filter nested objects and selects parent objects through inverse predicates. To enable nested filtering, you need to add the @search directive to the nested field (edge) without any arguments.

Example:

type User {
    name: String @search(by: [hash])
    friends: [User] @hasInverse(field: friends) @search
}

Given the schema above, you can query users who have friends named “Ben” using the following query:

query {
    queryUser(filter: { friends: { name: { eq: "Ben" } } }) {
        name
    }
}

Schema Validation:

If the @search directive is added to an edge, the @hasInverse directive must also be set on one of the referenced fields. This validation process includes handling interface fields by checking the implemented types for reverse fields.

4 Likes

Does the @cascade directives available with Graphql queries are a way to accomplish nested filtering, given that you can apply filters in nested nodes in the payload graph ?

If your aim is to filter on nested objects, you can abandon @cascade completely and use this feature. However, if your aim is to return objects without missing values, then use @cascade.

In addition to that, you can’t use aggregate functions with @cascade but here you can.

3 Likes