Aller au contenu

Getting Started

This guide will help you get started with Type GraphQL Query in your Plus261 project. You’ll learn how to create your first DTO, configure a module, and build a GraphQL schema.

By the end of this guide, you’ll be able to:

  • ✅ Set up Type GraphQL Query in your project
  • ✅ Create your first DTO (Data Transfer Object)
  • ✅ Configure a module with automatic resolvers
  • ✅ Build a GraphQL schema
  • ✅ Understand the basic concepts

Before starting, make sure you have:

  • TypeScript - Basic TypeScript knowledge
  • GraphQL - Basic understanding of GraphQL concepts
  • Decorators - Familiarity with TypeScript decorators
  • Node.js/Bun - Runtime environment

Install Dependencies

Install the required packages:

```bash bun add type-graphql graphql reflect-metadata ```

Configure TypeScript

Enable decorators in your tsconfig.json file.

Import Reflect Metadata

Add at the top of your entry file:

```typescript import "reflect-metadata"; ```

Add the following to your tsconfig.json:

{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}

Let’s create a simple example to get you started. We’ll create a User type with automatic CRUD operations.

Create your first GraphQL type:

import { ObjectType, Field, ID } from '@lib/type-graphql';
import { IDField, EnhancedField } from '@lib/type-graphql-query';
@ObjectType()
export class User {
@IDField(() => ID)
id!: string;
@EnhancedField(() => String)
name!: string;
@EnhancedField(() => String)
email!: string;
}

Create a module configuration that automatically generates CRUD resolvers:

import { TypeGraphqlQueryOptions } from '@lib/type-graphql-query';
import { User } from './dto/user.type';
import { user as userTable } from '@lib/db';
const resolvers = [
{
DTOClass: User,
Entity: userTable,
},
] satisfies TypeGraphqlQueryOptions['resolvers'];
export default resolvers;

Build your GraphQL schema using buildTypeDefsAndResolvers:

import { buildTypeDefsAndResolvers } from '@lib/type-graphql-query';
import { withDrizzle } from '@lib/drizzle-query';
import { withTypedi } from '@lib/typedi-query';
import { db } from '../database';
import userModuleResolvers from './user/user.module';
const { resolvers, typeDefs } = await buildTypeDefsAndResolvers(
withTypedi(
withDrizzle({
resolvers: [...userModuleResolvers],
database: db,
}),
),
);

Once configured, Type GraphQL Query automatically generates:

Feature Description
🔍 Queries findOneUser, users (with filtering, sorting, paging)
✏️ Mutations createOneUser, updateOneUser, deleteOneUser, upsertOneUser
🔄 Subscriptions Real-time updates (if enabled)
🔐 Authorization Permission checks (if configured)
📊 Aggregations Aggregate queries (if enabled)
Fenêtre de terminal
cd apps/api
bun dev

Navigate to http://localhost:4000/graphql and try a query:

query {
users {
nodes {
id
name
email
}
totalCount
}
}
mutation {
createOneUser(input: { name: "John Doe", email: "john@example.com" }) {
id
name
email
}
}
Task Documentation
Create DTOs DTOs Guide
Configure Modules Resolvers Guide
Add Queries Queries Guide
Add Mutations Mutations Guide
Add Relations Relations Guide
Add Authorization Authorization Guide

Continue your learning journey:

  1. 📝 DTOs - Learn about DTO definitions and decorators
  2. 🔍 Queries - Understand query operations with filtering and sorting
  3. ✏️ Mutations - Learn about data modifications
  4. 🔗 Relations - Manage relationships between entities