Relations
PostGraphile automatically discovers relations between database tables by
inspecting their foreign keys, and uses these to build relations into the
generated GraphQL schema. If you're using the PgIndexBehaviorsPlugin (enabled
by default) then PostGraphile will also look at the database indexes and only
include relations that don't require a table-scan to implement.
PostGraphile 会通过检查数据库表的外键自动发现表间的 relation,并据此在生成的 GraphQL schema 中构建对应的关联字段。如果你启用了 PgIndexBehaviorsPlugin(默认开启),PostGraphile 还会检查数据库索引,仅暴露那些无需全表扫描即可实现的 relation。
Views do not have constraints (foreign key or otherwise), so PostGraphile cannot
automatically expose relations for them. If you want relations on a view, add
"virtual constraints" with the @foreignKey smart tag; see the
views page for details.
视图本身没有约束(无论是外键还是其他约束),因此 PostGraphile 无法自动为视图暴露 relation。如果你需要在视图上建立关联,可以使用 @foreignKey smart tag 添加“虚拟约束”,详情请参阅视图(views)文档。
An example of a foreign key constraint when defining a table would be the
REFERENCES keyword below:
在建表时定义外键约束的示例之一,就是如下使用 REFERENCES 关键字:
create table app_public.users (
-- ...
organization_id int not null
references app_public.organizations on delete cascade,
-- ...
);
create index on app_public.users (organization_id);
Alternatively a foreign key constraint can be added after table creation:
也可以在建表之后再补充添加外键约束:
alter table users
add constraint users_organization_id_fkey
foreign key (organization_id)
references organizations
on delete cascade;
create index on users (organization_id);
You can read more about defining foreign key constraints, including constraints that utilise multiple columns, in the PostgreSQL documentation.
关于定义外键约束(包括多列复合外键约束)的更多内容,请参阅 PostgreSQL 官方文档。
PostGraphile detects and exposes one-to-one, one-to-many and many-to-one relations automatically. Many-to-many relationships can be handled with the many-to-many relations plugin.
PostGraphile 会自动识别并暴露一对一、一对多以及多对一 relation。多对多关系可以借助 many-to-many relations 插件来实现。
By default, relations are exposed as GraphQL fields using a camelCase
combination of the target type and the source fields (inflectors:
singleRelationByKeys, singleRelationByKeysBackwards, and
manyRelationByKeys). Unique constraints expose a GraphQL table type directly,
non-unique constraints expose a connection or list depending
on your configuration. The GraphQL connections that these relations expose
support pagination, filtering, and ordering (and other features
such as aggregation if you add the relevant plugin).
默认情况下,relation 会以目标类型与源字段名的驼峰(camelCase)组合形式暴露为 GraphQL 字段(对应的 inflector 为 singleRelationByKeys、singleRelationByKeysBackwards 和 manyRelationByKeys)。具有唯一约束的关联会直接暴露对应的 GraphQL 表类型;非唯一约束的关联则会根据你的配置暴露为 Connection 或普通列表。这些 relation 所暴露的 GraphQL Connection 支持分页、过滤和排序(如果安装了相应插件,还支持聚合等其他特性)。
Examples示例
Example database schema for one-to-many relation一对多 relation 的示例数据库 schema
create schema a;
create schema c;
create table c.person (
id serial primary key,
name varchar not null,
about text,
email varchar not null unique,
created_at timestamp default current_timestamp
);
create table a.post (
id serial primary key,
headline text not null,
body text,
-- `references` 👇 sets up the foreign key relation
author_id int4 references c.person(id)
);
create index on a.post (author_id);
Example query against the above schema针对上述 schema 的查询示例
{
allPosts {
nodes {
headline
body
# this relation is automatically exposed
personByAuthorId {
id
name
about
}
}
}
}
Many-to-many relations多对多 relation
Many-to-many relations can be handled with the
many-to-many relations plugin
or by using a computed column that returns setof:
多对多 relation 可以通过 many-to-many relations 插件处理,也可以使用返回 setof 的计算列(computed column)来实现:
create table post (
id serial primary key,
headline text,
body text
);
create table author (
id serial primary key,
name text
);
create table post_author (
post_id integer references post,
author_id integer references author,
primary key (post_id, author_id)
);
create function "post_authorsByPostId"(p post)
returns setof author as $$
select author.*
from author
inner join post_author
on (post_author.author_id = author.id)
where post_author.post_id = p.id;
$$ language sql stable;