From 776e458c584e89660e3d8449793b7a6b2c667e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lytek?= Date: Fri, 20 Aug 2021 21:34:07 +0200 Subject: [PATCH] docs(emit-schema): add example of printing schema with directives --- docs/emit-schema.md | 30 +++++++++++++++++++ .../version-1.0.0/emit-schema.md | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/docs/emit-schema.md b/docs/emit-schema.md index f718a40fb..80ebb97be 100644 --- a/docs/emit-schema.md +++ b/docs/emit-schema.md @@ -32,3 +32,33 @@ hypotheticalFileWatcher.watch("./src/**/*.{resolver,type,input,arg}.ts", async ( await emitSchemaDefinitionFile("/path/to/folder/schema.gql", schema); }); ``` + +### Emit schema with custom directives + +Currently TypeGraphQL does not directly support emitting the schema with custom directives due to `printSchema` function limitations from `graphql-js`. + +If we want the custom directives to appear in the generated schema definition file we have to create a custom function that use a third-party `printSchema` function. + +Below there is an example that uses the `printSchemaWithDirectives` function from [`@graphql-tools/utils`](https://www.graphql-tools.com/docs/api/modules/utils): + +```typescript +import { GraphQLSchema, lexicographicSortSchema } from "graphql"; +import { printSchemaWithDirectives } from "@graphql-tools/utils"; +import { outputFile } from "type-graphql/dist/helpers/filesystem"; + +export async function emitSchemaDefinitionWithDirectivesFile( + schemaFilePath: string, + schema: GraphQLSchema, +): Promise { + const schemaFileContent = printSchemaWithDirectives(lexicographicSortSchema(schema)); + await outputFile(schemaFilePath, schemaFileContent); +} +``` + +The usage of `emitSchemaDefinitionWithDirectivesFile` function is the same as with standard `emitSchemaDefinitionFile`: + +```typescript +const schema = await buildSchema(/*...*/); + +await emitSchemaDefinitionWithDirectivesFile("/path/to/folder/schema.gql", schema); +``` diff --git a/website/versioned_docs/version-1.0.0/emit-schema.md b/website/versioned_docs/version-1.0.0/emit-schema.md index bfde890fe..3d2c98915 100644 --- a/website/versioned_docs/version-1.0.0/emit-schema.md +++ b/website/versioned_docs/version-1.0.0/emit-schema.md @@ -34,3 +34,33 @@ hypotheticalFileWatcher.watch("./src/**/*.{resolver,type,input,arg}.ts", async ( await emitSchemaDefinitionFile("/path/to/folder/schema.gql", schema); }); ``` + +### Emit schema with custom directives + +Currently TypeGraphQL does not directly support emitting the schema with custom directives due to `printSchema` function limitations from `graphql-js`. + +If we want the custom directives to appear in the generated schema definition file we have to create a custom function that use a third-party `printSchema` function. + +Below there is an example that uses the `printSchemaWithDirectives` function from [`@graphql-tools/utils`](https://www.graphql-tools.com/docs/api/modules/utils): + +```typescript +import { GraphQLSchema, lexicographicSortSchema } from "graphql"; +import { printSchemaWithDirectives } from "@graphql-tools/utils"; +import { outputFile } from "type-graphql/dist/helpers/filesystem"; + +export async function emitSchemaDefinitionWithDirectivesFile( + schemaFilePath: string, + schema: GraphQLSchema, +): Promise { + const schemaFileContent = printSchemaWithDirectives(lexicographicSortSchema(schema)); + await outputFile(schemaFilePath, schemaFileContent); +} +``` + +The usage of `emitSchemaDefinitionWithDirectivesFile` function is the same as with standard `emitSchemaDefinitionFile`: + +```typescript +const schema = await buildSchema(/*...*/); + +await emitSchemaDefinitionWithDirectivesFile("/path/to/folder/schema.gql", schema); +```