Why does async code mess up my dataloader in a graphql resolver?

Why does async code mess up my dataloader in a graphql resolver?


0

I have a dataloader that I’m using to batch requests to a service to get my user’s addresses.
The loader doesn’t batch requests correctly when the parent resolver uses async code.
Here’s a general idea of what my code looks like:

// User.ts 
import Demographic from './Demographic'

export type User { 
 name: 'User', 
 description: 'User',
 ...
 fields: () => {
  someField: ...
  demographicInformation: {
     type: Demographic,
     resolve: async (source, args, context) => {
      // removing the following line causes the loader in Demographics.ts to batch properly
      const [hasViewPermissions, hasAuthForDemographics] = await Promise.all([getViewPermissions(), getAuthForDemographics()])
      if (hasAuth && hasAuthForDemographics){ return source}
      else { return null } 
     
     }

 } 

}
...
//Demographics.ts
export type Demographics { 
 name: 'somename', 
 description: '',
 fields: {
    name: // ...
    address: {
     type: Address, 
     resolve:  async (source, args, context) => {
       return myloaders.addressloader.load(myid)
     }

   }
}

I’m aware that the dataloader docs say that "DataLoader will coalesce all individual loads which occur within a single frame of execution (a single tick of the event loop)", however I don’t fully understand why using an async/await messes that up if that’s done in the parent resolver (User in my case).
And, I need to have the parent check the auth using some form of async code, is there a way to still make these auth calls at the parent level and also use the dataloader effectively?

I’ve seen this issue noted in Github as well, but I don’t think any workaround (such as adding the async calls to the loader) will work for me.

2

  • since resolve: (source, args, context) => { is not async you will get an error trying to use await

    – Jaromanda X

    2 hours ago

  • Thanks! Sorry this was just some pseudo-code, let me fix that. My actual code does not get an error – but the loader doesnt batch

    – SGolds

    2 hours ago


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *