I would like to represent many languages in my GraphQL schema using:
- Language's ISO Name
- Language's own endonym
- its ISO 639-1 code
- its ISO 639-3 code
Ideally it would look something like
enum Language {
(English, English, en, eng)
(German, Deutsch, de, deu)
(Mandarin, 官话, zh, cmn)
(Cantonese, 廣東話, zh, yue)
Because every combination of the values must be unique. However, this isn't a supported representation in GraphQL, so I thought of this kind of workaround:
type Language {
isoLanguageName: ISOLanguageName!
endonym: LanguageEndonym!
iso639_1: ISO639_1
iso639_3: ISO639_3
}
enum ISOLanguageName {
English
German
Mandarin
Cantonese
}
enum LanguageEndonym {
English
Deutsch
官话
廣東話
}
enum ISO639_1 {
en
de
zh
}
enum ISO639_3 {
eng
deu
cmn
yue
}
But I'm not sold on this at all because it doesn't specify the necessary-by-definition relationships between the values.
How can I represent languages by these data in the schema?
CodePudding user response:
GraphQL doesn't have the semantics needed to define valid combinations of enums. Your types are fine as is. I suggest just creating a JSON file that contains all the language definitions and importing it as needed (unless you'd prefer this in a database table). Also recommend an ID
for the Language
type for caching.
To import a JSON file:
import languages from 'languages.json';
languages.json: (as an object with language codes as keys, could also be done as an array)
{
"en" : {
"isoLanguageName": "English",
"endonym": "English",
"iso639_1": "en",
"iso639_3": "eng"
},
"de" : {
"isoLanguageName": "German",
"endonym": "Deutsch",
"iso639_1": "de",
"iso639_3": "deu"
},
…more languages
}