Skip to content

Commit 4822c94

Browse files
authored
feat(schema): Split resolver options and property resolvers (#2889)
1 parent 59f3cdc commit 4822c94

File tree

10 files changed

+184
-218
lines changed

10 files changed

+184
-218
lines changed

docs/api/schema/resolvers.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ class MyContext {
5252
}
5353

5454
const messageResolver = resolve<Message, MyContext>({
55-
properties: {
56-
likes: async (value, message, context) => {
57-
return context.getLikes(message.id)
58-
},
59-
user: async (value, message, context) => {
60-
return context.getUser(message.userId)
61-
}
55+
likes: async (value, message, context) => {
56+
return context.getLikes(message.id)
57+
},
58+
user: async (value, message, context) => {
59+
return context.getUser(message.userId)
6260
}
6361
})
6462

@@ -72,16 +70,9 @@ const resolvedMessage = await messageResolver.resolve(
7270
)
7371
```
7472

75-
## Options
76-
77-
A resolver takes the following options:
78-
79-
- `properties`: An object of property names and their [resolver functions](#property-resolvers)
80-
- `converter` (optional): A `async (data, context) => {}` function that can return a completely new representation of the data. A `converter` runs before `properties` resolvers.
81-
8273
## Property resolvers
8374

84-
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:
75+
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:
8576

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

9182
```ts
9283
const userResolver = resolve<User, MyContext>({
93-
properties: {
94-
isDrinkingAge: async (value, user, context) => {
95-
const drinkingAge = await context.getDrinkingAge(user.country)
84+
isDrinkingAge: async (value, user, context) => {
85+
const drinkingAge = await context.getDrinkingAge(user.country)
9686

97-
return user.age >= drinkingAge
98-
},
99-
fullName: async (value, user, context) => {
100-
return `${user.firstName} ${user.lastName}`
101-
}
87+
return user.age >= drinkingAge
88+
},
89+
fullName: async (value, user, context) => {
90+
return `${user.firstName} ${user.lastName}`
10291
}
10392
})
10493
```
@@ -109,6 +98,36 @@ Property resolver functions should only return a value and not have side effects
10998

11099
</BlockQuote>
111100

101+
## Options
102+
103+
A resolver takes the following options as the second parameter:
104+
105+
- `converter` (optional): A `async (data, context) => {}` function that can return a completely new representation of the data. A `converter` runs before `properties` resolvers.
106+
107+
```ts
108+
const userResolver = resolve<User, MyContext>(
109+
{
110+
isDrinkingAge: async (value, user, context) => {
111+
const drinkingAge = await context.getDrinkingAge(user.country)
112+
113+
return user.age >= drinkingAge
114+
},
115+
fullName: async (value, user, context) => {
116+
return `${user.firstName} ${user.lastName}`
117+
}
118+
},
119+
{
120+
// Convert the raw data into a new structure before running property resolvers
121+
converter: async (rawData, context) => {
122+
return {
123+
firstName: rawData.data.first_name,
124+
lastName: rawData.data.last_name
125+
}
126+
}
127+
}
128+
)
129+
```
130+
112131
## Hooks
113132

114133
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).

0 commit comments

Comments
 (0)