Registry / devops / graphql-fields-list

graphql-fields-list

JSON →
library2.3.0jsnpmunverified

Utility library for extracting requested field names from GraphQL resolver info objects. Version 2.3.0 is current stable; released actively with semver. It parses GraphQLResolveInfo to return lists, maps (including nested), or MongoDB-like projection objects. Differentiators: simple API, built-in TypeScript types, and support for directives and field aliases. Alternatives like graphql-fields and graphql-parse-resolve-info have different APIs or are less maintained.

npm install graphql-fields-list
INSTALL
IMPORT
SIG · GRAPHQL-FIELDS-LIS
G
graphql-fields-list
devopsjavascriptv2.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fieldsList
import { fieldsList } from 'graphql-fields-list';
const fieldsList = require('graphql-fields-list');
ESM import for TypeScript/ES modules. For CommonJS, use require('graphql-fields-list').fieldsList.
fieldsMap
import { fieldsMap } from 'graphql-fields-list';
import fieldsMap from 'graphql-fields-list';
fieldsMap is a named export, not default. In v2+, options are passed as an object, not positional arguments.
fieldsProjection
import { fieldsProjection } from 'graphql-fields-list';
const { fieldsProjection } = require('graphql-fields-list').default;
fieldsProjection is a named export; no default export exists.

Shows how to use fieldsList, fieldsMap, and fieldsProjection in a resolver to extract requested fields from GraphQLResolveInfo.

import { fieldsList, fieldsMap, fieldsProjection } from 'graphql-fields-list'; // Example resolver function resolve(source, args, context, info) { console.log(fieldsList(info)); // ['id', 'firstName', 'lastName'] console.log(fieldsMap(info)); // { id: false, firstName: false, lastName: false } console.log(fieldsProjection(info)); // { id: 1, firstName: 1, lastName: 1 } }
Debug
Known issues
breakingSince v2.0.0, fieldsMap() uses an options object argument instead of separate positional arguments (path, withDirectives).
fix
Change fieldsMap(info, 'some.path', true) to fieldsMap(info, { path: 'some.path', withDirectives: true }).
affects: >=2.0.0
gotchafieldsList and fieldsMap only return fields at the current level, not nested fields, unless options.fieldsListNested is used (not available in 2.x). For nested fields, use fieldsMap and inspect the object.
fix
For nested fields, use fieldsMap and recursively extract keys, or consider using fieldsProjection which flattens nested paths.
affects: *
gotchafieldsMap returns an object where leaf fields have value false, not true. This differs from some other libraries.
fix
To check if a field is requested, use if (fieldsMap(info)[field] !== undefined) or use fieldsList which returns an array.
affects: *
Errors
Common errors & fixes
TypeError: fieldsMap is not a function
Using default import instead of named import: import fieldsMap from 'graphql-fields-list'
fix
Use named import: import { fieldsMap } from 'graphql-fields-list'
TypeError: fieldsMap(...) is not a function
Passing arguments incorrectly (old v1 style) in v2: fieldsMap(info, path, withDirectives)
fix
Use options object: fieldsMap(info, { path: 'some.path' })
Upgrade
Version history
2.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
graphql-fields-list — npm install graphql-fields-list · libregistry