Registry / database / graphql-fields-to-relations

graphql-fields-to-relations

JSON →
library1.0.5jsnpmunverified

A GraphQL utility that extracts relation strings from GraphQL query fields for use with MikroORM's populate feature. Version 1.0.5 is the latest stable release; the package is maintained but sees infrequent updates. It specifically targets MikroORM users who need to dynamically populate relations based on client queries, reducing over-fetching. Unlike generic graphql-fields packages, this one outputs nested relation paths in the format MikroORM expects.

npm install graphql-fields-to-relations
INSTALL
IMPORT
SIG · GRAPHQL-FIELDS-TO-
G
graphql-fields-to-relations
databasejavascriptv1.0.5
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.

fieldsToRelations
import fieldsToRelations from 'graphql-fields-to-relations'
const fieldsToRelations = require('fieldsToRelations')
Default export. CommonJS users should use const fieldsToRelations = require('graphql-fields-to-relations').default or enable esModuleInterop.
fieldsToRelations
import fieldsToRelations from 'graphql-fields-to-relations'
import { fieldsToRelations } from 'graphql-fields-to-relations'
This is a default export, not a named export.
GraphQLResolveInfo
import { GraphQLResolveInfo } from 'graphql'
import { GraphQLResolveInfo } from 'graphql-fields-to-relations'
Type imported from 'graphql', not from this package.

Shows how to extract relation paths from GraphQL query info and pass them to MikroORM's populate option.

import fieldsToRelations from 'graphql-fields-to-relations'; import { GraphQLResolveInfo } from 'graphql'; const resolvers = { Query: { boards: (parent: unknown, args: unknown, context: unknown, info: GraphQLResolveInfo) => { const populate = fieldsToRelations(info) as string[]; // Use populate with MikroORM's find() return orm.em.find(Board, {}, { populate }); } } };
Debug
Known issues
gotchaPackage only works with MikroORM's relation naming convention; does not support custom aliases or sub-field selection within relations.
fix
Ensure your MikroORM entities use the same relation names as your GraphQL field names.
affects: >=1.0.0
gotchaThe function does not handle circular references; deeply nested queries may cause infinite recursion or stack overflow.
fix
Limit query depth or add a max depth check before calling fieldsToRelations.
affects: >=1.0.0
gotchaIf a GraphQL field is mapped to a different database column via @Field or @Property, the relation path may not match.
fix
Ensure field names in queries match entity property names.
affects: >=1.0.0
deprecatedThe package has not been updated since 2020; consider using graphql-fields or graphql-parse-resolve-info as more maintained alternatives.
fix
Migrate to graphql-fields with a MikroORM adapter if needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fieldsToRelations is not a function
Wrong import style for default export in CommonJS.
fix
Use const fieldsToRelations = require('graphql-fields-to-relations').default or enable esModuleInterop.
Cannot read property 'fieldNodes' of undefined
Passed undefined or null as info argument.
fix
Ensure the resolver receives the fourth argument (info) from GraphQL.
Maximum call stack size exceeded
Circular references in GraphQL query with infinite depth.
fix
Add a depth limit or use a query complexity analyzer.
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies
graphqloptionalUses GraphQLResolveInfo argument type
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-fields-to-relations — npm install graphql-fields-to-relations · libregistry