pg_graphql:Postgres 的 GraphQL
pg_graphql 是一个 Postgres 扩展,用于使用 GraphQL 而不是 SQL 来与数据库交互。
这个扩展会基于现有的 SQL 架构生成一个 GraphQL 架构,并通过一个 SQL 函数 graphql.resolve(...) 对外提供。这样,任何能连接 Postgres 的编程语言都可以通过 GraphQL 查询数据库,而不需要额外的服务器、进程或库。
🌐 The extension reflects a GraphQL schema from the existing SQL schema and exposes it through a SQL function, graphql.resolve(...). This enables any programming language that can connect to Postgres to query the database via GraphQL with no additional servers, processes, or libraries.
pg_graphql resolve 方法是为了与 PostgREST 互操作而设计的,这是支撑 Supabase API 的工具,这样 graphql.resolve 函数就可以通过 RPC 被调用,从而安全高效地通过 HTTP/S 暴露 GraphQL API。
🌐 The pg_graphql resolve method is designed to interop with PostgREST, the tool that underpins the Supabase API, such that the graphql.resolve function can be called via RPC to safely and performantly expose the GraphQL API over HTTP/S.
想了解更多关于 SQL 架构如何反映到 GraphQL 架构的信息,请查看 pg_graphql API 文档。
🌐 For more information about how the SQL schema is reflected into a GraphQL schema, see the pg_graphql API docs.
启用扩展 #
🌐 Enable the extension
- 在仪表板中转到数据库页面。
- 点击侧边栏的 扩展。
- 搜索“pg_graphql”并启用该扩展。
用法 #
🌐 Usage
给定一个表
🌐 Given a table
1create table "Blog"(2 id serial primary key,3 name text not null,4 description text5);67insert into "Blog"(name)8values ('My Blog');反射的 GraphQL 架构可以立即被查询,就像
🌐 The reflected GraphQL schema can be queried immediately as
1select2 graphql.resolve($$3 {4 blogCollection(first: 1) {5 edges {6 node {7 id,8 name9 }10 }11 }12 }13 $$);返回 JSON
🌐 returning the JSON
1{2 "data": {3 "blogCollection": {4 "edges": [5 {6 "node": {7 "id": 1,8 "name": "My Blog"9 }10 }11 ]12 }13 }14}注意,pg_graphql 支持模式自省,因此你可以连接任何 GraphQL IDE 或模式检查工具来查看 API 中可用的完整字段和参数集合。从 pg_graphql 1.6.0 开始,自省默认被禁用,必须在每个模式中手动启用:
🌐 Note that pg_graphql supports schema introspection, so you can connect any GraphQL IDE or schema inspection tool to see the full set of fields and arguments available in the API. Starting from pg_graphql 1.6.0, introspection is disabled by default and must be enabled per schema:
1comment on schema public is e'@graphql({"introspection": true})';详情请查看升级说明。
🌐 See the upgrade notes for details.
应用接口 #
🌐 API
graphql.resolve:一个用于执行 GraphQL 查询的 SQL 函数。
资源 #
🌐 Resources