pgSingleTablePolymorphic
此步骤类仅提供了一种在 @dataplan/pg
中支持多态性的方法;我们建议您阅读 多态性 文档,然后再决定是否需要此步骤 - 大多数情况下,最好在底层编解码器上使用 mode: "single"
多态性设置。
pgSingleTablePolymorphic
是一种非常简单的方法,用于表示所有底层值都存储在同一表中的多态类型。例如,您可能有一个 animals
表,用于存储所有猫、狗和鱼的详细信息;例如
create table animals (
id serial primary key,
type text not null,
name text not null,
date_of_birth date not null,
number_of_lives boolean,
wags_tail boolean,
freshwater boolean
);
在 GraphQL 中,我们可以将其表示为一个 Animal
接口,并有 Cat
、Dog
和 Fish
实现。
您调用 pgSingleTablePolymorphic($typeName, $row)
,传入一个表示 GraphQL 类型名称的步骤,以及一个表示表中行的步骤;例如
const $animal = animalsResource.get({ id: constant(1) });
// 'Cat', 'Dog' or 'Fish'; use a lambda step to transform the values if
// necessary.
const $typeName = $animal.get("type");
return pgSingleTablePolymorphic($typeName, $animal);