@haathie/postgraphile-targeted-conditions 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -15,6 +15,10 @@ export const TargetedConditionsPlugin = {
15
15
  description: 'Allow filtering using by fields on this relation',
16
16
  entities: ['pgCodecRef', 'pgRefDefinition']
17
17
  },
18
+ 'filterRequired': {
19
+ description: 'Make the filter condition required',
20
+ entities: ['pgCodecAttribute'],
21
+ },
18
22
  ...Object.entries(FILTER_TYPES_MAP).reduce((acc, [filterType, { description }]) => {
19
23
  const behaviourName = `filterType:${filterType}`;
20
24
  acc[behaviourName] = {
@@ -22,9 +22,13 @@ export const fields = (fieldMap, build, ctx) => {
22
22
  throw new Error(`Condition type ${typeName} for attribute "${attrName}" `
23
23
  + `not found in codec "${pgCodec.name}".`);
24
24
  }
25
+ const isRequired = behavior.pgCodecAttributeMatches([pgCodec, attrName], 'filterRequired');
26
+ const fieldType = isRequired
27
+ ? new build.graphql.GraphQLNonNull(type)
28
+ : type;
25
29
  const fieldName = inflection
26
30
  .attribute({ attributeName: attrName, codec: pgCodec });
27
- fieldMap[fieldName] = fieldWithHooks({ fieldName, isConditionContainer: true }, () => ({ extensions: { grafast: { apply: passThroughApply } }, type }));
31
+ fieldMap[fieldName] = fieldWithHooks({ fieldName, isConditionContainer: true }, () => ({ extensions: { grafast: { apply: passThroughApply } }, type: fieldType }));
28
32
  }
29
33
  // add queries via refs
30
34
  for (const [refName, { paths }] of Object.entries(pgCodec.refs || {})) {
package/lib/types.d.ts CHANGED
@@ -43,6 +43,7 @@ export type FilterMethodConfig = {
43
43
  };
44
44
  interface FilterBehaviours extends Record<`filterType:${FilterType}`, true>, Record<`filterMethod:${FilterMethod}`, true> {
45
45
  'filterable': true;
46
+ 'filterRequired': true;
46
47
  }
47
48
  declare global {
48
49
  namespace GraphileBuild {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haathie/postgraphile-targeted-conditions",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "files": ["lib"],
6
6
  "main": "lib/index.js",
@@ -16,7 +16,7 @@
16
16
  "build": "tsc -p tsconfig.json"
17
17
  },
18
18
  "dependencies": {
19
- "@haathie/postgraphile-common-utils": "*"
19
+ "@haathie/postgraphile-common-utils": "^0.1.2"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "postgraphile": "*"
package/readme.md CHANGED
@@ -59,6 +59,18 @@ input ContactNameCondition @oneOf {
59
59
 
60
60
  This allows for adding/removing filter types without breaking existing queries. For example, if you add a new filter type for "equals in" to the "name" column, and the above query will still work without any changes.
61
61
 
62
+ ## Making Conditions Required
63
+
64
+ By default, all conditions are optional. However, you can make a condition required by adding a `filterRequired` behaviour.
65
+
66
+ ``` sql
67
+ COMMENT ON COLUMN app.contacts.name is $$
68
+ @behaviour filterType:icontains filterType:eq filterRequired
69
+ $$;
70
+ ```
71
+
72
+ This will make the `condition` argument for the `name` field required in the GraphQL schema, and it will also make sure that at least one of the filter types is provided in the query.
73
+
62
74
  ## Relational Conditions
63
75
 
64
76
  Let's say we have a `contacts` table and a `tags` table, with each contact having multiple tags. We can add a filter to the `contacts` table to filter by tags. We'll create a ref and add `filterable` behaviour to it.
@@ -207,4 +219,4 @@ See how [paradedb method](src/filter-implementations/paradedb.ts) is implemented
207
219
 
208
220
  - Adding relational conditions can lead to performance issues, especially if the related table has a large number of rows. Use with caution and please ensure you have the necessary indices in place.
209
221
  - Apart from relations, adding arbitrary conditions on fields for vibes only is not a good idea. It can cause unexpected performance issues, and it is recommended to only add conditions that are necessary for your application. The plugin is meant for you to quickly add targeted conditions to your queries, and only the ones you want -- so we spend more time writing mission-critical code rather than boilerplate SQL or GraphQL plans.
210
- - This plugin does not work with Postgres compound types at the moment. Only the `eq` and `eqIn` filter types are supported for compound types.
222
+ - This plugin does not work with Postgres compound types at the moment. Only the `eq` and `eqIn` filter types are supported for compound types.