Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering based on child table relationship #88

Open
bossjapzz opened this issue Feb 21, 2022 · 24 comments
Open

Support filtering based on child table relationship #88

bossjapzz opened this issue Feb 21, 2022 · 24 comments
Assignees
Labels
enhancement New feature or request

Comments

@bossjapzz
Copy link

Describe the bug
Support filtering based on child table relationship

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

@bossjapzz bossjapzz added the triage-required Pending triage from maintainers label Feb 21, 2022
@olirice olirice added enhancement New feature or request and removed triage-required Pending triage from maintainers labels Feb 21, 2022
@github-actions
Copy link

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

@github-actions github-actions bot added the stale label Mar 24, 2022
@apecollector
Copy link

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

@olirice olirice removed the stale label Mar 30, 2022
@gromchen
Copy link

How can I vote for this enhancement? 😅

@olirice
Copy link
Contributor

olirice commented May 25, 2022

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

@vwkd
Copy link

vwkd commented Jan 5, 2023

+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.)

@robertn702
Copy link
Contributor

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

@olirice
Copy link
Contributor

olirice commented Mar 30, 2023

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

@cullophid
Copy link

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

Keep up the good work pg_graphql is very promising!

@davidchalifoux
Copy link

I'm also looking for this feature!

@azlekov
Copy link

azlekov commented Jul 13, 2023

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

@olirice
Copy link
Contributor

olirice commented Jul 13, 2023

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

@magrinj
Copy link

magrinj commented Nov 14, 2023

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 !

@olirice
Copy link
Contributor

olirice commented Nov 14, 2023

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

@RobSchilderr
Copy link

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?

@olirice
Copy link
Contributor

olirice commented Jan 3, 2024

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

@Mihai-github
Copy link

Mihai-github commented Feb 12, 2024

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?

@imor
Copy link
Contributor

imor commented Feb 13, 2024

@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?

@bryanmylee
Copy link
Contributor

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?

@imor
Copy link
Contributor

imor commented Apr 11, 2024

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.

@bh865
Copy link

bh865 commented Aug 13, 2024

We have any update on this?

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

Do you have a preferred method of these 3?

@olirice
Copy link
Contributor

olirice commented Aug 14, 2024

My preferred option would be #1
Extend with a function that performs the aggregation and then filter on that value
Docs: https://supabase.github.io/pg_graphql/computed_fields/#extending-types-with-functions

@bh865
Copy link

bh865 commented Aug 26, 2024

My preferred option would be #1 Extend with a function that performs the aggregation and then filter on that value Docs: https://supabase.github.io/pg_graphql/computed_fields/#extending-types-with-functions

So, I've managed to get a function that I can query successfully in the SQL Editor but it does not show up in my GraphiQL playground. Any suggestions? I left out 'immutable' and 'stable' as I've tried both to no avail.

CREATE OR REPLACE FUNCTION filter_events_search(gender VARCHAR) RETURNS TABLE( id UUID ) LANGUAGE plpgsql AS $$ BEGIN RETURN QUERY SELECT e.id FROM events e JOIN teams t ON e.team_id = t.id WHERE t.gender = filter_events_search.gender; END; $$;

Looks like I finally got it working! Thanks for the direction!

@bobbybol
Copy link

Hi @olirice, any progress on this? Thanks ;)

@olirice
Copy link
Contributor

olirice commented Sep 24, 2024

not yet. we're currently working on upsert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests