From 41cd2d2d962a35413331f1feb713b16f1c11aa63 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Thu, 30 Nov 2023 13:49:29 +0100 Subject: [PATCH 1/3] implement from --- grafast/dataplan-pg/src/interfaces.ts | 1 + .../src/plugins/PgRefsPlugin.ts | 2 ++ .../src/plugins/PgRelationsPlugin.ts | 32 +++++++++++++------ .../__tests__/kitchen-sink-schema.sql | 1 + .../__tests__/schema/v4/polymorphic.1.graphql | 5 +++ .../v4/skipNodePlugin.polymorphic.1.graphql | 5 +++ .../v5/skipNodePlugin.polymorphic.1.graphql | 5 +++ 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index bc100e469e..c5bcb2132f 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -45,6 +45,7 @@ export type PgDecode = ( export type PgRefDefinitionExtensions = DataplanPg.PgRefDefinitionExtensions; export interface PgRefDefinition { graphqlType?: string; + sourceGraphqlType?: string; singular?: boolean; description?: string; extensions?: DataplanPg.PgRefDefinitionExtensions; diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts index 0ea9e513bc..abe3cb06b4 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts @@ -133,6 +133,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { args: [name], params: { to, + from, plural: rawPlural, singular: rawSingular, via: rawVia, @@ -154,6 +155,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = { refDefinitions[name] = { singular, graphqlType: to, + sourceGraphqlType: from, extensions: { via: rawVia, tags: { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts index a7851af264..f22bbb9159 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts @@ -751,19 +751,31 @@ function addRelations( }> = isMutationPayload ? [] : codec.refs - ? Object.entries(codec.refs).map(([refName, spec]) => ({ - refName, - refDefinition: spec.definition, - ref: spec, - })) + ? Object.entries(codec.refs) + .filter( + ([, spec]) => + !spec.definition.sourceGraphqlType || + spec.definition.sourceGraphqlType === context.Self.name, + ) + .map(([refName, spec]) => ({ + refName, + refDefinition: spec.definition, + ref: spec, + })) : Object.entries( codec.extensions?.refDefinitions ?? (Object.create(null) as Record), - ).map(([refName, refDefinition]) => ({ - refName, - refDefinition, - codec, - })); + ) + .filter( + ([, refDefinition]) => + !refDefinition.sourceGraphqlType || + refDefinition.sourceGraphqlType === context.Self.name, + ) + .map(([refName, refDefinition]) => ({ + refName, + refDefinition, + codec, + })); type Digest = { identifier: string; diff --git a/postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql b/postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql index b29865e953..fc613e96ad 100644 --- a/postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql +++ b/postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql @@ -1364,6 +1364,7 @@ comment on table polymorphic.single_table_items is $$ @type CHECKLIST name:SingleTableChecklist attributes:title @type CHECKLIST_ITEM name:SingleTableChecklistItem attributes:description,note,priority_id @ref rootTopic to:SingleTableTopic singular via:(root_topic_id)->polymorphic.single_table_items(id) + @ref rootChecklistTopic from:SingleTableChecklist to:SingleTableTopic singular via:(root_topic_id)->polymorphic.single_table_items(id) $$; comment on constraint single_table_items_root_topic_fkey on polymorphic.single_table_items is $$ diff --git a/postgraphile/postgraphile/__tests__/schema/v4/polymorphic.1.graphql b/postgraphile/postgraphile/__tests__/schema/v4/polymorphic.1.graphql index eabc7e8e7d..e9a3a56fda 100644 --- a/postgraphile/postgraphile/__tests__/schema/v4/polymorphic.1.graphql +++ b/postgraphile/postgraphile/__tests__/schema/v4/polymorphic.1.graphql @@ -5775,6 +5775,11 @@ type SingleTableChecklist implements Node & SingleTableItem { personByAuthorId: Person position: BigInt! + """ + Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. + """ + rootChecklistTopic: SingleTableTopic + """ Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. """ diff --git a/postgraphile/postgraphile/__tests__/schema/v4/skipNodePlugin.polymorphic.1.graphql b/postgraphile/postgraphile/__tests__/schema/v4/skipNodePlugin.polymorphic.1.graphql index 46028636d9..dbc007c55c 100644 --- a/postgraphile/postgraphile/__tests__/schema/v4/skipNodePlugin.polymorphic.1.graphql +++ b/postgraphile/postgraphile/__tests__/schema/v4/skipNodePlugin.polymorphic.1.graphql @@ -5082,6 +5082,11 @@ type SingleTableChecklist implements SingleTableItem { personByAuthorId: Person position: BigInt! + """ + Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. + """ + rootChecklistTopic: SingleTableTopic + """ Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. """ diff --git a/postgraphile/postgraphile/__tests__/schema/v5/skipNodePlugin.polymorphic.1.graphql b/postgraphile/postgraphile/__tests__/schema/v5/skipNodePlugin.polymorphic.1.graphql index fc81f75c1d..60b6087f20 100644 --- a/postgraphile/postgraphile/__tests__/schema/v5/skipNodePlugin.polymorphic.1.graphql +++ b/postgraphile/postgraphile/__tests__/schema/v5/skipNodePlugin.polymorphic.1.graphql @@ -6252,6 +6252,11 @@ type SingleTableChecklist implements SingleTableItem { personByAuthorId: Person position: BigInt! + """ + Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. + """ + rootChecklistTopic: SingleTableTopic + """ Reads a single `SingleTableTopic` that is related to this `SingleTableChecklist`. """ From 536dd015bc06fffaeab453e80f7a1163e092768b Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Thu, 30 Nov 2023 13:55:31 +0100 Subject: [PATCH 2/3] document argument --- postgraphile/website/postgraphile/refs.md | 1 + 1 file changed, 1 insertion(+) diff --git a/postgraphile/website/postgraphile/refs.md b/postgraphile/website/postgraphile/refs.md index c7b41de1cf..874cb8c14b 100644 --- a/postgraphile/website/postgraphile/refs.md +++ b/postgraphile/website/postgraphile/refs.md @@ -14,6 +14,7 @@ The easiest way to define a ref is with a `@ref` smart tag. The first argument t is the name for your reference, and then it supports the following optional parameters: - `to:` - the name of the GraphQL type we're referencing (required if `via:` is not present) +- `from:` - the name of the GraphQL type we're applying the reference to when using polymorphism - `via:`- the route string (see below) through which we can reach the target - `singular` - present if this is a singular relationship - `plural` - indicates that the ref is plural (default). Not allowed if From 9fb5cc06ee52165378392969172e6ee8128833f6 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Thu, 30 Nov 2023 14:03:12 +0100 Subject: [PATCH 3/3] docs(changeset): Specify `from` in `@ref` to apply refs to a single type --- .changeset/two-panthers-laugh.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/two-panthers-laugh.md diff --git a/.changeset/two-panthers-laugh.md b/.changeset/two-panthers-laugh.md new file mode 100644 index 0000000000..d7c9ee57a9 --- /dev/null +++ b/.changeset/two-panthers-laugh.md @@ -0,0 +1,5 @@ +--- +"graphile-build-pg": minor +--- + +Specify `from` in `@ref` to apply refs to a single type