Skip to content

Commit

Permalink
docs(emit-schema): add example of printing schema with directives
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Aug 20, 2021
1 parent 6ac6e1b commit 776e458
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/emit-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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);
```
30 changes: 30 additions & 0 deletions website/versioned_docs/version-1.0.0/emit-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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);
```

0 comments on commit 776e458

Please sign in to comment.