Closed5

graphql/dataloaderを読んだときのメモ

Kazuki BandaiKazuki Bandai

To uphold the constraints of the batch function, it must return an Array of values the same length as the Array of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:

[
  { id: 2, name: 'San Francisco' },
  { id: 9, name: 'Chicago' },
  null, // or perhaps `new Error()`
  { id: 1, name: 'New York' },
];

key: 6 に紐づくデータが無い時は一貫性を保つために無いことを示すデータを返さなければならない (mustなので)

Kazuki BandaiKazuki Bandai

Clearing Cache
In certain uncommon cases, clearing the request cache may be necessary.
The most common example when clearing the loader's cache is necessary is after a mutation or update within the same request, when a cached value could be out of date and future loads should not use any possibly cached value.
Here's a simple example using SQL UPDATE to illustrate.

// Request begins...
const userLoader = new DataLoader(...)

// And a value happens to be loaded (and cached).
const user = await userLoader.load(4)

// A mutation occurs, invalidating what might be in cache.
await sqlRun('UPDATE users WHERE id=4 SET username="zuck"')
userLoader.clear(4)

// Later the value load is loaded again so the mutated data appears.
const user = await userLoader.load(4)

// Request completes.

データ更新などをしたらcacheをclearしなければならないケースがある

このスクラップは2024/01/11にクローズされました