Skip to content

Commit 71942f4

Browse files
authored
fix(schema): Ensure that resolveResult and resolveExternal are run as around hooks (#3032)
1 parent 1ac18a7 commit 71942f4

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

docs/api/schema/resolvers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ app.service('users').hooks({
315315

316316
<BlockQuote type="warning" label="important">
317317

318-
In order to get the safe data from resolved associations **all services** involved need the `schemaHooks.resolveExternal` (or `resolveAll`) hook registered even if it does not need a resolver (`schemaHooks.resolveExternal()`).
318+
In order to get the safe data from resolved associations **all services** involved need the `schemaHooks.resolveExternal` hook registered even if it does not need a resolver (`schemaHooks.resolveExternal()`).
319319

320320
`schemaHooks.resolveExternal` should be registered first when used as an `around` hook or last when used as an `after` hook so that it gets the final result data.
321321

packages/schema/src/hooks/resolve.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,30 @@ export const resolveData =
8181
export const resolveResult = <H extends HookContext>(...resolvers: Resolver<any, H>[]) => {
8282
const virtualProperties = new Set(resolvers.reduce((acc, current) => acc.concat(current.virtualNames), []))
8383

84-
return async (context: H, next?: NextFunction) => {
85-
if (typeof next === 'function') {
86-
const { $resolve, $select: select, ...query } = context.params?.query || {}
87-
const $select = Array.isArray(select) ? select.filter((name) => !virtualProperties.has(name)) : select
88-
const resolve = {
89-
originalContext: context,
90-
...context.params.resolve,
91-
properties: $resolve || select
92-
}
84+
return async (context: H, next: NextFunction) => {
85+
if (typeof next !== 'function') {
86+
throw new Error('The resolveResult hook must be used as an around hook')
87+
}
9388

94-
context.params = {
95-
...context.params,
96-
resolve,
97-
query: {
98-
...query,
99-
...($select ? { $select } : {})
100-
}
101-
}
89+
const { $resolve, $select: select, ...query } = context.params?.query || {}
90+
const $select = Array.isArray(select) ? select.filter((name) => !virtualProperties.has(name)) : select
91+
const resolve = {
92+
originalContext: context,
93+
...context.params.resolve,
94+
properties: $resolve || select
95+
}
10296

103-
await next()
97+
context.params = {
98+
...context.params,
99+
resolve,
100+
query: {
101+
...query,
102+
...($select ? { $select } : {})
103+
}
104104
}
105105

106+
await next()
107+
106108
const ctx = getContext(context)
107109
const status = context.params.resolve
108110
const { isPaginated, data } = getResult(context)
@@ -148,13 +150,15 @@ export const setDispatch = (current: any, dispatch: any) => {
148150
return dispatch
149151
}
150152

151-
export const resolveDispatch =
153+
export const resolveExternal =
152154
<H extends HookContext>(...resolvers: Resolver<any, H>[]) =>
153-
async (context: H, next?: NextFunction) => {
154-
if (typeof next === 'function') {
155-
await next()
155+
async (context: H, next: NextFunction) => {
156+
if (typeof next !== 'function') {
157+
throw new Error('The resolveExternal hook must be used as an around hook')
156158
}
157159

160+
await next()
161+
158162
const ctx = getContext(context)
159163
const existingDispatch = getDispatch(context.result)
160164

@@ -188,7 +192,7 @@ export const resolveDispatch =
188192
}
189193
}
190194

191-
export const resolveExternal = resolveDispatch
195+
export const resolveDispatch = resolveExternal
192196

193197
type ResolveAllSettings<H extends HookContext> = {
194198
data?: {

0 commit comments

Comments
 (0)