简体   繁体   中英

Is there a way to represent an object of key-value pairs in GraphQL

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM