Skip to content

Commit 9aa5565

Browse files
committed
fix(utils): to match early enough, we need build to be passed
1 parent 649b6a0 commit 9aa5565

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

packages/graphile-utils/src/introspectionHelpers.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PgProc,
88
} from "graphile-build-pg";
99
import parseIdentifierParts from "./parseIdentifierParts";
10+
import { Build } from "graphile-build";
1011

1112
export function isAttribute(obj: PgEntity): obj is PgAttribute {
1213
return obj.kind === PgEntityKind.ATTRIBUTE;
@@ -26,7 +27,8 @@ export function isProcedure(obj: PgEntity): obj is PgProc {
2627

2728
export function entityIsIdentifiedBy(
2829
obj: PgEntity,
29-
identifier: string
30+
identifier: string,
31+
build: Build
3032
): boolean {
3133
const parts = parseIdentifierParts(identifier);
3234
if (parts.length === 1) {
@@ -49,10 +51,16 @@ export function entityIsIdentifiedBy(
4951
const [grandparentName, parentName, expectedName] = parts;
5052
if (isAttribute(obj) || isConstraint(obj)) {
5153
// Parent is a table, grandparent is a schema
54+
const klass:
55+
| PgClass
56+
| undefined = build.pgIntrospectionResultsByKind.class.find(
57+
(kls: PgClass) => kls.id === obj.classId
58+
);
5259
return (
5360
obj.name === expectedName &&
54-
obj.class.name === parentName &&
55-
obj.class.namespaceName === grandparentName
61+
!!klass &&
62+
klass.name === parentName &&
63+
klass.namespaceName === grandparentName
5664
);
5765
} else {
5866
// Parent is a schema; grandparent doesn't make sense

packages/graphile-utils/src/makePgSmartTagsPlugin.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from "graphile-build";
1+
import { Plugin, Build } from "graphile-build";
22
import {
33
PgEntityKind,
44
PgEntity,
@@ -7,7 +7,7 @@ import {
77
import { inspect } from "util";
88
import { entityIsIdentifiedBy } from "./introspectionHelpers";
99

10-
export type PgSmartTagFilterFunction<T> = (input: T) => boolean;
10+
export type PgSmartTagFilterFunction<T> = (input: T, build: Build) => boolean;
1111

1212
export type PgSmartTagTags = {
1313
[tagName: string]: null | true | string | string[];
@@ -57,17 +57,17 @@ function compileRule<T extends PgEntity>(
5757
);
5858
}
5959

60-
const match: PgSmartTagFilterFunction<T> = obj => {
60+
const match: PgSmartTagFilterFunction<T> = (obj, build) => {
6161
if (obj.kind !== kind) {
6262
return false;
6363
}
6464

6565
if (typeof incomingMatch === "function") {
6666
// User supplied a match function; delegate to that:
67-
return incomingMatch(obj);
67+
return incomingMatch(obj, build);
6868
} else if (typeof incomingMatch === "string") {
6969
// It's a fully-qualified case-sensitive name of the thing.
70-
return entityIsIdentifiedBy(obj, incomingMatch);
70+
return entityIsIdentifiedBy(obj, incomingMatch, build);
7171
} else {
7272
throw new Error(
7373
"makePgSmartTagsPlugin rule 'match' is neither a string nor a function"
@@ -140,7 +140,7 @@ export function makePgSmartTagsPlugin(
140140

141141
let hits = 0;
142142
relevantIntrospectionResults.forEach(entity => {
143-
if (!rule.match(entity)) {
143+
if (!rule.match(entity, build)) {
144144
return;
145145
}
146146
hits++;

0 commit comments

Comments
 (0)