Skip to content

Commit 160bd3d

Browse files
docs(): update graphql section to match v10
1 parent 3d3368a commit 160bd3d

File tree

14 files changed

+998
-159
lines changed

14 files changed

+998
-159
lines changed

content/graphql/directives.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,49 @@ A directive is an identifier preceded by a `@` character, optionally followed by
1010

1111
#### Custom directives
1212

13-
To create a custom schema directive, declare a class which extends the `SchemaDirectiveVisitor` class exported from the `@graphql-tools/utils` package.
13+
To instruct what should happen when Apollo/Mercurius encounters your directive, you can create a transformer function. This function uses the `mapSchema` function to iterate through locations in your schema (field definitions, type definitions, etc.) and perform corresponding transformations.
1414

1515
```typescript
16-
import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
17-
import { defaultFieldResolver, GraphQLField } from 'graphql';
18-
19-
export class UpperCaseDirective extends SchemaDirectiveVisitor {
20-
visitFieldDefinition(field: GraphQLField<any, any>) {
21-
const { resolve = defaultFieldResolver } = field;
22-
field.resolve = async function(...args) {
23-
const result = await resolve.apply(this, args);
24-
if (typeof result === 'string') {
25-
return result.toUpperCase();
16+
import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils';
17+
import { defaultFieldResolver, GraphQLSchema } from 'graphql';
18+
19+
export function upperDirectiveTransformer(
20+
schema: GraphQLSchema,
21+
directiveName: string,
22+
) {
23+
return mapSchema(schema, {
24+
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
25+
const upperDirective = getDirective(
26+
schema,
27+
fieldConfig,
28+
directiveName,
29+
)?.[0];
30+
31+
if (upperDirective) {
32+
const { resolve = defaultFieldResolver } = fieldConfig;
33+
34+
// Replace the original resolver with a function that *first* calls
35+
// the original resolver, then converts its result to upper case
36+
fieldConfig.resolve = async function (source, args, context, info) {
37+
const result = await resolve(source, args, context, info);
38+
if (typeof result === 'string') {
39+
return result.toUpperCase();
40+
}
41+
return result;
42+
};
43+
return fieldConfig;
2644
}
27-
return result;
28-
};
29-
}
45+
},
46+
});
3047
}
3148
```
3249

33-
> info **Hint** Note that directives cannot be decorated with the `@Injectable()` decorator. Thus, they are not able to inject dependencies.
34-
35-
> warning **Warning** `SchemaDirectiveVisitor` is exported from the `@graphql-tools/utils` package. Note that the 8.x release of `graphql-tools` removes this export and provides a different and incompatible approach to directives, so make sure to install `@graphql-tools/utils@^7` in your project.
36-
37-
Now, register the `UpperCaseDirective` in the `GraphQLModule.forRoot()` method:
50+
Now, apply the `upperDirectiveTransformer` transformation function in the `GraphQLModule#forRoot` method using the `transformSchema` function:
3851

3952
```typescript
4053
GraphQLModule.forRoot({
4154
// ...
42-
schemaDirectives: {
43-
upper: UpperCaseDirective,
44-
},
55+
transformSchema: (schema) => upperDirectiveTransformer(schema, 'upper'),
4556
});
4657
```
4758

0 commit comments

Comments
 (0)