pg_graphql icon indicating copy to clipboard operation
pg_graphql copied to clipboard

Support filtering based on child table relationship

Open bossjapzz opened this issue 3 years ago • 24 comments

Describe the bug Support filtering based on child table relationship

Expected behavior Screen Shot 2022-02-21 at 5 33 29 PM

bossjapzz avatar Feb 21 '22 09:02 bossjapzz

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Mar 24 '22 02:03 github-actions[bot]

Would like to see this as well, this is really what makes graphql appealing imo.

apecollector avatar Mar 30 '22 14:03 apecollector

How can I vote for this enhancement? 😅

gromchen avatar May 22 '22 08:05 gromchen

you just did!

I agree this would be a great feature. We're currently focusing on some rough edges where we're not quite spec compliant + and improving type support (json/jsonb) arrays etc.

Once we have a good foundation for all the basics, this will be a feature we'll be interested in, but it isn't actively under development yet

olirice avatar May 25 '22 18:05 olirice

+1 (Adding context from #296)

Hasura can do this for both child and parent relationships. AFAIK the REST API can do this currently only for child relationships (see "Filter Foreign Tables").

For example with Hasura, this selects authors containing a name with all their books, or authors and the specific books whose description contains the name. This is often desired in a search. Note how this filters both on child and parent relationships.

query {
  authors(where: {_or: [{name: {_iregex: $name}}, {books: {description: {_iregex: $name}}}]}) {
    name
    books(where: {_or: [{author: {name: {_iregex: $name}}}, {description: {_iregex: $name}}]}) {
      title
      description
    }
  }
}

(Note, for the relationship names to be set like this in Hasura, the foreign key columns should not match the table name. For example, one can use a suffix like _id, e.g. books_id column in authors table and author_id column in books table.)

vwkd avatar Jan 05 '23 16:01 vwkd

Any update on this feature? It's currently my biggest blocker.

robertn702 avatar Mar 29 '23 20:03 robertn702

No movement yet but I'm aware that this is high interest feature

The only higher priority is user defined mutations via SQL functions

FYI: Everyone is currently finalizing deliverables for launch week 8. You should see the pace of development pick back up after that

olirice avatar Mar 30 '23 13:03 olirice

I just wanted to add to the hype of this feature 🥳.

Keep up the good work pg_graphql is very promising!

cullophid avatar Jun 14 '23 21:06 cullophid

I'm also looking for this feature!

davidchalifoux avatar Jun 25 '23 17:06 davidchalifoux

Interested in this one. Is there any workaround now like database views or whatever?

azlekov avatar Jul 13 '23 13:07 azlekov

Interested in this one. Is there any workaround now like database views or whatever?

unfortunately, there isn't currently a workaround for filtering parents based on children

olirice avatar Jul 13 '23 15:07 olirice

Hey @olirice, Any update on this ? We're using pg_graphql at Twenty when querying the database for the dynamic part of the GraphQL API, and really need this feature Thanks a lot to Supabase for this awesome project !

magrinj avatar Nov 14 '23 15:11 magrinj

none yet, but we've identified nested inserts (which requires upsert support to work well) as the next big project we're tackling in pg_graphql so you should start to see movement on it

olirice avatar Nov 14 '23 22:11 olirice

none yet, but we've identified nested inserts (which requires upsert support to work well) as the next big project we're tackling in pg_graphql so you should start to see movement on it

is there any workaround to achieve the same effect right now?

RobSchilderr avatar Jan 03 '24 07:01 RobSchilderr

You could

  • write a function that performs the filter ahead of time
  • use two requests / filter on the client
  • use a computed field if you're always filtering the child in the same way (no parameters required)

but all have their drawbacks

olirice avatar Jan 03 '24 16:01 olirice

I'm trying to filter data from table A based on a value in table B. Specifically, I want to filter records where B.something (some condition) specific value. I haven't been able to find an example of this query in the Supabase documentation. Could you please confirm if this type of filtering is supported in GraphQL on Supabase? If not, are there any workarounds or alternative approaches I could consider?

Mihai-github avatar Feb 12 '24 14:02 Mihai-github

@Mihai-github have you tried one of the options mentioned by @olirice above? If these don't work for you can you share more details about your use case?

imor avatar Feb 13 '24 08:02 imor

I'd love to tackle this problem, but I'm not sure how the GQL is translated into a PG query. Is the GQL just translated into a pgREST query?

bryanmylee avatar Apr 11 '24 09:04 bryanmylee

Is the GQL just translated into a pgREST query?

No, GQL is converted into a SQL query which is executed by the QueryEntrypoint::execute method for a query and MutationEntrypoint::execute method for a mutation.

I'm not sure how the GQL is translated into a PG query

The QueryEntrypoint::execute and MutationEntrypoint::execute methods convert GQL into SQL by calling the self.to_sql_entrypoint methods. The to_sql_entrypoint methods are implemented by various builder objects that implement the QueryEntrypoint or MutationEntrypoint traits. E.g. FunctionCallBuilder's impl just delegates to a call to its to_sql method. These builder objects are created earlier by parsing GQL and looking at the SQL context.

Let me know if this is sufficient detail for you to take a stab at implementing a solution or you need more.

imor avatar Apr 11 '24 09:04 imor