Skip to main content

Enums

PostGraphile will automatically map PostgreSQL enums into GraphQL enums; they’ll be automatically renamed in order to make sure they conform to the GraphQL naming requirements and conventions.

PostGraphile 会自动将 PostgreSQL 枚举类型映射为 GraphQL 枚举,并根据 GraphQL 的命名规范与约定自动调整名称。

create type animal_type as enum (
'CAT',
'DOG',
'FISH'
);
create table pets (
id serial primary key,
type animal_type not null,
name text not null
);

The @enum smart comments can be used to set the name (@enumName) or the description (@enumDescription) of PostgreSQL enums.

可以通过 @enum 相关的 smart comment 来指定 PostgreSQL 枚举在 GraphQL 中的名称(@enumName)或描述(@enumDescription)。

e.g.:

COMMENT ON TYPE animal_type IS E'@enum\n@enumName TypeOfAnimal';

Sometimes people prefer not to use PostgreSQL enums due to their technical limitations (e.g. you can never drop a value from a PostgreSQL enum, and you cannot add a value to it within a transaction). There are other ways to add enums to PostGraphile:

由于 PostgreSQL 枚举存在一些技术限制(例如无法从枚举中删除已有值,也不能在事务内为其新增值),不少开发者更倾向于避免使用原生枚举。在 PostGraphile 中,还可以通过以下方式添加枚举:

Enum tables枚举表(Enum tables)​

We can leverage PostgreSQL’s foreign key relations to enforce that a value is contained within a small set, defined by the values in some other table. To use this feature, we must have a table in which to contain our enums, and we must tell PostGraphile that it is an enum table using the @enum smart tag. You may also include a column named ‘description’ to provide the description for the enum value.

我们可以借助 PostgreSQL 的外键约束,限制某一列的取值必须落在另一张表所定义的有限集合内。要启用该特性,需要专门建一张表来存放枚举值,并通过 @enum smart tag 告知 PostGraphile 这是一张枚举表。你还可以添加一个名为“description”的列,用于为各枚举值提供描述信息。

create table animal_type (
type text primary key,
description text
);
comment on table animal_type is E'@enum';
insert into animal_type (type, description) values
('CAT', 'A feline animal'),
('DOG', 'A canine animal'),
('FISH', 'An aquatic animal');

create table pets (
id serial primary key,
type text not null references animal_type,
name text not null
);

We also support the @enum smart tag on unique constraints (not indexes) so you could use a single table to contain all your enums should you wish. We do not recommend this specific pattern, but it’s sometimes used in the ecosystem.

此外,我们也在唯一约束(注意是约束,而非唯一索引)上支持 @enum smart tag,这样你也可以用单张表来集中存放所有枚举。虽然我们并不推荐这种做法,但它在社区生态中偶有使用。

Should you wish to use a column other than description for the description of the enum, put the smart comment @enumDescription on the desired column.

如果你希望使用 description 之外的其他列作为枚举值的描述字段,只需在目标列上添加 @enumDescription smart comment 即可。

To set the name of the resulting enum, you may use the @enumName smart comment, e.g.:

若要自定义生成的枚举类型名称,可使用 @enumName smart comment,例如:

comment on table animal_type is E'@enum\n@enumName TypeOfAnimal';

The name must conform to the GraphQL Name restrictions.

指定的名称必须符合 GraphQL Name 规范的字符限制。

With extendSchema使用 extendSchema​

Use the standard enum GraphQL interface definition language (IDL/SDL) to define your enum:

使用标准的 GraphQL 接口定义语言(IDL/SDL)中的 enum 语法定义枚举:

import { constant } from "postgraphile/grafast";
import { gql, extendSchema } from "postgraphile/utils";

const myPlugin = extendSchema(() => ({
typeDefs: gql`
enum AnimalType {
"""
A feline animal
"""
CAT

"""
A canine animal
"""
DOG

"""
An aquatic animal
"""
FISH
}

extend type Pet {
type: AnimalType!
}
`,
enums: {
AnimalType: {
values: {
CAT: "cat",
DOG: "dog",
FISH: "fish",
},
},
},
objects: {
Pet: {
plans: {
type() {
/* TODO: add logic here */
return constant("cat");
},
},
},
},
}));

Other ways其他方式​

You can also use the underlying Graphile Build API to add a new GraphQLEnumType.

你也可以直接调用底层的 Graphile Build API 来新增 GraphQLEnumType。