Multi-language strings supported by GraphQL using @lang directive

By looking at the docs I couldn’t figure out if GraphQL supports Multi-language strings.

Is it implemented or is in the roadmap? Or is there a way to implement it by writing a custom directive?

At this time, the language directive is only for the DQL endpoints /query and /mutate or grpc. I have not seen the inclusion of the language directive on any graphql roadmap for DQL. Not saying it doesn’t exist, just I haven’t seen talk about it anywhere.

As an exercise to get to know the Dgraph code base (and Go for that matter) I forked it and started to add support for a @lang directive in GQL schema definitions (/admin/schema):

type Category {
  name: String! @lang
}

This gets included in the DQL rewrite so the field is properly marked as a multi-language field in Dgraph.

I also started experimenting with the code behind /query to see if it would be possible to query for specific languages. It sort of works with a field directive:

{
  queryCategory {
    name @language(tag: "en:ru")
  }
}

(could not reuse lang name for both field definitions and fields)

But I wonder if it wouldn’t be better to do this as arguments on a parent instead:

{
  queryCategory(..., lang: "en:ru") {
    name
  }
}

The rationale is that you probably would want the same set of languages for all fields rather than mix languages for the various fields.

But; I have no idea how a multi-language mutation would be designed. So I’m starting to wonder if it’s a dead end for multi-lang strings in GQL. Do you have any ideas of how mutations could look like?

1 Like

I think you’d have to define different fields corresponding to different languages to do mutations for them. So something like

type Category {
  name: String @dgraph(pred: "name@en:ru")
  nameEn: String @dgraph(pred: "name@en")
  nameRu: String @dgraph(pred: "name@ru")
}

So while mutating, you’d supply the appropriate language like nameEn or nameRu but while querying you could just use name which gives you the ability to query the name according to a preference order. We need separate fields for different languages because otherwise during a mutation, there is no way to tell which language the name is in. So that mapping needs to be in the field.

Something like the above should already be possible using the @dgraph directive. There might be better ways of doing this but this is what comes to mind right now.

2 Likes

Aah, great suggestion! Thanks, still learning Dgraph.

1 Like

Something like the above should already be possible using the @dgraph directive. There might be better ways of doing this but this is what comes to mind right now.

Hmm, tried this and it doesn’t seem to work, any idea how this could be done?

Here’s my schema/error:

type User {
  username: String @search(by: [fulltext]) @dgraph(pred: "username@en:fr")
  usernameEn: String @dgraph(pred: "username@en")
  usernameFr: String @dgraph(pred: "username@fr")
}

"message": "resolving updateGQLSchema failed because line 2 column 10: Missing colon in type declaration. Got @ (Locations: [{Line: 3, Column: 4}])"

with dgraph v20.11.0

Yes, it doesn’t work, because it generates an incorrect DQL schema at present.

GraphQL doesn’t yet support DQL language tags. It was just posted as an idea about how it could be made to work with GraphQL.

Multi-language support is definitely something I need in GraphQL for an up-coming app that I am currently in the planning stages on. I would be willing to work on a pull-request.

For mutations (and to query all languages) we might need to do something like the following:

type Translation {
  field: String!
  lang: String!
  value: String!
}

I don’t know which languages I’m going to support, so it is not practical to bake all possible languages into a schema. The above is a solution that works within the GraphQL standard.

Please provide your feedback on RFC for above feature request: [WIP]RFC: Allow language Tag support in GraphQl