简体   繁体   中英

How to reconcile Relay/GraphQL's globalId with mobgodb's _id?

I'm planning to use mobgodb as my backend storage and graphql + relay for the client-server communication.
How can I reconcile Relay's globalId and Mongo id? Should they even be the same, if not how can I connect one to another?

I think there are two options:

Use mongoose and set the id option to true on your models, it will generate an id attribute with the hex string

or on your graphql schemas add an id field and resolve it this way (not tested)

resolve(me) {
  return me._id.toString()
}

globalIdField is usually used to define the id field for the graphql entity and internally it uses toGlobalId function which accepts the id as the 2nd argument. fromGlobalId function could then be used in the node interface definition to extract both id and the defined type. Here is a mongodb example of how to define the id field, and then use it.

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