Skip to content

Commit 831fbea

Browse files
committed
Improve directives docs
1 parent 7e546d9 commit 831fbea

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/directives.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,31 @@ Besides declaring the usage of directives, you also have to register the runtime
8686

8787
> Be aware that TypeGraphQL doesn't have any special way for implementing schema directives. You should use some [3rd party libraries](https://the-guild.dev/graphql/tools/docs/schema-directives#implementing-schema-directives) depending on the tool set you use in your project, e.g. `@graphql-tools/*` or `ApolloServer`.
8888
89+
If you write your custom GraphQL directive or import a package that exports a `GraphQLDirective` instance, you need to register the directives definitions in the `buildSchema` options:
90+
91+
```ts
92+
// Build TypeGraphQL executable schema
93+
const tempSchema = await buildSchema({
94+
resolvers: [SampleResolver],
95+
// Register the directives definitions
96+
directives: [myDirective],
97+
});
98+
```
99+
100+
Then you need to apply the schema transformer for your directive, that implements the desired logic of your directive:
101+
102+
```ts
103+
// Transform and obtain the final schema
104+
const schema = myDirectiveTransformer(tempSchema);
105+
```
106+
107+
If the directive package used by you exports a string-based `typeDefs`, you need to add those typedefs to the schema and then apply directive transformer.
108+
89109
Here is an example using the [`@graphql-tools/*`](https://the-guild.dev/graphql/tools):
90110

91111
```ts
92112
import { mergeSchemas } from "@graphql-tools/schema";
113+
import { renameDirective } from "fake-rename-directive-package";
93114

94115
// Build TypeGraphQL executable schema
95116
const schemaSimple = await buildSchema({
@@ -99,9 +120,10 @@ const schemaSimple = await buildSchema({
99120
// Merge schema with sample directive type definitions
100121
const schemaMerged = mergeSchemas({
101122
schemas: [schemaSimple],
102-
typeDefs: [sampleDirective.typeDefs],
123+
// Register the directives definitions
124+
typeDefs: [renameDirective.typeDefs],
103125
});
104126

105127
// Transform and obtain the final schema
106-
const schema = sampleDirective.transformer(schemaMerged);
128+
const schema = renameDirective.transformer(schemaMerged);
107129
```

0 commit comments

Comments
 (0)