pg_graphql

PG内的GraphQL支持

概览

扩展包名版本分类许可证语言
pg_graphql1.5.12FEATApache-2.0Rust
ID扩展名BinLibLoadCreateTrustReloc模式
2750pg_graphqlgraphql
相关扩展age pg_jsonschema jsquery pg_net http pg_summarize pg_tiktoken wrappers

not an official release by Vonng

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.5.121817161514pg_graphql-
RPMPIGSTY1.5.121817161514pg_graphql_$v-
DEBPIGSTY1.5.121817161514postgresql-$v-pg-graphql-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
u22.x86_64
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
u22.aarch64
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
u24.x86_64
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
u24.aarch64
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12
PIGSTY 1.5.12

构建

您可以使用 pig build 命令构建 pg_graphql 扩展的 RPM / DEB 包:

pig build pkg pg_graphql         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_graphql 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_graphql;          # 当前活跃 PG 版本安装
pig ext install -y pg_graphql -v 18  # PG 18
pig ext install -y pg_graphql -v 17  # PG 17
pig ext install -y pg_graphql -v 16  # PG 16
pig ext install -y pg_graphql -v 15  # PG 15
pig ext install -y pg_graphql -v 14  # PG 14
dnf install -y pg_graphql_18       # PG 18
dnf install -y pg_graphql_17       # PG 17
dnf install -y pg_graphql_16       # PG 16
dnf install -y pg_graphql_15       # PG 15
dnf install -y pg_graphql_14       # PG 14
apt install -y postgresql-18-pg-graphql   # PG 18
apt install -y postgresql-17-pg-graphql   # PG 17
apt install -y postgresql-16-pg-graphql   # PG 16
apt install -y postgresql-15-pg-graphql   # PG 15
apt install -y postgresql-14-pg-graphql   # PG 14

创建扩展

CREATE EXTENSION pg_graphql;

用法

pg_graphql: 为数据库内置 GraphQL 支持

pg_graphql 从现有的 SQL 模式反射生成 GraphQL 模式,无需额外的服务器或中间件即可直接在 PostgreSQL 内部执行 GraphQL 查询。

模式反射

表、外键和枚举类型会自动映射为 GraphQL 类型:

CREATE TABLE account (
    id serial PRIMARY KEY,
    email varchar(255) NOT NULL,
    created_at timestamp NOT NULL
);

CREATE TABLE blog (
    id serial PRIMARY KEY,
    owner_id integer NOT NULL REFERENCES account(id),
    name varchar(255) NOT NULL,
    description varchar(255)
);

CREATE TYPE blog_post_status AS ENUM ('PENDING', 'RELEASED');

CREATE TABLE blog_post (
    id uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
    blog_id integer NOT NULL REFERENCES blog(id),
    title varchar(255) NOT NULL,
    body varchar(10000),
    status blog_post_status NOT NULL,
    created_at timestamp NOT NULL
);

此模式会自动生成 GraphQL 类型(AccountBlogBlogPost),关系由外键推导而来。

命名转换

启用 snake_case 到 camelCase/PascalCase 的自动转换:

COMMENT ON SCHEMA public IS e'@graphql({"inflect_names": true})';

查询

通过 graphql.resolve 函数执行 GraphQL 查询:

SELECT graphql.resolve($$
    {
      accountCollection(first: 1) {
        edges {
          node {
            id
            email
            blogCollection {
              edges {
                node {
                  name
                  blogPostCollection(filter: { status: { eq: RELEASED } }) {
                    edges {
                      node {
                        title
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
$$);

功能特性

  • 表查询以可分页集合的形式出现在根 Query 类型上
  • 外键关系自动创建嵌套查询字段
  • 变更操作支持批量插入、更新和删除
  • 内置过滤、排序和分页功能
  • 遵守 PostgreSQL 行级安全(RLS)策略

最后修改 2026-03-14: update extension metadata (953cbd0)