Summary
We have language tag support in dgraph where we can mutate or query a single field in multiple languages. Many users wants this functionality to be added in graphql. And also users which are having data in dgraph with fields @lang directive will be able to expose that in graphql. We will add support for @lang
in graphql and allow users to mutate/query value of single field in multiple languages.
Dgraph @lang support
If we have @lang
for a field in dgraph then a single field will allow us to set or query it’s value in multiple languages using @
followed by language tag .
For example , we can set food reviews in multiple languages as follow
{
"set": [
{
"food_name": "Sushi",
"review": [
{
"comment": "Tastes very good",
"comment@jp": "とても美味しい",
"comment@ru": "очень вкусно"
}
],
"origin": [
{
"country": "Japan"
}
]
}
]
}
and we can query it using different ways to either get tag or untagged values or to get all or selected languages. For example we can get the comments for Sushi
written in any language using below query
{
food_review(func: eq(food_name,"Sushi")) {
food_name
review {
comment@*
}
}
}
{
"data": [
{
"food_name": "Sushi",
"review": [
{
"comment@jp": "とても美味しい",
"comment@ru": "очень вкусно",
"comment": "Tastes very good"
}
]
}
]
}
For more examples look at https://dgraph.io/docs/tutorial-4/#querying-using-language-tags
GraphQL Design for @lang support
In graphql we can’t mutate or query multiple values for a single field. We should explicitly have different fields for every language tag in schema and then while doing query or mutation convert them to appropriate dgraph predicates.
One solution for this is to use the @dgraph type for every such field in graphql and describe the corresponding dgraph predicate name. For example, we can have a field name
and if we want to have its value in hindhi
we can have one another field nameHi
with @dgraph(pred:name@hi)
. And similarly, we can have different such fields for other languages.
type person {
name: String @lang
nameHi: String @dgraph(pred:"name@hi")
nameEn: String @dgraph(pred:"name@En")
}
This design can lead to lots of language fields in a type if we have multiple such @lang type.
Changes
We are purposing design in which user just need to give the language codes that he wants to support and any combined codes he wants to query and we will generate the fields for the users.
For this we are adding @lang
directive on field of type String in graphql with below arguments.
fieldName: String @lang(codes: [String], combine: [String])
codes
: slice of strings, user specify the language codes that he want to support for this field.
combine
: slice of strings, merged language codes that user may want to query
Example
:
type Student {
fullName: String @lang(codes: ["hi", "en", "ru"], combine: ["hi:en","en:ru:hi","en:.","en:ru:."])
Reg_No: String
}
we will generate all the fields which correspond to the codes and combined codes.
type Student {
fullName // untagged value
fullName_hi @dgraph(pred:"fullName@hi") // corresponds to language code "hi"
fullName_en @dgraph(pred:"fullName@en") // corresponds to language code "en"
fullName_ru @dgraph(pred:"fullName@ru") // corresponds to language code "ru"
fullName_hi_en @dgraph(pred:"fullName@hi:en:.") // corresponds to combined code "hi:en"
fullName_en_ru_hi @dgraph(pred:"fullName@en:ru:hi") // corresponds to combined code "en:ru:hi" http://discuss.dgraph.io/t/wip-rfc-allow-language-tag-support-in-graphql/13027
fullName_en_un @dgraph(pred:"fullName@en:.") // corresponds to combined code "en:."
fullName_en_ru_un @dgraph(pred:"fullName@en:ru:.") // corresponds to combined code "en:ru:."
}
Notes
-
un
code is reserved for untagged string. - we won’t be able to query
name@*
type of code because it returns a list. Although this can be achieved by querying all the codes.
User requests for language tag support in graphql:
@jdgamble555 @nossila @colinskow @dkjii @gregerolsson @amaster507 Pease provide your feedback on above RFC.