【问题标题】:How to define enums for key values in GraphQL如何在 GraphQL 中为键值定义枚举
【发布时间】:2019-10-19 10:27:53
【问题描述】:

如何在 GraphQL 架构中定义枚举键? 我期待的响应如下所示

 businessByState: { 
   MO: ["VALUE1", "VALUE2"],
   CA: ["VALUE1", "VALUE3", "VALUE4]
 }

我知道我可以为状态值定义枚举,但仍然想知道如何定义枚举以使键值只有 2 个字母的状态缩写?

【问题讨论】:

    标签: graphql apollo-server


    【解决方案1】:

    在 GraphQL 中,必须显式定义 Object 类型的每个字段。例如:

    type BusinessByState {
      AL: [String!]!
      AK: [String!]!
      AZ: [String!]!
      # and so on...
    }
    

    没有语法可用于基于某些输入(如现有枚举)定义具有相同类型的多个字段。

    如果你的 typeDefs 只是一个字符串,你可以使用字符串模板来节省一些输入,假设你有某种状态缩写数组:

    const states = ['AL', 'AK', 'AZ', /** and so on **/]
    const typeDefs = `
      enum STATES {
        ${states.join('\n')}
      }
    
      type BusinessByState {
        ${states.map(state => `${state}: [String!]!`).join('\n')}
      }
    `
    

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2019-10-01
      • 2018-03-20
      • 2017-03-31
      • 2022-01-03
      • 2020-06-10
      • 2020-01-12
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多