Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify from in @ref to apply refs to a single type #1876

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-panthers-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphile-build-pg": minor
---

Specify `from` in `@ref` to apply refs to a single type
1 change: 1 addition & 0 deletions grafast/dataplan-pg/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type PgDecode<TForJavaScript, TFromPostgres = string> = (
export type PgRefDefinitionExtensions = DataplanPg.PgRefDefinitionExtensions;
export interface PgRefDefinition {
graphqlType?: string;
sourceGraphqlType?: string;
singular?: boolean;
description?: string;
extensions?: DataplanPg.PgRefDefinitionExtensions;
Expand Down
2 changes: 2 additions & 0 deletions graphile-build/graphile-build-pg/src/plugins/PgRefsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = {
args: [name],
params: {
to,
from,
plural: rawPlural,
singular: rawSingular,
via: rawVia,
Expand All @@ -154,6 +155,7 @@ export const PgRefsPlugin: GraphileConfig.Plugin = {
refDefinitions[name] = {
singular,
graphqlType: to,
sourceGraphqlType: from,
extensions: {
via: rawVia,
tags: {
Expand Down
32 changes: 22 additions & 10 deletions graphile-build/graphile-build-pg/src/plugins/PgRelationsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, never>),
).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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 $$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
"""
Expand Down
1 change: 1 addition & 0 deletions postgraphile/website/postgraphile/refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down