[BUG] `maxBatchSize: NaN` is accepted and silently disables batching

未关闭 适合新手
#405 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@RaphaelFakhri 已经在做这个了。

开始于 2026年11月5日。

  • #406 来自 @RaphaelFakhri —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃

调研方向

阅读 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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphql/dataloader 的其他 Issue

查看 graphql/dataloader 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。