⚠️
This is the documentation for the old GraphQL Yoga version 2. We recommend upgrading to the latest GraphQL Yoga version 5.

Migrate to GraphQL Yoga v5

Integration with Nest

NestJS is a progressive Node.js framework for building efficient, reliable and scalable server-side applications.

GraphQL Yoga provides its own NestJS GraphQL Driver that supports building standalone GraphQL APIs and Federation GraphQL APIs (Gateway and Services).

Standalone GraphQL API

Installation

Terminal
yarn add @graphql-yoga/nestjs @nestjs/graphql
💡

For the setup of a new Nest project, please make sure to read the Nest GraphQL documentation.

Schema-First Approach

With the following Nest project structure:

- src/
  - cats/
    - cats.graphql
    - cats.resolver.ts
    - cats.module.ts
  - app.module.ts
  - main.ts

and the following .graphql schema definition (and its associated resolvers):

type Query {
  cats: [Cat]
  cat(id: ID!): Cat
}
 
type Mutation {
  createCat(createCatInput: CreateCatInput): Cat
}
 
type Subscription {
  catCreated: Cat
}
 
type Owner {
  id: Int!
  name: String!
  age: Int
  cats: [Cat!]
}
 
type Cat {
  id: Int
  name: String
  age: Int
  owner: Owner
}
"""
Test comment
"""
input CreateCatInput {
  name: String
  age: Int
}

Our Yoga Nest GraphQL Module (app.module.ts) will be defined as follows:

app.module.ts
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'
import { CatsModule } from './cats/cats.module'
 
@Module({
  imports: [
    CatsModule,
    GraphQLModule.forRoot<YogaDriverConfig>({
      driver: YogaDriver,
      typePaths: ['./**/*.graphql'],
      installSubscriptionHandlers: true
    })
  ]
})
export class AppModule {}

Code-First Approach

The code-first approach generates the GraphQL Schema based on resolvers and models.

With the following example Recipe model:

import { Directive, Field, ID, ObjectType } from '@nestjs/graphql'
 
@ObjectType({ description: 'recipe ' })
export class Recipe {
  @Field((type) => ID)
  id: string
 
  @Directive('@upper')
  title: string
 
  @Field({ nullable: true })
  description?: string
 
  @Field()
  creationDate: Date
 
  @Field((type) => [String])
  ingredients: string[]
}

and the following resolvers:

@Resolver((of) => Recipe)
export class RecipesResolver {
  constructor(private readonly recipesService: RecipesService) {}
 
  // ...
 
  @Query((returns) => [Recipe])
  recipes(@Args() recipesArgs: RecipesArgs): Promise<Recipe[]> {
    return this.recipesService.findAll(recipesArgs)
  }
 
  // ...
}

Our Yoga Nest GraphQL Module (app.module.ts) will be defined as follows:

app.module.ts
import { YogaDriver, YogaDriverConfig } from '@graphql-yoga/nestjs'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'
import { RecipesModule } from './recipes/recipes.module'
 
@Module({
  imports: [
    RecipesModule,
    GraphQLModule.forRoot<YogaDriverConfig>({
      driver: YogaDriver,
      autoSchemaFile: 'schema.gql'
    })
  ]
})
export class AppModule {}

Apollo Federation

Additionally, Yoga offers a separate driver called @graphql-yoga/nestjs-federation that supports building Apollo Federation Gateway and Services through the YogaGatewayDriver and YogaFederationDriver drivers.

A complete example is available in the repository.