[BUG] `maxBatchSize: NaN` is accepted and silently disables batching
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- javascript, node.js
- 领域
- backend, performance
调研方向
阅读 DataLoader 源码中的 getValidMaxBatchSize 和构造函数验证;issue 指出了失败的比较操作和预期的检查。找到现有的 maxBatchSize 验证测试,并添加针对 NaN 的测试。使用 NaN 构造时抛出预期的 TypeError,且相关测试通过后即完成。
由索引模型根据 Issue 内容生成。
描述
new DataLoader(fn, { maxBatchSize: NaN }) throws TypeError: maxBatchSize must be a positive number: NaN, the same as it does for 0, negative numbers, and non-numbers.
Current Behavior
The constructor accepts NaN. getCurrentBatch then compares keys.length < NaN, which is always false, so every load() call starts a new batch. Batching is turned off without any error or warning.
Possible Solution
In getValidMaxBatchSize, replace maxBatchSize < 1 with !(maxBatchSize >= 1) so that NaN fails the check.
Steps to Reproduce
const DataLoader = require('dataloader');
const calls = [];
const loader = new DataLoader(
async keys => {
calls.push(keys);
return keys;
},
// For example, read from an environment variable that is not set.
{ maxBatchSize: Number(process.env.MAX_BATCH_SIZE) },
);
Promise.all([loader.load(1), loader.load(2), loader.load(3)]).then(() =>
console.log(JSON.stringify(calls)),
);
// Prints [[1],[2],[3]]. Expected a TypeError from the constructor.
Context
maxBatchSize often comes from configuration, for example Number(process.env.X) or parseInt(value, 10). Both return NaN when the value is missing or invalid. The loader keeps working, but it sends one request per key, and nothing points to the misconfiguration.
- 主要语言
- JavaScript
- 星标
- 13.4k
- 派生
- 522
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql/dataloader 的其他 Issue
-
Failed batch evicts a replacement cache entry after clear/prime可能已有人在做 @Claire-ovo 于 12 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 57/100
graphql/dataloader#403 ·
-
enhancement
难度 5/5 一周以上 新手友好度 38/100
graphql/dataloader#393 ·
-
enhancement
难度 5/5 一周以上 新手友好度 25/100
graphql/dataloader#382 ·
-
enhancement
难度 4/5 3-5 天 新手友好度 35/100
graphql/dataloader#379 · 2 条评论 · 5 个 reaction ·
-
bug
难度 4/5 3-5 天 新手友好度 35/100
graphql/dataloader#377 · 1 条评论 · 1 个 reaction ·
查看 graphql/dataloader 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
postcss/postcss.org#302 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
ImranR98/apps.obtainium.imranr.dev#2000 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
-
难度 2/5 1-3 小时 新手友好度 83/100
microsoft/monaco-editor#5577 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
Ebony-Vinyl/dsh-our-free-model#785 ·
维护者通常 1 天内回复