Filter On Single Relations
See original GitHub issueI have
CREATE TABLE publisher (
publisher_id serial PRIMARY KEY,
name text
);
CREATE TABLE author (
author_id serial PRIMARY KEY,
name text
);
CREATE TABLE book (
book_id serial PRIMARY KEY,
title text
publisher integer REFERENCES publisher(publisher_id),
author integer REFERENCES author(author_id)
);
I display all the books in a table from the query:
query allBooks {
allBooks {
nodes {
book_id
title
publisherByPublisher {
name
}
authorByAuthor {
name
}
}
}
}
I want to be able to apply arbitrary filters such as all the books with publisher.name ilike ‘%foo%’ or author.name ilike ‘%bar%’ or both. Also, I would like to be able to construct a single query that accepts filters but will return all results if “unfiltered.”
This seems somewhat related to #26 but I decided to open a new issue because of how graphile has changed since that one was opened. Looking into how to implement this myself but any assistance you can provide would be greatly appreciated.
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (10 by maintainers)
Top Results From Across the Web
Filter a relation using projection and restriction - Retrieve Data ...
Filter a relation using projection and restriction ... These are unary operations, which means that they are defined on a single relation.
Read more >Filtering in relational data models
The {dm} package offers functions to work with relational data models in R. This document introduces you to filtering functions, and shows how ......
Read more >Filtering on a one-to-many relationship in MySQL
I am assuming an active member is one who has a membership whose date range includes the current date. Then to select active...
Read more >Filter Which Relations Appear? : r/Notion - Reddit
I have a GTD setup in Notion with two related databases: one for next actions (tasks) and another for projects. Each task gets...
Read more >Model relationships in Power BI Desktop - Microsoft Learn
A model relationship propagates filters applied on the column of one model table to a ... As long as a filter is applied...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just published a
forward-relations
branch with this functionality included. I still need to add proper tests before merging.For anyone curious as to how this actually works in PostGraphile, I’ll walk you through it…
Let’s start with a simple schema:
and a simple GraphQL query with no filter argument:
which PostGraphile transforms into the following SQL:
Now let’s run a GraphQL query with a simple filter:
which PostGraphile transforms into the following SQL:
Now let’s assume we want to filter on the
name
field in theauthor
table using the following GraphQL query:If PostGraphile had generated the original SQL using joins, the
__local_2__
alias would be accessible to the where clause where the filter plugin operates, and we could simply add another condition such as__local_2__."name" = $1
. But since__local_2__
is buried in a subselect, we need to rebuild that link within the where clause.(NOTE: Using a join here would almost certainly be more performant, but PostGraphile does not currently support joins; see https://github.com/graphile/graphile-engine/issues/2. If join support is added at some point, we should revisit this implementation.)
The SQL query we want to generate (credit to @benjie for the
where exists(select 1 from ... )
tip) looks like this:We just duplicated the logic from the subselect (with
__local_2__
becoming__local_3__
) and then tacked on__local_3__."name" = $1
.This approach can even be used to recursively resolve relations in the GraphQL query. To prove this, let’s start with a schema that includes an additional relation:
and then consider the following GraphQL query:
The SQL query we want to generate looks like this:
This is the approach I took on the
forward-relations
branch. Don’t expect it to be performant, but I believe it’s the best we can do until PostGraphile supports joins.@benjie Thanks a lot for this great help!!!