使用 Postgraphile 进行关系查询
Making relational queries with Postgraphile
我有以下数据库设置
事件
| event_id | event_name |
|----------|------------|
| 1 | Test |
| 2 | World |
奖项
| award_id | event_id | award_name |
|----------|----------|-------------|
| 1 | 1 | greatness |
| 2 | 2 | really good |
我正在尝试按如下方式查询数据库:
{
allAwards {
edges {
node {
awardId
eventByEventId(eventId: eventId) {
year
name
country
location
date
dateCreated
}
}
}
}
}
出现以下错误:
Cannot query field "eventByEventId" on type "Award". Did you mean "eventId"?",
GraphQL 不查询数据库,你用的是什么中间件?
eventByEventId
必须在 GraphQL 解析器中定义。
您收到的错误表明您在表之间没有外键约束(即 references events
),请参阅 PostGraphile 文档中的 relations。您的查询也是无效的,因为它向 eventByEventId
字段添加了不存在的参数: eventId
是隐式的,因为它已经存在于您正在查询的记录中。如果您使用 GraphiQL 或类似的 GraphQL IDE,它应该会告诉您您的查询在哪里与 GraphQL 架构不匹配,甚至会提示您如何修复它。
我已经使用以下 SQL 重新创建了您的架构:
create table events (
event_id serial primary key,
event_name text
);
insert into events (event_name) values ('Test'), ('World');
create table awards (
award_id serial primary key,
event_id int references events, -- < THIS IS THE IMPORTANT BIT
award_name text
);
insert into awards (event_id, award_name) values (1, 'greatness'), (2, 'really good');
然后 运行 反对它的最新邮寄:
$ npx postgraphile@latest -c deleteme
npx: installed 119 in 11.26s
PostGraphile v4.4.1 server listening on port 5000
‣ GraphQL API: http://localhost:5000/graphql
‣ GraphiQL GUI/IDE: http://localhost:5000/graphiql (enhance with '--enhance-graphiql')
‣ Postgres connection: postgres:///deleteme
‣ Postgres schema(s): public
‣ Documentation: https://graphile.org/postgraphile/introduction/
‣ Join Jimmy McBroom in supporting PostGraphile development: https://graphile.org/sponsor/
* * *
并发出 GraphQL 查询:
{
allAwards {
edges {
node {
awardId
eventByEventId {
eventName
}
}
}
}
}
这一切都按预期工作:
PS:我建议你使用@graphile-contrib/pg-simplify-inflector插件来自动简化各种关系的名称。使用此插件,命令行将是:
postgraphile -c deleteme --append-plugins @graphile-contrib/pg-simplify-inflector
以及查询:
{
awards {
edges {
node {
awardId
event {
eventName
}
}
}
}
}
我有以下数据库设置
事件
| event_id | event_name |
|----------|------------|
| 1 | Test |
| 2 | World |
奖项
| award_id | event_id | award_name |
|----------|----------|-------------|
| 1 | 1 | greatness |
| 2 | 2 | really good |
我正在尝试按如下方式查询数据库:
{
allAwards {
edges {
node {
awardId
eventByEventId(eventId: eventId) {
year
name
country
location
date
dateCreated
}
}
}
}
}
出现以下错误:
Cannot query field "eventByEventId" on type "Award". Did you mean "eventId"?",
GraphQL 不查询数据库,你用的是什么中间件?
eventByEventId
必须在 GraphQL 解析器中定义。
您收到的错误表明您在表之间没有外键约束(即 references events
),请参阅 PostGraphile 文档中的 relations。您的查询也是无效的,因为它向 eventByEventId
字段添加了不存在的参数: eventId
是隐式的,因为它已经存在于您正在查询的记录中。如果您使用 GraphiQL 或类似的 GraphQL IDE,它应该会告诉您您的查询在哪里与 GraphQL 架构不匹配,甚至会提示您如何修复它。
我已经使用以下 SQL 重新创建了您的架构:
create table events (
event_id serial primary key,
event_name text
);
insert into events (event_name) values ('Test'), ('World');
create table awards (
award_id serial primary key,
event_id int references events, -- < THIS IS THE IMPORTANT BIT
award_name text
);
insert into awards (event_id, award_name) values (1, 'greatness'), (2, 'really good');
然后 运行 反对它的最新邮寄:
$ npx postgraphile@latest -c deleteme
npx: installed 119 in 11.26s
PostGraphile v4.4.1 server listening on port 5000
‣ GraphQL API: http://localhost:5000/graphql
‣ GraphiQL GUI/IDE: http://localhost:5000/graphiql (enhance with '--enhance-graphiql')
‣ Postgres connection: postgres:///deleteme
‣ Postgres schema(s): public
‣ Documentation: https://graphile.org/postgraphile/introduction/
‣ Join Jimmy McBroom in supporting PostGraphile development: https://graphile.org/sponsor/
* * *
并发出 GraphQL 查询:
{
allAwards {
edges {
node {
awardId
eventByEventId {
eventName
}
}
}
}
}
这一切都按预期工作:
PS:我建议你使用@graphile-contrib/pg-simplify-inflector插件来自动简化各种关系的名称。使用此插件,命令行将是:
postgraphile -c deleteme --append-plugins @graphile-contrib/pg-simplify-inflector
以及查询:
{
awards {
edges {
node {
awardId
event {
eventName
}
}
}
}
}