Skip to content
Back to skills

Graphql N Plus One

ASecurity

Finds resolvers that fire one query per parent object and fixes them with request-scoped dataloaders, batching and proof from query counts.

  • 3 stars
  • 0 votes
  • 0 copies
  • 6 views
  • Added November 15, 2026
ai-agentsdatabase

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill graphql-n-plus-one --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Graphql N Plus One?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Graphql N Plus One
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-graphql-n-plus-one/badge)](https://www.skillsdirectory.com/skills/workspace-templates-graphql-n-plus-one)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: graphql-n-plus-one
description: Finds resolvers that fire one query per parent object and fixes them with request-scoped dataloaders, batching and proof from query counts.
---

# GraphQL N+1

Count queries before and after. No count, no fix.

## Steps

1. Reproduce with one realistic query that returns a list with nested fields. Enable query logging (ORM debug flag, `pg_stat_statements` or a logging data source) and count statements for that single request. Record the number.
2. Find the pattern: a resolver on a child type that runs a lookup by the parent's id, for example `Order.customer` calling `db.customer.findById(order.customerId)`.
3. Add a dataloader per resolver family. Create loaders in the per-request context, never at module level, so one user's cache and permissions never leak to another.
4. The batch function receives an array of keys and must return an array of the same length and order. Fetch with one `WHERE id IN (...)` query, then map back by key and return `null` or an error for the missing ones.
5. For one-to-many fields, load by foreign key and group the rows into an array per key.
6. Where a query can join cheaply (the parent query already knows the children are always wanted), prefer a join or a lookahead on the selected fields over a loader.
7. Add a test that runs the query against a fixture of 20 parents and asserts the statement count (for example 3, not 41).

## Rules

- Never share a loader between requests.
- Never rely on the batch result order from the database; reorder it.
- Do not add loaders for fields that are not in a list; it adds code with no gain.

## Report

Query count before and after, resolvers changed, loaders added and the test that guards them.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.