Skip to content

Commit

Permalink
feat(schema): Split resolver options and property resolvers (#2889)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Nov 24, 2022
1 parent 59f3cdc commit 4822c94
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 218 deletions.
65 changes: 42 additions & 23 deletions docs/api/schema/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ class MyContext {
}

const messageResolver = resolve<Message, MyContext>({
properties: {
likes: async (value, message, context) => {
return context.getLikes(message.id)
},
user: async (value, message, context) => {
return context.getUser(message.userId)
}
likes: async (value, message, context) => {
return context.getLikes(message.id)
},
user: async (value, message, context) => {
return context.getUser(message.userId)
}
})

Expand All @@ -72,16 +70,9 @@ const resolvedMessage = await messageResolver.resolve(
)
```

## Options

A resolver takes the following options:

- `properties`: An object of property names and their [resolver functions](#property-resolvers)
- `converter` (optional): A `async (data, context) => {}` function that can return a completely new representation of the data. A `converter` runs before `properties` resolvers.

## Property resolvers

A resolver function is an `async` function that resolves a property on a data object. If it returns `undefined` the property will not be included. It gets passed the following parameters:
Property resolvers are a map of property names to resolver functions. A resolver function is an `async` function that resolves a property on a data object. If it returns `undefined` the property will not be included. It gets passed the following parameters:

- `value` - The current value which can also be `undefined`
- `data` - The initial data object
Expand All @@ -90,15 +81,13 @@ A resolver function is an `async` function that resolves a property on a data ob

```ts
const userResolver = resolve<User, MyContext>({
properties: {
isDrinkingAge: async (value, user, context) => {
const drinkingAge = await context.getDrinkingAge(user.country)
isDrinkingAge: async (value, user, context) => {
const drinkingAge = await context.getDrinkingAge(user.country)

return user.age >= drinkingAge
},
fullName: async (value, user, context) => {
return `${user.firstName} ${user.lastName}`
}
return user.age >= drinkingAge
},
fullName: async (value, user, context) => {
return `${user.firstName} ${user.lastName}`
}
})
```
Expand All @@ -109,6 +98,36 @@ Property resolver functions should only return a value and not have side effects

</BlockQuote>

## Options

A resolver takes the following options as the second parameter:

- `converter` (optional): A `async (data, context) => {}` function that can return a completely new representation of the data. A `converter` runs before `properties` resolvers.

```ts
const userResolver = resolve<User, MyContext>(
{
isDrinkingAge: async (value, user, context) => {
const drinkingAge = await context.getDrinkingAge(user.country)

return user.age >= drinkingAge
},
fullName: async (value, user, context) => {
return `${user.firstName} ${user.lastName}`
}
},
{
// Convert the raw data into a new structure before running property resolvers
converter: async (rawData, context) => {
return {
firstName: rawData.data.first_name,
lastName: rawData.data.last_name
}
}
}
)
```

## Hooks

In a Feathers application, resolvers are used through [hooks](../hooks.md) to convert service method query, data and responses. The context for these resolvers is always the [hook context](../hooks.md#hook-context).
Expand Down
Loading

0 comments on commit 4822c94

Please # to comment.