19

I am using GraphQL in my Node.js application. And I want to store an object that uses keys as a locale short code and values for the string in the corresponding language. (like { en: "Hello", ru: "Привет" } etc.)

I want to represent this using GraphQL, both in input (like that):

mutation {
  addExercise(name: { en: "Hello", ru: "Привет" })
}

and in output (like that)

{
  exercises {
    _id, name
  }
}

(name should return an object of key-value pairs:)

[
  {_id: 1, name: { en: "Hello", ru: "Привет" }}
]

How can I do something like that?

I know I can store my data this way

{ name: "en", value: "Hello }`

but the previous one seems to be easier.

serge1peshcoff
  • 4,342
  • 11
  • 45
  • 76

1 Answers1

8

you can define scalar type for JSON, and then return the data as is. here is an example library that does just that (use it or copy & modify): https://github.com/taion/graphql-type-json/blob/master/src/index.js

enjoy!

HagaiCo
  • 696
  • 5
  • 9