graphql-lookahead is a TypeScript library (v1.4.0) that enables GraphQL resolvers to check whether specific fields are requested in the incoming operation (query/mutation). This allows optimizing database queries by avoiding fetching nested relationships when not needed. It works with any GraphQL server and ORM. The library is actively maintained with regular releases and provides accurate field/type/hierarchy checks through a callback-based API (`until`, `next`, `nextFragment`). Key differentiators: lightweight, no magic AST parsing, typed, and supports fragment spreads. Peer dependency: graphql 16.x. ESM-only, ships TypeScript types.
npm install graphql-lookaheadNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic usage of lookahead in a resolver to conditionally fetch a field.
Upgrade GraphQL to v16.x.
Use import syntax or enable ESM in your project.
Use the `depth` option to limit recursion or early return in the callback.
Check `selection.name.value` for the original field name or use the `fieldDef` property.
Run 'npm install graphql graphql-lookahead' and ensure package.json contains '"type": "module"'.
Change to 'import { lookahead } from 'graphql-lookahead'.Use dynamic import: 'const { lookahead } = await import('graphql-lookahead');'Run 'npm install graphql@16' to upgrade.