Generating SDL
Warning This chapter applies only to the code first approach.
To generate a GraphQL SDL schema manually (i.e., without running an application, connecting to the database, hooking up resolvers, etc.), use the GraphQLSchemaBuilderModule:
async function generateSchema() {
const app = await NestFactory.create(GraphQLSchemaBuilderModule);
await app.init();
const gqlSchemaFactory = app.get(GraphQLSchemaFactory);
const schema = await gqlSchemaFactory.create([RecipesResolver]);
console.log(printSchema(schema));
}
Hint TheGraphQLSchemaBuilderModuleandGraphQLSchemaFactoryare imported from the@nestjs/graphqlpackage. TheprintSchemafunction is imported from thegraphqlpackage.
Usage#
The gqlSchemaFactory.create() method takes an array of resolver class references. For example:
const schema = await gqlSchemaFactory.create([
RecipesResolver,
AuthorsResolver,
PostsResolver,
]);
It also takes an optional second argument with an array of scalar classes:
const schema = await gqlSchemaFactory.create(
[RecipesResolver, AuthorsResolver, PostsResolver],
[DurationScalar, DateScalar],
);
Lastly, you can pass an options object (the same BuildSchemaOptions accepted by the buildSchemaOptions property of the GraphQLModule configuration):
const schema = await gqlSchemaFactory.create([RecipesResolver], {
skipCheck: true,
orphanedTypes: [],
});
skipCheck: skip schema validation; boolean, defaults tofalseorphanedTypes: an array of classes to generate even though they aren't explicitly referenced (i.e., aren't part of the object graph). Normally, a class that is declared but not referenced anywhere in the graph is omitted.
Federation#
GraphQLSchemaFactory builds a plain GraphQL schema: it knows nothing about Apollo Federation. To get the SDL of a code first federated subgraph (for example, to pass it to rover subgraph check or rover supergraph compose), print it with printSubgraphSchema from @apollo/subgraph instead of printSchema, so that directives such as @key, @shareable or @external are kept:
import { printSubgraphSchema } from '@apollo/subgraph';
async function generateSchema() {
const app = await NestFactory.create(GraphQLSchemaBuilderModule);
await app.init();
const gqlSchemaFactory = app.get(GraphQLSchemaFactory);
const schema = await gqlSchemaFactory.create([UsersResolver], {
orphanedTypes: [User],
});
console.log(printSubgraphSchema(schema));
}
For Federation 2, the printed SDL must also link the federation specification, or it is treated as a Federation 1 subgraph. Prepend the @link yourself, importing every federation directive your types use:
const sdl = `extend schema @link(url: "https://specs.apollo.dev/federation/v2.14", import: ["@key", "@shareable", "@external"])
${printSubgraphSchema(schema)}`;
Hint Entity types that are only referenced through_entities(for example,@extendsstubs resolved with@ResolveReference()) aren't reachable from the root types, so pass them inorphanedTypes. Otherwise, the schema is generated without them.

