如何在 AppSync 中实现类似 graphql/dataloader 的行为?

问题描述 投票:0回答:1

我目前正在构建一个 AppSync API,在对如何使用两个数据源(每个数据源两个字段)加载四个不同字段进行一些研究后,我遇到了这个问题

对这个问题的回答似乎很好地解释了数据加载器的使用和主要使用字段解析器。问题是,在 AppSync 中,我无法使用像 graphql/dataloader 这样的工具,它允许我删除重复数据,例如对同一 DynamoDB 项目的多个 get 请求。

然后我想也许这样的东西原生内置于 AppSync 中,但在尝试并查看跟踪后,它似乎没有执行任何重复数据删除工作,只是对每个字段的 DynamoDB 表进行愚蠢的调用,尽管它们使用相同的密钥.

不,我想知道是否有一种方法可以实现这一目标,而无需在管道解析器中自己编写边界?

amazon-web-services graphql amazon-dynamodb aws-appsync dataloader
1个回答
0
投票

检查@InBatches lib,它使得使用装饰器实现数据加载器变得更容易。

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

© www.soinside.com 2019 - 2024. All rights reserved.