gatsbyjs/gatsby · error

The $nin operator expects an array as value

Error message

The $nin operator expects an array as value

What it means

`$nin` (negated membership, `filter: { field: { nin: [...] } }`) mirrors `$in`: its value must be an Array so the runner can build a set of values to exclude. A non-array is rejected up front.

Source

Thrown at packages/gatsby/src/datastore/in-memory/indexing.ts:746

        // This will also dedupe so don't do that immediately
        return unionNodesByCounter(nodes, arr)
      }
    }

    // elemMatch can cause a node to appear in multiple buckets so we must dedupe first
    if (wasElemMatch) {
      expensiveDedupeInline(arr)
    }

    return arr
  }

  if (op === `$nin`) {
    // This is essentially the same as the $ne operator, just with multiple
    // values to exclude.

    if (!Array.isArray(filterValue)) {
      throw new Error(`The $nin operator expects an array as value`)
    }

    const values: Set<FilterValueNullable> = new Set(filterValue)
    const set = new Set(filterCache.meta.nodesUnordered)

    // Do the action for "$ne" for each element in the set of values
    values.forEach(filterValue => {
      removeBucketFromSet(filterValue, filterCache, set)
    })

    // TODO: there's probably a more efficient algorithm to do set
    //       subtraction in such a way that we don't have to re-sort
    return [...set].sort(sortByIds)
  }

  if (op === `$ne`) {
    const set = new Set(filterCache.meta.nodesUnordered)

View on GitHub (pinned to 8b06340921)

Solutions

  1. Wrap the value in an array: `nin: ["draft"]`.
  2. Use a list GraphQL variable type.
  3. If you meant a single exclude, consider `$ne` instead.

Example fix

// before
filter: { status: { nin: "draft" } }
// after
filter: { status: { nin: ["draft"] } }
Defensive patterns

Strategy: type-guard

Validate before calling

function asArray<T>(v: T | T[]): T[] { return Array.isArray(v) ? v : [v] }
// filter: { status: { nin: asArray(excluded) } }

Type guard

function isNinFilterValue(v: unknown): v is unknown[] { return Array.isArray(v) }

Prevention

When it happens

Trigger: `filter: { status: { nin: "draft" } }` or a non-array variable.

Common situations: Same as `$in`: scalar passed instead of list; mistyped GraphQL variable.

Related errors


AI-assisted analysis of gatsbyjs/gatsby@8b06340921 (2026-08-13). Data as JSON: /api/errors/f373ee6ab6d0ce6a. Report an issue: GitHub.