Skip to content

Commit a2e458e

Browse files
Geoffroy Empaingempain
authored andcommitted
docs(graphql): document fieldResolverEnhancers
1 parent 5653d3c commit a2e458e

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+
#### Interceptors at the `@ResoleField()` method level
90+
91+
In a GraphQL context, interceptors [do not access the full GraphQL response](https://github.com/nestjs/graphql/issues/320#issuecomment-511193229): they only get the response of the `@Query()`/`@Mutation()` method, but not the complete response with the resolved fields. You can tell Nest to execute interceptors (as well as guards and filters) for methods annotated `@ResolveField()` by setting the `fieldResolverEnhancers` option in `GqlModuleOptions`:
92+
93+
```typescript
94+
GraphQLModule.forRoot({
95+
fieldResolverEnhancers: ['interceptors']
96+
}),
97+
```
98+
99+
> **Warning** Enabling interceptors 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 interceptors that are not strictly necessary for your field resolvers. You can do this using the following helper method:
100+
101+
```typescript
102+
export function isResolvingGraphQLField(context: ExecutionContext): boolean {
103+
if (context.getType().toString() === '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)