graphql/graphql-relay-js

asynchronous nodeDefinitions?

tanekim88 opened this issue · 3 comments

I am trying to get nodeDefinitions work asynchronously. Does this work? According to the definition of nodeDefinitions function, it does not seem it is designed to return a promise. But would await nodeDefinitions work?
How do people resolve this issue, when database is fetched asynchronously, like await db.findOne({ id });?

const { nodeInterface, nodeField } =  await nodeDefinitions(
    async (globalId) => {
     const { type, id } = fromGlobalId(globalId);
     const foundEntity = await db.findOne({ id });
     // return {id}
     return foundEntity;
   },
   (obj) => {
      return GraphQLUser;
   },
 );

this is async

 async (globalId) => {
     const { type, id } = fromGlobalId(globalId);
     const foundEntity = await db.findOne({ id });
     // return {id}
     return foundEntity;
   },

the nodeDefinition does not need to be

check this https://github.com/entria/graphql-dataloader-boilerplate/blob/master/src/interface/NodeInterface.js#L12

Thanks! Also, is there a way to change the default id key to _id? I am using mongodb, and it uses _id as key.
what I am trying to do is: const { type, _id } = fromGlobalId(globalId);

Use idFetcher, check code