Custom filters for postgraphile
I'm submitting a ...
- [ ] bug report
- [ x] feature request
- [ ] question
PostGraphile version: 4.0.0-rc.5
This is a feature request, but I actually almost have some thing ready, it is more like a discussion.
First of all, thank you guys very much for developing this awesome library. I have been using it for several months now and it's going great!
In my project, a lot of times I will need to query for a Connection with more restricted parameters, (not just only the fields on that table, but more nested with join etc). For example, I have user table and a status table, which stores the start, end time of this user's status.
User
name: String
Status
id: Int,
user_id: Int,
start: Date
end: Date
status: String
Then if I want to know the users who are active a year ago, in postGraphile I'll have to do it in two steps, first find the status that are active at that time, then from the user_id, find Users. You can imagine with more associations, it'll be more complicated.
But these situations are not a lot, just few queries that are hard to do. So I think I could add a customFilter object in the ConnectionArgs, then add some string and write a custom query to filter the connection.
Example:
allUsers(customFilter:{activeOn:'2017-01-01'}){
}
This can be done by hook to init and create this InputType.
Then in the hook on 'GraphQLObjectType:fields:field:args', I could use the addArgsGenerator
const {pgSql} = build;
addArgDataGenerator(({ customFilter }) => {
const { activeOn } = (customFilter || {});
return {
pgQuery: (queryBuilder) => {
queryBuilder.where('users.id in (select user_id from status where status.status = 'active' and ${activeOn} between (status.start, status.end))')
},
};
});
}
The query here is just to demonstrate the purpose, of course this doesn't work, I'll need to use pgSql2 to convert this into
queryBuilder.getTableAlias().pgSql.identifier('id) in (pgSql.query(...))
But this was the idea.
For people who knows what they are doing, (sanitize query and etc), this could be a great help as a special filter. I actually have a plugin written with this method, and I am working on polishing it. I would publish it unless I am doing something totally wrong. Any feedback would be appreciated.
Looks like you're on the right track as far as the plugin goes. :+1: If you get stuck, this could probably serve as a template: https://github.com/graphile-contrib/postgraphile-plugin-connection-filter
You could also solve this with a custom query procedure:
create function active_users(active_on timestamptz) returns setof user as $$
select user
from user
inner join status ...
where
...
$$ language sql stable;
PostGraphile would expose that as an activeUsers Connection type.
Also.. the forward-relations branch of postgraphile-plugin-connection-filter enables filtering on forward relation fields. (For more detail, see: https://github.com/graphile-contrib/postgraphile-plugin-connection-filter/issues/50 .) You would need to move your one-to-one relation field to the user table, as backward relation support is still a work-in-progress. I don't plan to merge and publish the relation filtering support until we add some helpers to the core plugins; it will be some time after v4.0.0. But it's coming. :smile:
@mattbretl Thank you! Yeah reading through the code from plugin-connection-filter got me more familiar with how to write it. That's actually where my motivation came from. I thought about creating the procedure, but that's not exposed on all connections. for example,
accountById(id:1){
// get the active users
}
For the procedures, I would have to also create function account_active_users() so that it add that field in account, which is more redundant. That's why I went to this direction. Also exposing the queryBuilder object actually allows the user to do a lot more than just custom filtering. (I am not sure this was intended I just found out about it). For example, you can do custom sorting too!
addArgDataGenerator(({ customFilter }) => {
const { sortByStatusStart } = (customFilter || {});
if(sortByStatusStart)
return {
pgQuery: (queryBuilder) => {
queryBuilder.sortBy('select start from status where status.user_id = ${queryBuilder.getTableAlias()}.id')
},
};
});
}
This will sort the User by the status start time, etc. I would think if we can, still use the plugin-connection-filter because it's more strict and more well defined. But this custom filter way is like a last resort for very special situations.
I think everything you need is already exposed - queryBuilder is documented a tiny amount here and you've shown how to use the plugin interface.
I think this is more of a discussion so I'm going to close it (I'm not planning to add an interface like this to core - I prefer doing this kind of thing via plugins) but if there is something missing from the plugin interface that you require please do re-open with a comment explaining what specific feature(s) you require to implement your plugin. Please feel free to keep discussing below, I'm just trying to keep open issues to actual bugs or definitely planned features.
@mattbretl, @benjie Hi, I have written the plugin and documentation of it. I made a PR for the plugin page.
https://github.com/graphile/graphile.github.io/pull/74
Let me know if there's anything unclear.
Thanks