From 4a9ca7a9fabf27da391c97259d3bb4a6f152fecb Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 25 Sep 2024 16:39:28 +0100 Subject: [PATCH] Fix PgPolymorphismPlugin --- .../src/plugins/PgPolymorphismPlugin.ts | 80 ++++++++++--------- graphile-build/graphile-build/src/behavior.ts | 2 +- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts index a65967c36..1b19d7277 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgPolymorphismPlugin.ts @@ -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: { diff --git a/graphile-build/graphile-build/src/behavior.ts b/graphile-build/graphile-build/src/behavior.ts index adfd23a60..ac19aa9d7 100644 --- a/graphile-build/graphile-build/src/behavior.ts +++ b/graphile-build/graphile-build/src/behavior.ts @@ -509,7 +509,7 @@ export class Behavior { stack: [ ...inferredBehavior.stack, { - source: "__ApplyBehaviors__", + source: `__ApplyBehaviors_${entityType}__`, prefix: "", suffix: `-* ${inferredBehaviorWithPreferencesApplied}`, },