Registry / api / type-graphql

type-graphql

JSON →
library2.0.0-rc.3jsnpmunverified

TypeGraphQL is a framework for building GraphQL APIs with TypeScript using classes and decorators. Current stable version is v2.0.0-rc.3, actively maintained with regular releases. It leverages TypeScript's decorators and type metadata to define GraphQL types, inputs, and resolvers, reducing boilerplate. Key differentiators include first-class integration with class-validator for input validation and graphql-scalars for custom scalar support. It supports both code-first and schema-first approaches, though decorator-based code-first is preferred. Requires GraphQL v16+, class-validator >=0.14.3, and graphql-scalars >=1.25.0. Beta releases occur every few months.

npm install type-graphql
INSTALL
IMPORT
SIG · TYPE-GRAPHQL
T
type-graphql
apijavascriptv2.0.0-rc.3
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.

ObjectType
import { ObjectType } from 'type-graphql'
const { ObjectType } = require('type-graphql')
ESM-only since v2; CommonJS require is unsupported.
Resolver
import { Resolver } from 'type-graphql'
import { RootResolver } from 'type-graphql'
Named export, not default. 'RootResolver' does not exist.
buildSchema
import { buildSchema } from 'type-graphql'
import buildSchema from 'type-graphql'
Named export, not default.

Shows defining a GraphQL type with ObjectType and a resolver with Query, then building the schema and starting an Apollo server.

import 'reflect-metadata'; import { ObjectType, Field, ID, Resolver, Query, buildSchema } from 'type-graphql'; import { ApolloServer } from '@apollo/server'; import { startStandaloneServer } from '@apollo/server/standalone'; @ObjectType() class Recipe { @Field(type => ID) id: string; @Field() title: string; @Field({ nullable: true }) description?: string; } @Resolver(of => Recipe) class RecipeResolver { @Query(() => Recipe) async randomRecipe(): Promise<Recipe> { return { id: '1', title: 'Pizza', description: 'Delicious' }; } } async function main() { const schema = await buildSchema({ resolvers: [RecipeResolver] }); const server = new ApolloServer({ schema }); const { url } = await startStandaloneServer(server, { listen: { port: 4000 } }); console.log(`Server running at ${url}`); } main();
Debug
Known issues
breakingv2 drops CommonJS support; only ESM modules (import) are supported.
fix
Migrate import statements to ESM syntax and ensure package.json includes 'type': 'module'.
affects: >=2.0.0-alpha.0
breakingv2 requires Node.js >= 20.11.1
fix
Upgrade Node.js to at least version 20.11.1.
affects: >=2.0.0
breakingv2 requires Reflect.defineMetadata polyfill from 'reflect-metadata' to be imported at the entry point.
fix
Add import 'reflect-metadata' at the top of your main file.
affects: >=2.0.0
deprecatedDeprecated: 'buildTypeDefsAndResolvers' is removed in v2. Use 'buildSchema' instead.
fix
Replace calls to buildTypeDefsAndResolvers with buildSchema.
affects: >=2.0.0
gotchaField returns must be decorated with @Field. Missing @Field on a property leads to silent schema omission.
fix
Ensure all GraphQL-exposed properties have the @Field decorator.
affects: >=1.0.0
gotchaResolver class methods must be decorated with @Query, @Mutation, or @FieldResolver. Omitting decorator will not infer them.
fix
Always decorate resolver methods appropriately.
affects: >=1.0.0
gotchaInheriting from a base class does not automatically inherit @Field decorators; must redeclare on subclass.
fix
Add @Field on each subclass property that should appear in the schema.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'prototype')
Missing 'reflect-metadata' import
fix
Add 'import "reflect-metadata"' at the top of your entry file.
NoExplicitTypeError: Unable to infer GraphQL type from Reflect metadata. You need to provide explicit type for argument.
Class-validator or type metadata not emitted
fix
Enable 'emitDecoratorMetadata' and 'experimentalDecorators' in tsconfig.json.
Cannot determine GraphQL type for argument named 'data' of mutation 'createRecipe'. Make sure to explicitly provide the type.
Parameter type is not decorator-friendly or missing decorator
fix
Use @Arg() decorator with explicit type, e.g., @Arg('data', () => RecipeInput) data: RecipeInput.
Duplicate directive: @ObjectType
Multiple decorators of same kind on same class
fix
Ensure each class uses only one @ObjectType() (or @InputType, etc.) decorator.
Upgrade
Version history
2.0.0-rc.3latest on npm
Audit
Dependencies
graphqlrequiredcore peer dependency for building GraphQL schemas
class-validatoroptionaldecorator-based input validation for resolvers
graphql-scalarsoptionalprovides custom scalar types like DateTime
Agent activity
34 hits · last 30 days
node
32
Resources
type-graphql — npm install type-graphql · libregistry