Skip to content

Commit e7177b5

Browse files
Merge pull request #1144 from gempain/patch-1
docs(graphql): document fieldResolverEnhancers
2 parents 9222237 + 9aab6d5 commit e7177b5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

content/graphql/guards-interceptors.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,27 @@ async upvotePost(
8585
```
8686

8787
> info **Hint** In the above example, we have assumed that the `user` object is assigned to the context of your GraphQL application.
88+
89+
#### Execute enhancers at the field resolver level
90+
91+
In the GraphQL context, Nest does not run **enhancers** (the generic name for interceptors, guards and filters) at the field level [see this issue](https://github.com/nestjs/graphql/issues/320#issuecomment-511193229): they only run for the top level `@Query()`/`@Mutation()` method. You can tell Nest to execute interceptors, guards or filters for methods annotated with `@ResolveField()` by setting the `fieldResolverEnhancers` option in `GqlModuleOptions`. Pass it a list of `'interceptors'`, `'guards'`, and/or `'filters'` as appropriate:
92+
93+
```typescript
94+
GraphQLModule.forRoot({
95+
fieldResolverEnhancers: ['interceptors']
96+
}),
97+
```
98+
99+
> **Warning** Enabling enhancers for field resolvers can cause performance issues when you are returning lots of records and your field resolver is executed thousands of times. For this reason, when you enable `fieldResolverEnhancers`, we advise you to skip execution of enhancers that are not strictly necessary for your field resolvers. You can do this using the following helper function:
100+
101+
```typescript
102+
export function isResolvingGraphQLField(context: ExecutionContext): boolean {
103+
if (context.getType<GqlContextType>() === 'graphql') {
104+
const gqlContext = GqlExecutionContext.create(context);
105+
const info = gqlContext.getInfo();
106+
const parentType = info.parentType.name;
107+
return parentType !== 'Query' && parentType !== 'Mutation';
108+
}
109+
return false;
110+
}
111+
```

0 commit comments

Comments
 (0)