Use number as key in GraphQL Schema?

我与影子孤独终老i 提交于 2020-02-03 23:38:31

问题


Can you use numbers as a key in GraphQL Schema using the GraphQL Schema Language? i.e. (this is a small snippet...)

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
}

OR the below, which I realise is incorrect 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
}

回答1:


No, this is forbidden by the specification. It is possible to prefix the numbers with an underscore:

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
}

Or alternatively not do this on the type level at all and instead map the query or simply run a filter 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) }


来源:https://stackoverflow.com/questions/52265518/use-number-as-key-in-graphql-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!