【问题标题】:How to run GraphQL filter query on an ARRAY field type in Hasura?如何对 Hasura 中的 ARRAY 字段类型运行 GraphQL 过滤器查询?
【发布时间】:2019-11-23 05:19:09
【问题描述】:

我正在尝试对数组字段类型运行 GraphQL 过滤器查询,例如文本 ARRAY 字段类型。

在以下示例场景中:

创建表

CREATE TABLE Employee (
    firstName       text,
    lastName        text,    
    tags            text[]
);

我们可以通过以下方式之一过滤文本数组字段:

在数组类型上使用 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 字段类型上实现类似结果的参考或建议吗?

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!请查看How do I ask a good question? 并考虑编辑您的问题 - 如果您没有向我们提供您尝试过的任何代码,我们将无法帮助您,以便我们帮助您解决问题!
  • @Jeffrey,感谢您的反馈。按建议编辑。

标签: graphql hasura postgraphile


【解决方案1】:

我认为您不能直接在 Hasura 控制台中使用数组。您应该改用jsonb。更合适的是你可以使用_append_prepend_delete_key...

但您似乎可以将 Array 与 hasura 一起使用。如果您的架构来自外部服务,则数组的输入应该是文字。对于tags[] 类型的列,输入值必须是类似"{teamplayer,punctual}" 的字符串。就是这样Arrayworks in postgres

所以你的突变将是:

mutation {
  insert_table(objects:{
    tags: "{teamplayer,punctual}"
  }) {
    returning {
      tags
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 2021-12-19
    • 2021-08-19
    • 2020-01-12
    • 2016-11-07
    • 2022-01-14
    • 2020-08-13
    • 2020-01-23
    相关资源
    最近更新 更多