Skip to content

Commit bab28cc

Browse files
Merge pull request #2093 from alitnk/master
docs(graphql): `interface resolvers` section
2 parents c7ffaee + 9bcaa9b commit bab28cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

content/graphql/interfaces.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ export abstract class Book {
6666
}
6767
```
6868

69+
#### Interface resolvers
70+
71+
So far, using interfaces, you could only share field definitions with your objects. If you also want to share the actual field resolvers implementation, you can create a dedicated interface resolver, as follows:
72+
73+
```typescript
74+
import { Resolver, ResolveField, Parent, Info } from '@nestjs/graphql';
75+
76+
@Resolver(type => Character) // Reminder: Character is an interface
77+
export class CharacterInterfaceResolver {
78+
@ResolveField(() => [Character])
79+
friends(
80+
@Parent() character, // Resolved object that implements Character
81+
@Info() { parentType }, // Type of the object that implements Character
82+
@Args('search', { type: () => String }) searchTerm: string,
83+
) {
84+
// Get character's friends
85+
return [];
86+
}
87+
}
88+
```
89+
90+
Now the `friends` field resolver is auto-registered for all object types that implement the `Character` interface.
91+
6992
#### Schema first
7093

7194
To define an interface in the schema first approach, simply create a GraphQL interface with SDL.

0 commit comments

Comments
 (0)