Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use number as key in GraphQL Schema?

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
}
like image 972
Matthew P Avatar asked Sep 10 '18 21:09

Matthew P


People also ask

How do you use scalar in GraphQL?

The GraphQL specification includes default scalar types Int , Float , String , Boolean , and ID . Although these scalars cover the majority of use cases, some applications need to support other atomic data types (such as Date ) or add validation to an existing type. To enable this, you can define custom scalar types.

What is a scalar in GraphQL?

Scalars are “leaf” values in GraphQL. There are several built-in scalars, and you can define custom scalars, too. (Enums are also leaf values.) The built-in scalars are: String , like a JSON or Ruby string.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).


1 Answers

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) }
like image 97
Herku Avatar answered Oct 31 '22 23:10

Herku