本页还没有译文
本页 24 段还没译成Español,下面显示的是英文原文。pg_graphql: GraphQL for Postgres
还没翻译译自英文(supabase.com)本页 24 段都还没译
pg_graphql is Postgres extension for interacting with the database using GraphQL instead of SQL.
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.
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.
For more information about how the SQL schema is reflected into a GraphQL schema, see the pg_graphql API docs.
Enable the extension
DashboardSQL
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "pg_graphql" and enable the extension.
-- Enable the "pg_graphql" extension
create extension pg_graphql;
-- Disable the "pg_graphql" extension
drop extension if exists pg_graphql;Even though the SQL code is create extension, this is the equivalent of "enabling the extension".
To disable an extension you can call drop extension.
Usage
Given a table
create table "Blog"(
id serial primary key,
name text not null,
description text
);
insert into "Blog"(name)
values ('My Blog');The reflected GraphQL schema can be queried immediately as
select
graphql.resolve($$
{
blogCollection(first: 1) {
edges {
node {
id,
name
}
}
}
}
$$);returning the JSON
{
"data": {
"blogCollection": {
"edges": [
{
"node": {
"id": 1,
"name": "My Blog"
}
}
]
}
}
}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:
comment on schema public is e'@graphql({"introspection": true})';See the upgrade notes for details.
API
graphql.resolve: A SQL function for executing GraphQL queries.
Resources
- Official
pg_graphqldocumentation