how supposed (re)query object when id
different on every load? what missing?
const {nodeinterface, nodefield} = nodedefinitions( (globalid) => { const {type, id} = fromglobalid(globalid); // id different every time (if page reloaded/refreshed) // how suppose use id database query (e.g id)? // how "the right id"? (id used in database) console.log('id:', id); // correct: "user" console.log('type:', type); if (type === 'user') { // function suppose user id useless .. return getuserbyid(id); } return null; }, (obj) => { if (obj instanceof user) { return usertype; } return null; } ); const usertype = new graphqlobjecttype({ name: 'user', fields: () => ({ id: globalidfield('user'), // relay id _id: { type: graphqlstring }, // mongodb id email: { type: graphqlstring }, name: { type: graphqlstring } }), interfaces: [nodeinterface] });
global id used refetching objects in relay client store. i'll dry , point an excellent related post, nicely explains how global id used in relay.
if use helper functions libraries, example, graphql-relay-js in javascript, dealing global id becomes pretty straight-forward:
- you decide server-side object type x corresponds graphql object type y.
- add field
id
x.id
local id , must unique object of type x. if x corresonds mongodb document type, simple approach assign string representation of_id
id
field:instanceofx.id = dbobject._id.tohexstring()
. - add field
id
y usingglobalidfield
helper function.id
global id , it's unique across types , objects. how globally unique id field generated depends on implementation.globalidfield
helper function generatesid
field in object x , type namex
. - in
nodedefinitions
, retrieve local id , type global id usingfromglobalid
helper function. since,id
field in x_id
field in mongodb, can use local id perform db operations. convert hex string mongodb id type first.
local id assignment (step 2) must broken in implementation. otherwise, id of same object not different on every reload.
Comments
Post a Comment