Filtering after expand()

Hello.

Sorry if this is in a wrong category.

Would it be possible to add a funtionality that would allow some edges with expand() to be repeated - not to get the “Repeated subgraph while using expand()” error? I have no idea how it is implemented, but perhaps this could be done when parsing the input query? Something like a new “@overwrite” directive to indicate that the latest usage of edge with some filter is supposed to be used instead of the original one from expand().

From a users point of view it is annoying to keep hard-typing all attributes (when there are many) and sometimes the query is too complicated to use variables and it is very hard (or sometimes impossible) to avoid the “expand is only compatible with type filters” error. The filter on some edges is needed because otherwise there would be too many nodes returned.

Hey @Dencomon, can you clarify if it is really related to GraphQL? and give an example?

Hi, it is related to graph database Dgraph. It probably should not be in the GraphQL category, sorry about that. How can I remove this tag?

Here is an example of what I am trying to do (don’t think too much about the logic of my example, I had to change some details in order to be able to post my problem publicly).

Consider these types:

type Person {
  person.job
  ...
  person.collaborated
}

type Company {
  company.someatr
  ...
  company.produced
  <~person.collaborated>
}

type Food {
  ...
  <~company.produced>
}

type Clothes {
  ...
  <~company.produced>
}

(Plus some other types produced by Company and other edges were omitted.)

So the relationships can be drawn like this:

(Person node) --person.collaborated edge--> (Company node) --company.produced edge--> (Food node)
                                       --company.produced edge--> (Clothes node)
                                       ...

Now what my problem is: I sometimes want to be able to do queries like the one below - and use the functionality of Types and @recurse at the same time.

{
  getProducts(func: eq(person.job, "Something")) @recurse(depth: 3, loop: true) {
    expand(_all_) (first:10)
  }
}

But what happens - e.g. the company.produced edge leads to way too many nodes and I need to be able to filter on them based on specific values of their attributes.

If the edge wasn’t part of my Type definition, I am able to do at least this:

{
  getProducts(func: eq(person.job, "Something")) @recurse(depth: 3, loop: true) {
    expand(_all_) 
    person.collaborated @filter(ge(company.someatr, 5)) 
  }
}

But I can’t use this technique on the products of a Company, because “recurse queries require that all predicates are specified in one level”. And also I need these edges in my Type definitions because they are used in other queries…

If edges are part of the Type definition and I try to use a filter like this directly, I get the “expand is only compatible with type filters” error:

{
  getProducts(func: eq(person.job, "Something")) @recurse(depth: 3, loop: true) {
    expand(_all_) @filter(ge(company.someatr, 5)) 
  }
}

Or am I missing something and a query like this can be easily written using variables? So far I was not successful. Thank you very much for your time and help in advance!

Hi @MichelDiz again. I just read my question again and probably my formulation was unnecessarily confusing. What would be a solution to my problem is actually if other @filters could be used with expand() and not only those on Types (https://dgraph.io/docs/query-language/expand-predicates/#filtering-during-expand) - “other type of filters and directives are not currently supported with the expand function”. Do you happen to know if they will be supported any time soon?

I have created this ticket some time ago Add other filters to expand() - As far I can tell this is pretty low priority right now. It may change with popularity (more users asking for this) or in few months if the other priorities decreases.