Using Nest.js with GraphQL: A Simple Step-By-Step Guide

Richard HenryRichard Henry
4 min read

Building robust APIs is a key skill for software engineers and web developers. If you want fast, organized, and scalable APIs, combining Nest.js with GraphQL can streamline your workflow. The nest.js application structure gives you a solid foundation that scales cleanly as your app grows.

This practical guide covers the best way to set up and connect Nest.js with GraphQL. You'll learn why structure matters, how to organize your project for real projects, and follow a hands-on example to get started. Whether you're new to Nest.js or refining your approach, this guide makes it easy to build clean, maintainable GraphQL APIs.

Understanding Nest.js Application Structure for GraphQL APIs

A well-organized nest.js application structure helps you manage code as projects grow. This structure separates features into modules and files, keeping logic and responsibilities clear. When you work with GraphQL, having a good structure ensures fast development and easier collaboration—especially when your API expands or your team gets larger.

If you want help building highly-structured and scalable backends, check out NestJS development services to see how experts approach real-world projects.

Core Principles of Nest.js Application Structure

At its heart, the Nest.js app structure is built around modularity:

  • Modules: Break your application into logical pieces. Each module covers a feature or section.

  • Controllers: Handle incoming requests and connect to services.

  • Services: Hold business logic and talk to databases.

  • Providers: Inject reusable dependencies—like services—across your modules.

For GraphQL, you'll see an extra layer—resolvers and schemas—that map queries to your business logic.

Folders, Modules, and GraphQL Integration

Typical Nest.js projects for GraphQL use these folders:

  • /src: All source code.

  • /src/modules: Divided by feature (e.g. users, posts).

  • /src/modules/[feature]/[feature].module.ts: The module file.

  • /src/modules/[feature]/[feature].resolver.ts: Handles GraphQL queries and mutations.

  • /src/modules/[feature]/[feature].service.ts: Handles all business logic.

  • /src/modules/[feature]/dto/: Input/output data objects for validation.

When GraphQL sets up, Nest.js generates and connects schemas based on decorators and TypeScript classes.

Why Structure Matters for GraphQL APIs

A strong project structure makes teamwork easier. It:

  • Reduces code conflict: Clear separation of features lowers merge headaches.

  • Simplifies scaling: Adding new features becomes safer and faster.

  • Speeds up onboarding: New developers get up to speed fast.

By merging good module patterns with GraphQL schemas and resolvers, you unlock clean, rapid development. This is why top teams favor a strong nest.js application structure for backend APIs.

Building a Simple GraphQL API with Nest.js: Step-By-Step

Building an API doesn't need to be complex. Here’s how you can set up, structure, and test a simple GraphQL API in Nest.js, following best practices used in real production work.

Setting Up a New Nest.js Project

First, install the Nest.js CLI globally if you haven't already:

npm i -g @nestjs/cli

Create a new project:

nest new project-name
Navigate into your project folder:

cd project-name

This command gives you the standard folder structure with starter files. It's now easy to add new features as modules.

Installing and Configuring GraphQL Support

Install GraphQL dependencies:

npm install @nestjs/graphql graphql-tools graphql

For an easier development experience, add Apollo Server:

npm install @nestjs/apollo apollo-server-express

Integrate GraphQL in your project. Open app.module.ts and import the GraphQL module:

import { GraphQLModule } from '@nestjs/graphql';

@Module({

imports: [

GraphQLModule.forRoot({

autoSchemaFile: true, // Lets Nest.js generate a schema automatically

}),

],

})

export class AppModule {}

This connects your decorators and resolver classes to the schema on startup.

Creating Modules, Resolvers, and Schema

Let’s make a "users" feature. Scaffolding helps:

nest generate module users

nest generate resolver users

nest generate service users

In /src/users/users.resolver.ts, define a simple query:

@Resolver('User')

export class UsersResolver {

@Query(() => String)

helloUser(): string {

return 'Hello from GraphQL API';

}

}

The @Query() decorator marks this as a GraphQL query. Use DTOs and models to describe inputs and outputs—this keeps data types clear and code easy to test.

To connect with broader Node.js environments or add more advanced async logic, see how Node.js development services integrate with Nest.js to build high-performance APIs across different stacks.

Testing Your First Query

Run the app:

npm run start:dev

Visit http://localhost:3000/graphql in your browser. Use the built-in playground to run this query:

query {

helloUser

}

You should get back:

{

"data": {

"helloUser": "Hello from GraphQL API"

}

}

Congrats! You’ve just connected a basic folder structure, set up GraphQL, and tested an endpoint.

Conclusion

A clear nest.js application structure sets the stage for GraphQL APIs that scale and adapt as teams and products grow. Modular folders, feature-specific resolvers, and generated schemas keep things manageable and easy to expand.

By following these steps, you can build tightly-organized projects with agile development in mind. As you take on more complex challenges or larger teams, it’s worth exploring more advanced NestJS development services to fine-tune your architecture and unlock even greater speed.

Start simple, structure your project smartly, and you’ll see how quickly your APIs grow with your code and your business.

0
Subscribe to my newsletter

Read articles from Richard Henry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Richard Henry
Richard Henry