How can I achieve graphql/dataloader like behaviour in AppSync?

How can I achieve graphql/dataloader like behaviour in AppSync?


1

I am currently building an AppSync API and after doing some research into how to load four different fields with two data sources (two fields per data source) I came across this question.

The response to this question seems to do a great job explaining the use of data loaders and using mostly field resolver. The problem is that in AppSync I can’t use tools like graphql/dataloader which would allow me to deduplicate for example multiple get requests to the same DynamoDB item.

Then I thought maybe something like this is built into AppSync natively but after trying and looking at the traces it seems to not be doping any deduplication work and just does the dumb call to the DynamoDB table for each field, although they use the same key.

No I was wondering if there is a way to achieve this without borderline writing it myself in a pipeline resolver?

Share
Improve this question

1 Answer
1

Reset to default


0

Check the @InBatches lib, it makes it easier to implement dataloader using a decorator.

import { InBatches } from 'inbatches';

class MyService {

  // (optional) overloaded method, where you define the keys as `number` and the return type as `string` for typings
  async fetch(keys: number): Promise<string>;

  // in reality the Decorator will wrap this method and it will never be called with a single key :)
  @InBatches() // This method is now batch-enabled
  async fetch(keys: number | number[]): Promise<string | string[]> {
    if (Array.isArray(keys)) {
      return this.db.getMany(keys);
    }

    // the Decorator will wrap this method and because of that it will never be called with a single key
    throw new Error('It will never be called with a single key 😉');
  }
}

https://www.npmjs.com/package/inbatches

Share
Improve this answer



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

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