如何使用 GraphQL 查询 KeystoneJS 选择字段的选项

问题描述

我已经按照 docskeystonejs 中设置了一个 select 字段:

const { Select } = require('@keystonejs/fields');

keystone.createList('Order',{
  fields: {
    status: { type: Select,options: 'pending,processed' },},});

如何使用 introspection 获取此字段的有效选项列表?

解决方法

KeystoneJS 中的类型似乎使用以下格式命名:

<list name><field name>Type

此外,如果 <list name><field name> 的第一个字母是小写,则将它们更改为大写。因此,对于问题中给出的示例,字段 status 的类型名称将是:

OrderStatusType

然后可以使用以下内容查询可用选项:

query {
  __type (name: "OrderStatusType") {
    enumValues {
      name
    }
  }
}

在带有 Postgres 数据库的标准 KeystoneJS 安装上进行测试。