Registry / database / typeorm-relations-graphql

typeorm-relations-graphql

JSON →
library3.0.0jsnpmunverified

Automatically join TypeORM relations based on GraphQL query fields to reduce N+1 queries. Version 3.0.0, compatible with TypeORM v0.3.x and GraphQL v14+. Unlike DataLoader, it generates a single SQL query with joins, avoiding multiple round trips. However, large nested joins may degrade performance, and it requires careful handling of circular references and depth limits. Written in TypeScript with built-in type definitions.

npm install typeorm-relations-graphql
INSTALL
IMPORT
SIG · TYPEORM-RELATIONS-
T
typeorm-relations-graphql
databasejavascriptv3.0.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.

getRelations
import { getRelations } from 'typeorm-relations-graphql'
const { getRelations } = require('typeorm-relations-graphql')
ESM-only since v3; require() will cause runtime error
default
import trg from 'typeorm-relations-graphql'
const trg = require('typeorm-relations-graphql').default
Default export is the main function, but named import is preferred
GraphqlRelation
import { GraphqlRelation } from 'typeorm-relations-graphql'
import { GraphqlRelation } from 'typeorm-relations-graphql/dist/types'
Type is exported from the main entry point since v3

Shows how to use getRelations to parse GraphQL info and pass relations to TypeORM find, with allowedRelations whitelist and maxDepth limit.

import { getRelations } from 'typeorm-relations-graphql'; import { getConnection } from 'typeorm'; import { GraphQLResolveInfo } from 'graphql'; async function resolveProducts(parent, args, context, info: GraphQLResolveInfo) { const relations = getRelations({ info, entity: Product, allowedRelations: ['owner', 'owner.profile'], maxDepth: 3 }); const products = await getConnection() .getRepository(Product) .find({ relations }); return products; }
Debug
Known issues
breakingv3 drops support for TypeORM v0.2.x
fix
If using TypeORM <0.3.0, install typeorm-relations-graphql@^1
affects: >=3.0.0
gotchaCircular relations can cause infinite recursion without proper depth limit
fix
Set a maxDepth option, e.g., { maxDepth: 5 }
affects: >=1.0.0
gotchaallowedRelations that do not exist will throw at runtime
fix
Ensure all allowedRelations paths are defined in your entity's relations
affects: >=1.0.0
deprecatedGraphqlRelation type was previously imported from a different path
fix
Use import { GraphqlRelation } from 'typeorm-relations-graphql' in v3+
affects: <3.0.0
gotchagetRelations may return unexpected nested relations if info contains aliases
fix
Avoid aliases in GraphQL queries or handle alias mapping manually
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: getRelations is not a function
Using CommonJS require on ESM-only package
fix
Change to import { getRelations } from 'typeorm-relations-graphql'
Cannot find module 'typeorm-relations-graphql' or its corresponding type declarations.
Missing dependency or incorrect TypeScript resolution
fix
Run 'npm install typeorm-relations-graphql' and ensure tsconfig.json has 'esModuleInterop': true
Error: No relations found for entity Product
Entity not registered in TypeORM or has no relations
fix
Verify entity is loaded in TypeORM and has @ManyToOne or similar decorators
Error: Relation path 'owner.address' not allowed
Path not in allowedRelations array
fix
Add 'owner.address' to allowedRelations or remove the field from query
Error: Circular relation detected
Self-referencing or mutually recursive relations without maxDepth
fix
Pass maxDepth option to getRelations
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency: required for GraphQL schema and info objects
typeormrequiredPeer dependency: required for TypeORM entities and query builder
Agent activity
23 hits · last 30 days
node
18
Meta
2
Amazon
1
OpenAI (training)
1
Resources
typeorm-relations-graphql — npm install typeorm-relations-graphql · libregistry