Skip to content

Commit

Permalink
Fix PgPolymorphismPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Sep 25, 2024
1 parent b368cb0 commit 4a9ca7a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,48 +673,56 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
},
},
pgCodecRelation: {
inferred(behavior, entity, build) {
const {
input: {
pgRegistry: { pgRelations },
},
grafast: { arraysMatch },
} = build;
const { localCodec, remoteResource, isUnique, isReferencee } = entity;
const remoteCodec = remoteResource.codec;
inferred: {
provides: ["inferred"],
after: ["default", "PgRelationsPlugin"],
callback(behavior, entity, build) {
const {
input: {
pgRegistry: { pgRelations },
},
grafast: { arraysMatch },
} = build;
const { localCodec, remoteResource, isUnique, isReferencee } =
entity;
const remoteCodec = remoteResource.codec;

// Hide relation from a concrete type back to the abstract root table.
if (
isUnique &&
!isReferencee &&
remoteCodec.polymorphism?.mode === "relational"
) {
const localTypeName = build.inflection.tableType(localCodec);
const polymorphicTypeDefinitionEntry = Object.entries(
remoteCodec.polymorphism.types,
).find(([, val]) => val.name === localTypeName);
if (polymorphicTypeDefinitionEntry) {
const [, { relationName }] = polymorphicTypeDefinitionEntry;
const relation = pgRelations[remoteCodec.name]?.[relationName];
if (
arraysMatch(relation.remoteAttributes, entity.localAttributes)
) {
return [behavior, "-connection", "-list", "-single"];
// Hide relation from a concrete type back to the abstract root table.
if (
isUnique &&
!isReferencee &&
remoteCodec.polymorphism?.mode === "relational"
) {
const localTypeName = build.inflection.tableType(localCodec);
const polymorphicTypeDefinitionEntry = Object.entries(
remoteCodec.polymorphism.types,
).find(([, val]) => val.name === localTypeName);
if (polymorphicTypeDefinitionEntry) {
const [, { relationName }] = polymorphicTypeDefinitionEntry;
const relation = pgRelations[remoteCodec.name]?.[relationName];
if (
arraysMatch(relation.remoteAttributes, entity.localAttributes)
) {
return [behavior, "-connection", "-list", "-single"];
}
}
}
}

// Hide relation from abstract root table to related elements
if (isReferencee && localCodec.polymorphism?.mode === "relational") {
const relations = Object.values(localCodec.polymorphism.types).map(
(t) => pgRelations[localCodec.name]?.[t.relationName],
);
if (relations.includes(entity)) {
return [behavior, "-connection", "-list", "-single"];
// Hide relation from abstract root table to related elements
if (
isReferencee &&
localCodec.polymorphism?.mode === "relational"
) {
const relations = Object.values(
localCodec.polymorphism.types,
).map((t) => pgRelations[localCodec.name]?.[t.relationName]);
if (relations.includes(entity)) {
return [behavior, "-connection", "-list", "-single"];
}
}
}

return behavior;
return behavior;
},
},
},
pgCodecAttribute: {
Expand Down
2 changes: 1 addition & 1 deletion graphile-build/graphile-build/src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export class Behavior {
stack: [
...inferredBehavior.stack,
{
source: "__ApplyBehaviors__",
source: `__ApplyBehaviors_${entityType}__`,
prefix: "",
suffix: `-* ${inferredBehaviorWithPreferencesApplied}`,
},
Expand Down

0 comments on commit 4a9ca7a

Please sign in to comment.