【问题标题】:Use number as key in GraphQL Schema?在 GraphQL Schema 中使用数字作为键?
【发布时间】:2019-02-15 08:15:50
【问题描述】:

您可以使用 GraphQL Schema 语言在 GraphQL Schema 中使用数字作为键吗?即(这是一个小sn-p...)

type tax_code_allocation_country_KOR_states {
  "11": tax_code_allocation_state_tax_query
  "26": tax_code_allocation_state_tax_query
  "27": tax_code_allocation_state_tax_query
}

或以下,我意识到这是不正确的 JSON:

type tax_code_allocation_country_KOR_states {
  11: tax_code_allocation_state_tax_query
  26: tax_code_allocation_state_tax_query
  27: tax_code_allocation_state_tax_query
}

【问题讨论】:

    标签: graphql apollo-server


    【解决方案1】:

    不,specification 禁止这样做。可以在数字前加下划线:

    type tax_code_allocation_country_KOR_states {
      _11: tax_code_allocation_state_tax_query
      _26: tax_code_allocation_state_tax_query
      _27: tax_code_allocation_state_tax_query
    }
    

    或者根本不在类型级别上执行此操作,而是映射查询或简单地运行过滤查询:

    type tax_code_allocation_country_KOR_states {
      tax(code: 11): tax_code_allocation_state_tax_query
      tax(codes: [11, 26, 27]): [tax_code_allocation_state_tax_query]
    }
    
    # query subselection
    { _11: tax(code: 11), _26: tax(code: 26), _27: tax(code: 27) }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2020-08-29
      • 2019-12-24
      • 2020-12-17
      • 2018-09-07
      • 2020-05-22
      • 2017-01-27
      • 2016-03-03
      • 2019-07-01
      相关资源
      最近更新 更多