reactjs - How to use global ID? -


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:

  1. you decide server-side object type x corresponds graphql object type y.
  2. 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().
  3. add field id y using globalidfield helper function. id global id , it's unique across types , objects. how globally unique id field generated depends on implementation. globalidfield helper function generates id field in object x , type name x.
  4. in nodedefinitions, retrieve local id , type global id using fromglobalid 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