听说Supabase现在能够使用GraphQL的扩展🔥

首先

今天是Advent Calendar的第24天。平安夜!即使我的Advent Calendar的投稿有些延迟,我从早上8点开始就去看了《呪術◯戦》的电影。
昨天@nyax的文章是关于“自然语言处理的国际会议和论文搜索”,内容很有参考价值。虽然对于我的就业活动而言,我没有时间从事研究让我感到有些尴尬,但这篇文章确实给了我很大的帮助!

下列句子的中文表述(只需一种选择):

这是

12月3日,Supabase的博客上发布了一篇名为「pg_graphql: A GraphQL extension for PostgreSQL」的文章。
根据这篇文章,pg_graphql将模式生成、查询解析和解析器全部放在数据库服务器上,不需要外部服务。
而且,pg_graphql似乎可以检查现有的PostgreSQL模式并将GraphQL模式反映到解析器中,就像Hasura一样。
在这篇文章中,我打算尝试一下作为预阿尔法版发布的pg_graphql!

试一试

由于Supabase的扩展尚未添加,我将根据官方的快速入门敲击代码以在Docker上设置db服务器。正好,我将稍微修改创建表的部分代码。

产品表(table)具有一个标签(tag),而标签表(table)则包含多个产品,形成一对多的关系。在上一篇文章中,我们建立了多对多的关系,但是这次为简单起见我们改为了一对多关系。

---- この辺りは変更なし
create role anon;
create extension if not exists "uuid-ossp";
create extension if not exists pg_graphql cascade;


grant usage on schema public to postgres, anon;
alter default privileges in schema public grant all on tables to postgres, anon;
alter default privileges in schema public grant all on functions to postgres, anon;
alter default privileges in schema public grant all on sequences to postgres, anon;

grant usage on schema graphql to postgres, anon;
grant all on function graphql.resolve to postgres, anon;

alter default privileges in schema graphql grant all on tables to postgres, anon;
alter default privileges in schema graphql grant all on functions to postgres, anon;
alter default privileges in schema graphql grant all on sequences to postgres, anon;


-- GraphQL Entrypoint
create function graphql("operationName" text default null, query text default null, variables jsonb default null)
    returns jsonb
    language sql
as $$
    select graphql.resolve(query, variables);
$$;

---- ここまで変更なし

create table tag(
    id serial primary key,
    name varchar(255) not null,
    created_at timestamp not null,
    updated_at timestamp not null
);

create table product(
    id serial primary key,
    name varchar(255) not null,
    created_at timestamp not null,
    updated_at timestamp not null,
    tag_id integer not null references tag(id)
);

-- insert data
insert into tag(name, created_at, updated_at)
values
    ('果物', now(), now()),
    ('魚', now(), now()),
    ('肉', now(), now());

insert into product(name, tag_id, created_at, updated_at)
values
    ('りんご', 1, now(), now()),
    ('みかん', 1, now(), now()),
    ('さば', 2, now(), now()),
    ('うに', 2, now(), now()),
    ('馬刺し', 3, now(), now());

执行上述操作并打开http://localhost:4000/,可以看到能够打开GraphiQL的GUI界面,并自动生成了模式和查询。

query.png

如果发送以下查询、

{
  allTags {
    totalCount
    edges {
      node {
        id
        name
        products {
          edges {
            node {
              id
              name
        }
      }
    }
      }
    }
  }
}

会返回这个样子。

{
  "data": {
    "allTags": {
      "edges": [
        {
          "node": {
            "id": 1,
            "name": "果物",
            "products": {
              "edges": [
                {
                  "node": {
                    "id": 1,
                    "name": "りんご"
                  }
                },
                {
                  "node": {
                    "id": 2,
                    "name": "みかん"
                  }
                }
              ]
            }
          }
        },
        {
          "node": {
            "id": 2,
            "name": "魚",
            "products": {
              "edges": [
                {
                  "node": {
                    "id": 3,
                    "name": "さば"
                  }
                },
                {
                  "node": {
                    "id": 4,
                    "name": "うに"
                  }
                }
              ]
            }
          }
        },
        {
          "node": {
            "id": 3,
            "name": "肉",
            "products": {
              "edges": [
                {
                  "node": {
                    "id": 5,
                    "name": "馬刺し"
                  }
                }
              ]
            }
          }
        },
      ],
      "totalCount": 4
    }
  },
  "errors": []
}

顺便提一句,如果想要获取一个产品,自动生成的文档中指出需要指定nodeId,或者在获取嵌套标签时也要指定nodeId。实际上,当你在编辑器中输入以下查询时会被警告,但是响应会返回预期的结果。?

{
  product(id: 1) {
    id
    name
        tag {
            id
            name
        }
    }
}
ang.png

↓回答

{
  "data": {
    "product": {
      "id": 1,
      "tag": {
        "id": 1,
        "name": "果物"
      },
      "name": "りんご"
    }
  },
  "errors": []
}

对此的感受

    • Hasuraと比べたメリットは現時点ではあまり感じられない?

 

    • MutationやSubscriptionなど、ドキュメントに書かれていないことについても調べていきたい

 

    • まだプレアルファ版なので、今後に期待!

 

    リリースのたびにテンションが上がるOSSなので、自分も貢献したい!

以上、我对这个杂项新闻的延迟非常抱歉~?

广告
将在 10 秒后关闭
bannerAds