NestJS / Apollo Server¶
GraphLink generates a NestJS server from your schema, written the way you would write it by hand:
@Resolver classes for GraphQL, @Controller classes for @glRest* operations, and a dynamic
GraphLinkModule you import. Every collaborator is constructor-injected. You implement the services;
GraphLink owns the wiring. The service model is the same as in the Express / Apollo target.
Server mode config¶
{
"schemaPaths": ["schema/*.graphql"],
"mode": "server",
"typeMappings": { "ID": "string", "String": "string", "Float": "number", "Int": "number", "Boolean": "boolean", "Null": "null" },
"outputDir": "src/generated",
"serverConfig": {
"nestApollo": {
"port": 3000,
"graphqlPath": "/graphql",
"generateEntryPoint": true,
"injectContext": false,
"useResolveInfo": false
}
}
}
| Option | Default | Description |
|---|---|---|
port |
3000 |
Informational; your main.ts decides the port. |
graphqlPath |
/graphql |
Path the Apollo endpoint, the query depth limit and the WebSocket server are bound to. |
generateEntryPoint |
true |
Generate graphlink.module.ts and index.ts. Set to false to wire the generated resolvers and controllers yourself. |
injectContext |
false |
Pass GraphLinkContext to every service method. Use @glInjectContext to opt in per field. |
useResolveInfo |
false |
Pass GraphQLResolveInfo to every service method. |
Your tsconfig.json needs the standard Nest decorator options:
What gets generated¶
src/generated/
graphlink.module.ts ← GraphLinkModule.forRoot(...)
graphlink.options.ts ← GraphLinkModuleOptions, GRAPHLINK_OPTIONS
graphlink-exception.filter.ts
index.ts ← exports the module, services, guards, interceptor
services/ ← abstract classes: implement these
guards/ ← abstract classes for @glValidate
resolvers/
article-service.resolver.ts ← @Resolver(): queries, mutations, subscriptions
author-fields.resolver.ts ← @Resolver('Author'): @glMapsTo fields, batch loaders
rest/
rest-item-service.controller.ts ← @Controller(): @glRest operations
types/ inputs/ enums/
Services are abstract classes¶
A generated service is an abstract class, so it is its own injection token:
export abstract class ArticleService {
abstract getArticle(id: string): Promise<Article>;
abstract createArticle(input: CreateArticleInput): Promise<Article>;
abstract articleCreated(): AsyncIterable<Article>;
}
Implement it as an ordinary provider, with whatever dependencies it needs:
@Injectable()
export class ArticleServiceImpl implements ArticleService {
constructor(private readonly articles: ArticleRepository) {}
async getArticle(id: string): Promise<Article> {
return this.articles.findById(id);
}
// ...
}
Wiring¶
Provide the implementations in a module and pass that module to GraphLinkModule.forRoot:
@Module({
providers: [{ provide: ArticleService, useClass: ArticleServiceImpl }],
exports: [ArticleService],
})
class ServicesModule {}
@Module({ imports: [GraphLinkModule.forRoot({ imports: [ServicesModule] })] })
export class AppModule {}
const app = await NestFactory.create(AppModule, { rawBody: true });
app.enableCors();
await app.listen(3000);
rawBody: true is only needed when the schema has form-encoded @glRest* operations.
forRoot takes:
| Option | Description |
|---|---|
imports |
Modules that provide and export every service, guard and the interceptor. |
contextFactory |
(req, res) => GraphLinkContext, called for every HTTP request. |
maxQueryDepth |
Query depth limit for HTTP and WebSocket operations. Default 32. |
What follows the schema¶
- Resolvers. Queries, mutations and subscriptions are
@Query,@Mutationand@Subscriptionmethods. A@glValidateguard runs before the service call and a@glInterceptinterceptor runs before the guard. - Mapped fields.
@glMapsTofields are@ResolveFieldmethods; batch mappings are served from per-requestDataLoaders. - REST. Each service with
@glRest*operations gets a@Controller. Argument reading, validation and error bodies are the same as in the Express target, and unknown or wrong-method requests under the REST prefix answer 404 and 405 as JSON. - Uploads. GraphQL multipart uploads need
graphql-upload(^17.1.0) in yourpackage.json; REST uploads needbusboy. - Errors. Malformed or oversized request bodies are answered by the generated exception filter: GraphQL errors on
the GraphQL path,
{ status, error }JSON on REST paths.
Dependencies¶
"@apollo/server": "^5", "@as-integrations/express5": "^1", "@nestjs/apollo": "^13", "@nestjs/common": "^11",
"@nestjs/core": "^11", "@nestjs/graphql": "^13", "@nestjs/platform-express": "^11", "dataloader": "^2",
"graphql": "^16", "graphql-ws": "^6", "reflect-metadata": "^0.2", "rxjs": "^7"
The generated code is tested against every GraphLink client (Dart, TypeScript, Java, Kotlin, Python, Go) with the same fault, REST and GraphQL suites as the Express server.