Get started with GraphQL Yoga v3
Integration with Nest
NestJS is a progressive Node.js framework for building efficient, reliable and scalable server-side applications.
GraphQL Yoga provides its own Nest GraphQL Driver that supports building standalone GraphQL APIs and Federation GraphQL APIs (Gateway and Services).
Standalone GraphQL API
Installation
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:
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 {}
More information on the dedicated example.
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:
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 {}
More information on the dedicated example.
Apollo Federation
Yoga Nest GraphQL integration supports building Apollo Federation Gateway and Services through the YogaGatewayDriver
and YogaFederationDriver
drivers.
A complete example is available in the repository.
YogaGatewayDriver
and YogaFederationDriver
drivers should be used with
graphql@15
.