|
1 | 1 | import type { |
| 2 | + PgCodecAttribute, |
2 | 3 | PgCodecWithAttributes, |
3 | 4 | PgConditionCapableParent, |
4 | 5 | } from "@dataplan/pg"; |
5 | 6 | import type { GraphQLInputObjectType } from "graphql"; |
6 | 7 | import { isEmpty } from "./utils"; |
| 8 | +import { EXPORTABLE } from "./EXPORTABLE"; |
7 | 9 |
|
8 | 10 | import { version } from "./version"; |
9 | 11 |
|
| 12 | +const pgConnectionFilterApplyAttribute = EXPORTABLE( |
| 13 | + (isEmpty) => |
| 14 | + ( |
| 15 | + PgCondition: GraphileBuild.Build["dataplanPg"]["PgCondition"], |
| 16 | + fieldName: string, |
| 17 | + attributeName: string, |
| 18 | + attribute: PgCodecAttribute, |
| 19 | + connectionFilterAllowEmptyObjectInput: boolean | undefined, |
| 20 | + connectionFilterAllowNullInput: boolean | undefined, |
| 21 | + queryBuilder: PgConditionCapableParent, |
| 22 | + value: unknown |
| 23 | + ) => { |
| 24 | + if (value === undefined) { |
| 25 | + return; |
| 26 | + } |
| 27 | + if (!connectionFilterAllowEmptyObjectInput && isEmpty(value)) { |
| 28 | + throw Object.assign( |
| 29 | + new Error("Empty objects are forbidden in filter argument input."), |
| 30 | + { |
| 31 | + //TODO: mark this error as safe |
| 32 | + } |
| 33 | + ); |
| 34 | + } |
| 35 | + if (!connectionFilterAllowNullInput && value === null) { |
| 36 | + throw Object.assign( |
| 37 | + new Error("Null literals are forbidden in filter argument input."), |
| 38 | + { |
| 39 | + //TODO: mark this error as safe |
| 40 | + } |
| 41 | + ); |
| 42 | + } |
| 43 | + const condition = new PgCondition(queryBuilder); |
| 44 | + condition.extensions.pgFilterAttribute = { |
| 45 | + fieldName, |
| 46 | + attributeName, |
| 47 | + attribute, |
| 48 | + }; |
| 49 | + return condition; |
| 50 | + }, |
| 51 | + [isEmpty], |
| 52 | + "pgConnectionFilterApplyAttribute" |
| 53 | +); |
| 54 | + |
10 | 55 | export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = { |
11 | 56 | name: "PgConnectionArgFilterAttributesPlugin", |
12 | 57 | version, |
@@ -47,7 +92,6 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = { |
47 | 92 | continue; |
48 | 93 | } |
49 | 94 | const fieldName = inflection.attribute({ codec, attributeName }); |
50 | | - const colSpec = { fieldName, attributeName, attribute }; |
51 | 95 | const digest = connectionFilterOperatorsDigest(attribute.codec); |
52 | 96 | if (!digest) { |
53 | 97 | continue; |
@@ -76,51 +120,36 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = { |
76 | 120 | apply: EXPORTABLE( |
77 | 121 | ( |
78 | 122 | PgCondition, |
79 | | - colSpec, |
| 123 | + attribute, |
| 124 | + attributeName, |
80 | 125 | connectionFilterAllowEmptyObjectInput, |
81 | 126 | connectionFilterAllowNullInput, |
82 | | - isEmpty |
| 127 | + fieldName, |
| 128 | + pgConnectionFilterApplyAttribute |
83 | 129 | ) => |
84 | 130 | function ( |
85 | 131 | queryBuilder: PgConditionCapableParent, |
86 | 132 | value: unknown |
87 | 133 | ) { |
88 | | - if (value === undefined) { |
89 | | - return; |
90 | | - } |
91 | | - if ( |
92 | | - !connectionFilterAllowEmptyObjectInput && |
93 | | - isEmpty(value) |
94 | | - ) { |
95 | | - throw Object.assign( |
96 | | - new Error( |
97 | | - "Empty objects are forbidden in filter argument input." |
98 | | - ), |
99 | | - { |
100 | | - //TODO: mark this error as safe |
101 | | - } |
102 | | - ); |
103 | | - } |
104 | | - if (!connectionFilterAllowNullInput && value === null) { |
105 | | - throw Object.assign( |
106 | | - new Error( |
107 | | - "Null literals are forbidden in filter argument input." |
108 | | - ), |
109 | | - { |
110 | | - //TODO: mark this error as safe |
111 | | - } |
112 | | - ); |
113 | | - } |
114 | | - const condition = new PgCondition(queryBuilder); |
115 | | - condition.extensions.pgFilterAttribute = colSpec; |
116 | | - return condition; |
| 134 | + return pgConnectionFilterApplyAttribute( |
| 135 | + PgCondition, |
| 136 | + fieldName, |
| 137 | + attributeName, |
| 138 | + attribute, |
| 139 | + connectionFilterAllowEmptyObjectInput, |
| 140 | + connectionFilterAllowNullInput, |
| 141 | + queryBuilder, |
| 142 | + value |
| 143 | + ); |
117 | 144 | }, |
118 | 145 | [ |
119 | 146 | PgCondition, |
120 | | - colSpec, |
| 147 | + attribute, |
| 148 | + attributeName, |
121 | 149 | connectionFilterAllowEmptyObjectInput, |
122 | 150 | connectionFilterAllowNullInput, |
123 | | - isEmpty, |
| 151 | + fieldName, |
| 152 | + pgConnectionFilterApplyAttribute, |
124 | 153 | ] |
125 | 154 | ), |
126 | 155 | }) |
|
0 commit comments