NestJS – How can dataloaders be used in combination with GraphQL Shield?

NestJS – How can dataloaders be used in combination with GraphQL Shield?


0

I recently found that because I have asynchronous calls being made in some of my shield rules, it’s causing my dataloader batch functions to be called multiple times, when they should only be called once, which leaves me with the N+1 problem.

I believe this is due to how the dataloader library requires that all of the batch function calls occur during the same event loop "tick" (source), and the asynchronous calls made in my shield rules are preventing this.

Here’s the useFactory function that I pass to GraphQLModule in my app:

  useFactory(dataloaderService: DataloaderService, usersService: UsersService) {
    return {
      context: async ({ req }: { req: Request }) => {
        const loaders = dataloaderService.getLoaders();
        const services = { usersService };
        return { loaders, services };
      },
      transformSchema: (schema: GraphQLSchema) => {
        schema = applyMiddleware(schema, shieldPermissions);
        return schema;
      },
    };
  },

Is there any way to use both dataloaders and an authorization library like GraphQL Shield at the same time? Is there anything wrong with my current configuration that could be causing this?


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

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