hasura / hasura/graphql-engine

Self-reference in `where` clause

Open
#9,447 1 comment 3 reactions 0 assignees View on GitHub
k/enhancement t/native-dbs
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Is your proposal related to a problem?

Would be amazing if an entity self-referencing mechanism existed in the graphQL extensions. I mainly envision this being used in `where` clauses. Example:

Let's say I am trying to find children through a relationship where on its descendants I am trying to test that two fields are the same to ascertain they are a type of entity I need.
If the child in question is known at the time of querying (for example foreign key gained from event trigger), props can of course be used as args to achieve this:

```
query myQuery(
$id: String!
$child_id: String!
) {
parents_by_pk(id: $id) {
child {
grand_child_linked_on_from_id(
where: {
from_id: { _eq: $child_id }
to_id: { _eq: $child_id }
}
) {
id
}
}
some_other_child {
id
}
}
}
```

But if I am trying to do this in a context where I don't already know the child entity but am also trying to find it in the same query as I'm trying to use its props to filter its descendants, as far as I know it cannot be done in a single query.

### Describe the solution you'd like

This may already exist and I was unable to find it. It would be super cool to be able to self-reference entities in the `where` clause. Absolutely no idea how hard/easy/impossible this might be to achieve. Might also be completely off with the use of a directive. Example:

```
query myQuery(
$id: String!
# here I do not yet now what "child" is
) {
parents_by_pk(id: $id) {
child {
grand_child_linked_on_from_id(
where: {
from_id: { _eq: @this(to_id) }
}
) {
id
}
}
}
}
```

### Describe alternatives you've considered

Alternative of course is to simply fetch *all* `grand_child` records and filter in app code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.