Skip to content

Commit 0eb9ac4

Browse files
committed
Start addressing issues for V5 beta2
1 parent 7771714 commit 0eb9ac4

11 files changed

+253
-191
lines changed

src/AddConnectionFilterOperatorPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GraphQLInputType } from "graphql";
22
import { $$filters } from "./interfaces";
3-
import { makeApplyPlanFromOperatorSpec } from "./PgConnectionArgFilterOperatorsPlugin";
3+
import { makeApplyFromOperatorSpec } from "./PgConnectionArgFilterOperatorsPlugin";
44

55
const { version } = require("../package.json");
66

@@ -103,7 +103,7 @@ export const AddConnectionFilterOperatorPlugin: GraphileConfig.Plugin = {
103103
{
104104
description,
105105
type,
106-
applyPlan: makeApplyPlanFromOperatorSpec(
106+
apply: makeApplyFromOperatorSpec(
107107
build,
108108
Self.name,
109109
filterName,

src/PgConnectionArgFilterAttributesPlugin.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { PgConditionStep, PgCodecWithAttributes } from "@dataplan/pg";
1+
import type {
2+
PgCodecWithAttributes,
3+
PgConditionCapableParent,
4+
} from "@dataplan/pg";
5+
import { GraphQLInputObjectType } from "graphql";
26

37
const { version } = require("../package.json");
48

@@ -17,7 +21,7 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
1721
const {
1822
inflection,
1923
connectionFilterOperatorsDigest,
20-
dataplanPg: { PgConditionStep },
24+
dataplanPg: { PgCondition },
2125
EXPORTABLE,
2226
} = build;
2327
const {
@@ -47,7 +51,9 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
4751
if (!digest) {
4852
continue;
4953
}
50-
const OperatorsType = build.getTypeByName(digest.operatorsTypeName);
54+
const OperatorsType = build.getTypeByName(
55+
digest.operatorsTypeName
56+
) as GraphQLInputObjectType;
5157
if (!OperatorsType) {
5258
continue;
5359
}
@@ -66,51 +72,50 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
6672
() => ({
6773
description: `Filter by the object’s \`${fieldName}\` field.`,
6874
type: OperatorsType,
69-
applyPlan: EXPORTABLE(
75+
apply: EXPORTABLE(
7076
(
71-
PgConditionStep,
77+
PgCondition,
7278
colSpec,
7379
connectionFilterAllowEmptyObjectInput,
7480
connectionFilterAllowNullInput
7581
) =>
76-
function ($where: PgConditionStep<any>, fieldArgs: any) {
77-
const $raw = fieldArgs.getRaw();
78-
if ($raw.evalIs(undefined)) {
82+
function (
83+
queryBuilder: PgConditionCapableParent,
84+
value: unknown
85+
) {
86+
if (value === undefined) {
7987
return;
8088
}
81-
if (
82-
!connectionFilterAllowEmptyObjectInput &&
83-
"evalIsEmpty" in $raw &&
84-
$raw.evalIsEmpty()
85-
) {
89+
if (!connectionFilterAllowNullInput && value === null) {
8690
throw Object.assign(
8791
new Error(
88-
"Empty objects are forbidden in filter argument input."
92+
"Null literals are forbidden in filter argument input."
8993
),
9094
{
9195
//TODO: mark this error as safe
9296
}
9397
);
9498
}
9599
if (
96-
!connectionFilterAllowNullInput &&
97-
$raw.evalIs(null)
100+
!connectionFilterAllowEmptyObjectInput &&
101+
value != null &&
102+
Object.keys(value).length === 0
98103
) {
99104
throw Object.assign(
100105
new Error(
101-
"Null literals are forbidden in filter argument input."
106+
"Empty objects are forbidden in filter argument input."
102107
),
103108
{
104109
//TODO: mark this error as safe
105110
}
106111
);
107112
}
108-
const $col = new PgConditionStep($where);
109-
$col.extensions.pgFilterAttribute = colSpec;
110-
fieldArgs.apply($col);
113+
const condition = new PgCondition(queryBuilder);
114+
condition.extensions.pgFilterAttribute = colSpec;
115+
return condition;
111116
},
112117
[
113-
PgConditionStep,
118+
PgCondition,
114119
colSpec,
115120
connectionFilterAllowEmptyObjectInput,
116121
connectionFilterAllowNullInput,

src/PgConnectionArgFilterBackwardRelationsPlugin.ts

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type {
2-
PgConditionStep,
2+
PgCondition,
33
PgCodecRelation,
44
PgCodecWithAttributes,
55
PgRegistry,
66
PgResource,
7+
PgConditionCapableParent,
78
} from "@dataplan/pg";
89
import { makeAssertAllowed } from "./utils";
10+
import { GraphQLInputObjectType } from "graphql";
911

1012
const { version } = require("../package.json");
1113

@@ -319,7 +321,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
319321
inflection.filterType(foreignTableTypeName);
320322
const ForeignTableFilterType = build.getTypeByName(
321323
foreignTableFilterTypeName
322-
);
324+
) as GraphQLInputObjectType;
323325
if (!ForeignTableFilterType) continue;
324326

325327
if (typeof foreignTable.from === "function") {
@@ -338,8 +340,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
338340
source.codec,
339341
foreignTable
340342
);
341-
const FilterManyType =
342-
build.getTypeByName(filterManyTypeName);
343+
const FilterManyType = build.getTypeByName(
344+
filterManyTypeName
345+
) as GraphQLInputObjectType;
346+
if (!FilterManyType) {
347+
throw new Error(
348+
`Failed to retrieve type '${filterManyTypeName}'`
349+
);
350+
}
343351
// TODO: revisit using `_` prefixed inflector
344352
const fieldName = inflection._manyRelation({
345353
registry: source.registry,
@@ -360,7 +368,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
360368
() => ({
361369
description: `Filter by the object’s \`${fieldName}\` relation.`,
362370
type: FilterManyType,
363-
applyPlan: EXPORTABLE(
371+
apply: EXPORTABLE(
364372
(
365373
assertAllowed,
366374
foreignTable,
@@ -369,10 +377,10 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
369377
remoteAttributes
370378
) =>
371379
function (
372-
$where: PgConditionStep<any>,
373-
fieldArgs
380+
$where: PgCondition,
381+
value: object | null
374382
) {
375-
assertAllowed(fieldArgs, "object");
383+
assertAllowed(value, "object");
376384
// $where.alias represents source; we need a condition that references the relational target
377385
const $rel = $where.andPlan();
378386
$rel.extensions.pgFilterRelation = {
@@ -381,7 +389,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
381389
localAttributes,
382390
remoteAttributes,
383391
};
384-
fieldArgs.apply($rel);
392+
return $rel;
385393
},
386394
[
387395
assertAllowed,
@@ -417,7 +425,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
417425
// and in PgConnectionArgFilterForwardRelationsPlugin
418426
// are very very similar. We should extract them to a
419427
// helper function.
420-
applyPlan: EXPORTABLE(
428+
apply: EXPORTABLE(
421429
(
422430
assertAllowed,
423431
foreignTable,
@@ -427,14 +435,15 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
427435
sql
428436
) =>
429437
function (
430-
$where: PgConditionStep<any>,
431-
fieldArgs
438+
$where: PgCondition,
439+
value: boolean | null
432440
) {
433-
assertAllowed(fieldArgs, "scalar");
441+
assertAllowed(value, "scalar");
442+
if (value == null) return;
434443
const $subQuery = $where.existsPlan({
435444
tableExpression: foreignTableExpression,
436445
alias: foreignTable.name,
437-
$equals: fieldArgs.get(),
446+
equals: value as boolean,
438447
});
439448
localAttributes.forEach((localAttribute, i) => {
440449
const remoteAttribute = remoteAttributes[i];
@@ -483,7 +492,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
483492
() => ({
484493
description: `Filter by the object’s \`${fieldName}\` relation.`,
485494
type: ForeignTableFilterType,
486-
applyPlan: EXPORTABLE(
495+
apply: EXPORTABLE(
487496
(
488497
assertAllowed,
489498
foreignTable,
@@ -492,8 +501,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
492501
remoteAttributes,
493502
sql
494503
) =>
495-
function ($where: PgConditionStep<any>, fieldArgs) {
496-
assertAllowed(fieldArgs, "object");
504+
function (
505+
$where: PgCondition,
506+
value: object | null
507+
) {
508+
assertAllowed(value, "object");
497509
const $subQuery = $where.existsPlan({
498510
tableExpression: foreignTableExpression,
499511
alias: foreignTable.name,
@@ -508,7 +520,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
508520
)}`
509521
);
510522
});
511-
fieldArgs.apply($subQuery);
523+
return $subQuery;
512524
},
513525
[
514526
assertAllowed,
@@ -541,7 +553,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
541553
() => ({
542554
description: `A related \`${fieldName}\` exists.`,
543555
type: GraphQLBoolean,
544-
applyPlan: EXPORTABLE(
556+
apply: EXPORTABLE(
545557
(
546558
assertAllowed,
547559
foreignTable,
@@ -551,14 +563,15 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
551563
sql
552564
) =>
553565
function (
554-
$where: PgConditionStep<any>,
555-
fieldArgs
566+
$where: PgCondition,
567+
value: boolean | null
556568
) {
557-
assertAllowed(fieldArgs, "scalar");
569+
assertAllowed(value, "scalar");
570+
if (value == null) return;
558571
const $subQuery = $where.existsPlan({
559572
tableExpression: foreignTableExpression,
560573
alias: foreignTable.name,
561-
$equals: fieldArgs.get(),
574+
equals: value,
562575
});
563576
localAttributes.forEach((localAttribute, i) => {
564577
const remoteAttribute = remoteAttributes[i];
@@ -596,7 +609,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
596609
);
597610
const foreignTableFilterTypeName =
598611
inflection.filterType(foreignTableTypeName);
599-
const FilterType = build.getTypeByName(foreignTableFilterTypeName);
612+
const FilterType = build.getTypeByName(
613+
foreignTableFilterTypeName
614+
) as GraphQLInputObjectType;
615+
if (!FilterType) {
616+
throw new Error(
617+
`Failed to load type ${foreignTableFilterTypeName}`
618+
);
619+
}
600620

601621
const manyFields = {
602622
every: fieldWithHooks(
@@ -607,10 +627,14 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
607627
() => ({
608628
description: `Every related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
609629
type: FilterType,
610-
applyPlan: EXPORTABLE(
630+
apply: EXPORTABLE(
611631
(assertAllowed, sql) =>
612-
function ($where: PgConditionStep<any>, fieldArgs) {
613-
assertAllowed(fieldArgs, "object");
632+
function (
633+
$where: PgCondition<any>,
634+
value: object | null
635+
) {
636+
assertAllowed(value, "object");
637+
if (value == null) return;
614638
if (!$where.extensions.pgFilterRelation) {
615639
throw new Error(
616640
`Invalid use of filter, 'pgFilterRelation' expected`
@@ -636,7 +660,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
636660
)}`
637661
);
638662
});
639-
fieldArgs.apply($subQuery.notPlan().andPlan());
663+
return $subQuery.notPlan().andPlan();
640664
},
641665
[assertAllowed, sql]
642666
),
@@ -650,10 +674,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
650674
() => ({
651675
description: `Some related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
652676
type: FilterType,
653-
applyPlan: EXPORTABLE(
677+
apply: EXPORTABLE(
654678
(assertAllowed, sql) =>
655-
function ($where: PgConditionStep<any>, fieldArgs) {
656-
assertAllowed(fieldArgs, "object");
679+
function ($where: PgCondition, value: object | null) {
680+
assertAllowed(value, "object");
681+
if (value == null) return;
657682
if (!$where.extensions.pgFilterRelation) {
658683
throw new Error(
659684
`Invalid use of filter, 'pgFilterRelation' expected`
@@ -679,7 +704,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
679704
)}`
680705
);
681706
});
682-
fieldArgs.apply($subQuery);
707+
return $subQuery;
683708
},
684709
[assertAllowed, sql]
685710
),
@@ -693,10 +718,11 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
693718
() => ({
694719
description: `No related \`${foreignTableTypeName}\` matches the filter criteria. All fields are combined with a logical ‘and.’`,
695720
type: FilterType,
696-
applyPlan: EXPORTABLE(
721+
apply: EXPORTABLE(
697722
(assertAllowed, sql) =>
698-
function ($where: PgConditionStep<any>, fieldArgs) {
699-
assertAllowed(fieldArgs, "object");
723+
function ($where: PgCondition, value: object | null) {
724+
assertAllowed(value, "object");
725+
if (value == null) return;
700726
if (!$where.extensions.pgFilterRelation) {
701727
throw new Error(
702728
`Invalid use of filter, 'pgFilterRelation' expected`
@@ -722,7 +748,7 @@ export const PgConnectionArgFilterBackwardRelationsPlugin: GraphileConfig.Plugin
722748
)}`
723749
);
724750
});
725-
fieldArgs.apply($subQuery);
751+
return $subQuery;
726752
},
727753
[assertAllowed, sql]
728754
),

src/PgConnectionArgFilterCompositeTypeAttributesPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PgCodecAttributes, PgCodecWithAttributes } from "@dataplan/pg";
2+
import { GraphQLInputObjectType } from "graphql";
23

34
const { version } = require("../package.json");
45

@@ -72,7 +73,9 @@ export const PgConnectionArgFilterCompositeTypeAttributesPlugin: GraphileConfig.
7273
}
7374

7475
const filterTypeName = inflection.filterType(nodeTypeName);
75-
const CompositeFilterType = build.getTypeByName(filterTypeName);
76+
const CompositeFilterType = build.getTypeByName(
77+
filterTypeName
78+
) as GraphQLInputObjectType;
7679
if (!CompositeFilterType) {
7780
continue;
7881
}

0 commit comments

Comments
 (0)