Skip to content

Commit

Permalink
Improvements around @interface mode:relational (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Jul 31, 2023
2 parents 3436dd1 + 1a90640 commit 47b2360
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 365 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-radios-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"graphile-build-pg": patch
"postgraphile": patch
---

When using `@interface mode:relational`, don't add pointless relationships from
concrete type back to abstract or from abstract to related concrete types.
6 changes: 6 additions & 0 deletions .changeset/light-knives-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-build-pg": patch
"postgraphile": patch
---

Fix spurious error relating to connection for `@interface type:relational`
5 changes: 5 additions & 0 deletions .changeset/neat-zebras-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphile": patch
---

Fix bug in `graphile behavior debug` command relating to `pgCodecRelation`
9 changes: 9 additions & 0 deletions .changeset/proud-kangaroos-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"graphile-build-pg": patch
"graphile-build": patch
"postgraphile": patch
"graphile-config": patch
---

Plugin name now automatically used in `provides` for every hook, allowing
ordering hooks before/after their equivalents in other plugins.
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,53 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
];
},
},
pgCodecRelation: {
provides: ["inferred"],
after: ["default", "PgRelationsPlugin"],
before: ["override"],
callback(behavior, entity, build) {
const {
input: {
pgRegistry: { pgRelations },
},
} = 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 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;
},
},
},
hooks: {
init(_, build, _context) {
Expand Down Expand Up @@ -703,18 +750,18 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
}),
`PgPolymorphismPlugin single table type for ${codec.name}`,
);
build.registerCursorConnection({
typeName: tableTypeName,
connectionTypeName:
inflection.connectionType(tableTypeName),
edgeTypeName: inflection.edgeType(tableTypeName),
scope: {
isPgConnectionRelated: true,
pgCodec: codec,
},
nonNullNode: pgForbidSetofFunctionsToReturnNull,
});
}
build.registerCursorConnection({
typeName: tableTypeName,
connectionTypeName:
inflection.connectionType(tableTypeName),
edgeTypeName: inflection.edgeType(tableTypeName),
scope: {
isPgConnectionRelated: true,
pgCodec: codec,
},
nonNullNode: pgForbidSetofFunctionsToReturnNull,
});
}
} else if (polymorphism.mode === "union") {
const interfaceTypeName = inflection.tableType(codec);
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 @@ -28,7 +28,7 @@ const getEntityBehaviorHooks = (plugin: GraphileConfig.Plugin) => {
string
> = {
provides: ["default"],
before: ["inferred"],
before: ["inferred", "override"],
callback: (behavior) => [lhs, behavior],
};
entry[1] = hook;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,6 @@ type CreateRelationalStatusPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalStatus`.
"""
relationalItemById: RelationalItem

"""The `RelationalStatus` that was created by this mutation."""
relationalStatus: RelationalStatus

Expand Down Expand Up @@ -600,11 +595,6 @@ type CreateRelationalTopicPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalTopic`.
"""
relationalItemById: RelationalItem

"""The `RelationalTopic` that was created by this mutation."""
relationalTopic: RelationalTopic

Expand Down Expand Up @@ -1294,11 +1284,6 @@ type DeleteRelationalStatusPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalStatus`.
"""
relationalItemById: RelationalItem

"""The `RelationalStatus` that was deleted by this mutation."""
relationalStatus: RelationalStatus

Expand Down Expand Up @@ -1347,11 +1332,6 @@ type DeleteRelationalTopicPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalTopic`.
"""
relationalItemById: RelationalItem

"""The `RelationalTopic` that was deleted by this mutation."""
relationalTopic: RelationalTopic

Expand Down Expand Up @@ -3346,16 +3326,6 @@ interface RelationalItem {
buildingByConstructor: Building
constructor: String
id: Int!

"""
Reads a single `RelationalStatus` that is related to this `RelationalItem`.
"""
relationalStatusById: RelationalStatus

"""
Reads a single `RelationalTopic` that is related to this `RelationalItem`.
"""
relationalTopicById: RelationalTopic
type: ItemType!
}

Expand Down Expand Up @@ -3426,11 +3396,6 @@ type RelationalStatus implements Node & RelationalItem {
nodeId: ID!
note: String

"""
Reads a single `RelationalItem` that is related to this `RelationalStatus`.
"""
relationalItemById: RelationalItem

"""
Reads a single `RelationalStatus` that is related to this `RelationalStatus`.
"""
Expand Down Expand Up @@ -3540,11 +3505,6 @@ type RelationalTopic implements Node & RelationalItem {
"""
nodeId: ID!

"""
Reads a single `RelationalItem` that is related to this `RelationalTopic`.
"""
relationalItemById: RelationalItem

"""
Reads a single `RelationalStatus` that is related to this `RelationalTopic`.
"""
Expand Down Expand Up @@ -4385,11 +4345,6 @@ type UpdateRelationalStatusPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalStatus`.
"""
relationalItemById: RelationalItem

"""The `RelationalStatus` that was updated by this mutation."""
relationalStatus: RelationalStatus

Expand Down Expand Up @@ -4447,11 +4402,6 @@ type UpdateRelationalTopicPayload {
"""
query: Query

"""
Reads a single `RelationalItem` that is related to this `RelationalTopic`.
"""
relationalItemById: RelationalItem

"""The `RelationalTopic` that was updated by this mutation."""
relationalTopic: RelationalTopic

Expand Down
Loading

0 comments on commit 47b2360

Please sign in to comment.