graphql - 如何对 Hasura 中的 ARRAY 字段类型运行 GraphQL 过滤器查询?
问题描述
我正在尝试对数组字段类型运行 GraphQL 过滤器查询,例如在文本 ARRAY 字段类型上。
在以下示例场景中:
创建表
CREATE TABLE Employee (
firstName text,
lastName text,
tags text[]
);
我们可以通过以下方式之一过滤文本数组字段:
在 ARRAY 类型上使用 CONDITION SELECT STATEMENT
SELECT * FROM Employee WHERE tags @> ARRAY['teamplayer']::varchar[]
这在 PostGres 和 Postgraphile 中隐式有效。
在 Postgraphile GraphQL 上,我们可以查询上表如下:
询问
{
allEmployees(filter: {tags: {contains: "teamplayer"}}) {
nodes {
firstName
lastName
tags
}
}
}
结果将是:
回复
{
"data": {
"allEmployees": {
"nodes": [
{
firstName: 'Russell'
lastName: 'Dodds'
tags: ['teamplayer', 'punctual']
},
{
firstName: 'Emma'
lastName: 'Samsin'
tags: ['teamplayer']
}
]
}
}
}
有人可以给我一些关于如何在 Hasura 中的 ARRAY 字段类型上获得类似结果的参考或建议吗?
解决方案
我认为您不能直接在 Hasura 控制台中使用数组。你应该jsonb
改用。它更合适,因为您可以使用_append
, _prepend
, _delete_key
...
但似乎您可以Array
与 hasura 一起使用。如果您的架构来自外部服务,则数组的输入应该是文字。对于类型tags[]
的列,输入值必须是字符串,如:"{teamplayer,punctual}"
。这就是Array
在 postgres 中的工作方式。
所以你的突变将是:
mutation {
insert_table(objects:{
tags: "{teamplayer,punctual}"
}) {
returning {
tags
}
}
}
推荐阅读
- blazor - 为什么我的 Blazor 日期输入类型无法正常工作?
- python - 有没有一种方法可以创建可供 .NET 和 Python 使用的库?
- python - 我是 python 新手,我偶然发现了一个函数/变量?我不知道它是做什么的,有人可以解释一下吗?
- python - 如何在模板的类中显示函数的循环?
- reporting-services - 如何查找 SSRS 报告的最后部署日期
- c# - WPF Datagrid:根据单元格值为整行或行中的第一个单元格着色
- android - 自定义可绘制状态
- php - PHP 中 Iconomi API 中的身份验证
- microcontroller - int32_t 用于 32 微控制器中的堆栈
- linux - 大对齐的 mmap