Skip to content

Commit 241c8b9

Browse files
authored
Smaller exports (#221)
2 parents 621e70a + 2cc716b commit 241c8b9

8 files changed

+1591
-377
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@babel/plugin-transform-runtime": "^7.28.5",
3737
"@babel/preset-env": "^7.28.5",
3838
"@babel/preset-typescript": "^7.28.5",
39-
"@dataplan/pg": "^1.0.0-rc.1",
39+
"@dataplan/pg": "^1.0.0-rc.6",
4040
"@eslint/compat": "^2.0.0",
4141
"@eslint/eslintrc": "^3.3.1",
4242
"@eslint/js": "^9.39.1",
@@ -54,19 +54,19 @@
5454
"eslint-plugin-simple-import-sort": "^12.1.1",
5555
"eslint-plugin-tsdoc": "^0.5.0",
5656
"eslint_d": "^14.3.0",
57-
"grafast": "^1.0.0-rc.1",
57+
"grafast": "^1.0.0-rc.8",
5858
"grafserv": "^1.0.0-rc.1",
59-
"graphile": "^5.0.0-rc.1",
60-
"graphile-build": "^5.0.0-rc.1",
61-
"graphile-build-pg": "^5.0.0-rc.1",
62-
"graphile-export": "^1.0.0-rc.1",
59+
"graphile": "^5.0.0-rc.5",
60+
"graphile-build": "^5.0.0-rc.5",
61+
"graphile-build-pg": "^5.0.0-rc.6",
62+
"graphile-export": "^1.0.0-rc.5",
6363
"graphql": "^16.9.0",
6464
"jest": "29.7.0",
6565
"module-from-string": "^3.3.0",
6666
"pg": "^8.14.1",
67-
"pg-introspection": "^1.0.0-rc.1",
68-
"pg-sql2": "^5.0.0-rc.1",
69-
"postgraphile": "^5.0.0-rc.1",
67+
"pg-introspection": "^1.0.0-rc.4",
68+
"pg-sql2": "^5.0.0-rc.4",
69+
"postgraphile": "^5.0.0-rc.8",
7070
"prettier": "^3.6.2",
7171
"ts-jest": "29.1.0",
7272
"typescript": "^5.0.4",

src/EXPORTABLE.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function EXPORTABLE<T, TScope extends readonly any[]>(
2+
factory: (...args: TScope) => T,
3+
args: readonly [...TScope],
4+
nameHint?: string
5+
): T {
6+
const fn: T = factory(...args);
7+
if (
8+
((typeof fn === "object" && fn !== null) || typeof fn === "function") &&
9+
!("$exporter$factory" in fn)
10+
) {
11+
Object.defineProperties(fn, {
12+
$exporter$args: { value: args },
13+
$exporter$factory: { value: factory },
14+
$exporter$name: { writable: true, value: nameHint },
15+
});
16+
}
17+
return fn;
18+
}

src/PgConnectionArgFilterAttributesPlugin.ts

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
11
import type {
2+
PgCodecAttribute,
23
PgCodecWithAttributes,
34
PgConditionCapableParent,
45
} from "@dataplan/pg";
56
import type { GraphQLInputObjectType } from "graphql";
67
import { isEmpty } from "./utils";
8+
import { EXPORTABLE } from "./EXPORTABLE";
79

810
import { version } from "./version";
911

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+
1055
export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
1156
name: "PgConnectionArgFilterAttributesPlugin",
1257
version,
@@ -47,7 +92,6 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
4792
continue;
4893
}
4994
const fieldName = inflection.attribute({ codec, attributeName });
50-
const colSpec = { fieldName, attributeName, attribute };
5195
const digest = connectionFilterOperatorsDigest(attribute.codec);
5296
if (!digest) {
5397
continue;
@@ -76,51 +120,36 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
76120
apply: EXPORTABLE(
77121
(
78122
PgCondition,
79-
colSpec,
123+
attribute,
124+
attributeName,
80125
connectionFilterAllowEmptyObjectInput,
81126
connectionFilterAllowNullInput,
82-
isEmpty
127+
fieldName,
128+
pgConnectionFilterApplyAttribute
83129
) =>
84130
function (
85131
queryBuilder: PgConditionCapableParent,
86132
value: unknown
87133
) {
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+
);
117144
},
118145
[
119146
PgCondition,
120-
colSpec,
147+
attribute,
148+
attributeName,
121149
connectionFilterAllowEmptyObjectInput,
122150
connectionFilterAllowNullInput,
123-
isEmpty,
151+
fieldName,
152+
pgConnectionFilterApplyAttribute,
124153
]
125154
),
126155
})

src/PgConnectionArgFilterBackwardRelationsPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
367367
() => ({
368368
description: `Filter by the object’s \`${fieldName}\` relation.`,
369369
type: FilterManyType,
370+
// $where.alias represents source; we need a condition that references the relational target
370371
apply: EXPORTABLE(
371372
(
372373
assertAllowed,
@@ -380,7 +381,6 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
380381
value: object | null
381382
) {
382383
assertAllowed(value, "object");
383-
// $where.alias represents source; we need a condition that references the relational target
384384
const $rel = $where.andPlan();
385385
$rel.extensions.pgFilterRelation = {
386386
tableExpression: foreignTableExpression,

src/PgConnectionArgFilterComputedAttributesPlugin.ts

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { PgCondition } from "@dataplan/pg";
1+
import type { PgCodec, PgCondition, PgResource } from "@dataplan/pg";
22
import {
33
getComputedAttributeResources,
44
isComputedScalarAttributeResource,
55
} from "./utils";
66
import type { GraphQLInputObjectType } from "graphql";
7+
import { EXPORTABLE } from "./EXPORTABLE";
78

89
import { version } from "./version";
910

@@ -15,6 +16,36 @@ declare global {
1516
}
1617
}
1718

19+
const pgConnectionFilterApplyComputedAttribute = EXPORTABLE(
20+
() =>
21+
(
22+
PgCondition: GraphileBuild.Build["dataplanPg"]["PgCondition"],
23+
computedAttributeResource: PgResource,
24+
fieldName: string,
25+
functionResultCodec: PgCodec,
26+
$where: PgCondition,
27+
value: object | null
28+
) => {
29+
if (typeof computedAttributeResource.from !== "function") {
30+
throw new Error(`Unexpected...`);
31+
}
32+
// TODO: assertAllowed?
33+
if (value == null) return;
34+
const expression = computedAttributeResource.from({
35+
placeholder: $where.alias,
36+
});
37+
const $col = new PgCondition($where);
38+
$col.extensions.pgFilterAttribute = {
39+
fieldName,
40+
codec: functionResultCodec,
41+
expression,
42+
};
43+
return $col;
44+
},
45+
[],
46+
"pgConnectionFilterApplyComputedAttribute"
47+
);
48+
1849
export const PgConnectionArgFilterComputedAttributesPlugin: GraphileConfig.Plugin =
1950
{
2051
name: "PgConnectionArgFilterComputedAttributesPlugin",
@@ -149,32 +180,25 @@ export const PgConnectionArgFilterComputedAttributesPlugin: GraphileConfig.Plugi
149180
PgCondition,
150181
computedAttributeResource,
151182
fieldName,
152-
functionResultCodec
183+
functionResultCodec,
184+
pgConnectionFilterApplyComputedAttribute
153185
) =>
154186
function ($where: PgCondition, value: object | null) {
155-
if (
156-
typeof computedAttributeResource.from !== "function"
157-
) {
158-
throw new Error(`Unexpected...`);
159-
}
160-
// TODO: assertAllowed?
161-
if (value == null) return;
162-
const expression = computedAttributeResource.from({
163-
placeholder: $where.alias,
164-
});
165-
const $col = new PgCondition($where);
166-
$col.extensions.pgFilterAttribute = {
187+
return pgConnectionFilterApplyComputedAttribute(
188+
PgCondition,
189+
computedAttributeResource,
167190
fieldName,
168-
codec: functionResultCodec,
169-
expression,
170-
};
171-
return $col;
191+
functionResultCodec,
192+
$where,
193+
value
194+
);
172195
},
173196
[
174197
PgCondition,
175198
computedAttributeResource,
176199
fieldName,
177200
functionResultCodec,
201+
pgConnectionFilterApplyComputedAttribute,
178202
]
179203
),
180204
}

0 commit comments

Comments
 (0)