Closed t829702 closed 3 years ago
is it even possible? with no custom coding or very little wrapping?
Could you try this, based on your suggestion?
allEntries(filter({
or: [
{ isNull: $countries },
{ country: { in: $countries } }
]
})) {
(disclaimer, I don't currently use this library)
You can accomplish this using the connectionFilterAllowNullInput
option.
CREATE SCHEMA app_public;
CREATE TABLE app_public.entries (
id serial PRIMARY KEY,
country text
);
INSERT INTO app_public.entries VALUES
(1, 'United Kingdom'),
(2, 'United States');
const http = require("http");
const { postgraphile } = require("postgraphile");
const ConnectionFilterPlugin = require("postgraphile-plugin-connection-filter");
http
.createServer(
postgraphile(
"postgres:///filter_issue_147",
"app_public",
{
appendPlugins: [ConnectionFilterPlugin],
graphiql: true,
enhanceGraphiql: true,
graphileBuildOptions: {
connectionFilterAllowNullInput: true
}
}
)
)
.listen(process.env.PORT || 3000);
query Entries($countries: [String!] = null) {
allEntries(filter: { country: { in: $countries } }) {
nodes {
id
country
}
totalCount
}
}
With that query, if you supply countries
in the query variables, it will add country IN (...)
to the SQL WHERE clause. If you don't supply countries
, it won't alter the SQL WHERE clause at all, thus returning all countries.
Beautiful! @mattbretl probably okay to close this issue then? Though it might be helpful to add this example to the docs for that option…
Yep, @t829702 feel free to reach out if this doesn't solve your issue.
PRs are always welcome on the docs. :slightly_smiling_face:
i have a simple table with field
country TEXT
in postgres, want to filter based on user input can sayWHERE country IN (a-list-of-user-selected-countries)
in GraphQL it's likethis indeed works when providing
variables={ "countries": ["United States", "United Kingdom", ...] }
, but then I want to use this same query when user did not select any countries from UI, I don't want to pass all possible values (there are 180+ country names in the world) into variables, to save from providing a too big list; but instead I want to omit countries from variables to mean don't filter oncountry
field, wonder is it possible for GraphQL to check and putWHERE country in (?)
only when got a non-null$countries
from GraphQL variables?want it to convert to some SQL query like:
Another question is when I do provide
variables={ "countries": ["United States", "United Kingdom", ...] }
, how can I also have a boolean switch in the UI to re-use same query to also select those rows with unknown country (akacountry IS NULL
)?