Skip to content

Commit

Permalink
Merge pull request nestjs#2093 from alitnk/master
Browse files Browse the repository at this point in the history
docs(graphql): `interface resolvers` section
  • Loading branch information
kamilmysliwiec authored Apr 7, 2022
2 parents c7ffaee + 9bcaa9b commit bab28cc
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions content/graphql/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ export abstract class Book {
}
```

#### Interface resolvers

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:

```typescript
import { Resolver, ResolveField, Parent, Info } from '@nestjs/graphql';

@Resolver(type => Character) // Reminder: Character is an interface
export class CharacterInterfaceResolver {
@ResolveField(() => [Character])
friends(
@Parent() character, // Resolved object that implements Character
@Info() { parentType }, // Type of the object that implements Character
@Args('search', { type: () => String }) searchTerm: string,
) {
// Get character's friends
return [];
}
}
```

Now the `friends` field resolver is auto-registered for all object types that implement the `Character` interface.

#### Schema first

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

0 comments on commit bab28cc

Please sign in to comment.