diff --git a/packages/schema/src/hooks/resolve.ts b/packages/schema/src/hooks/resolve.ts index e50b43200e..b61bbcb110 100644 --- a/packages/schema/src/hooks/resolve.ts +++ b/packages/schema/src/hooks/resolve.ts @@ -157,6 +157,12 @@ export const resolveExternal = const status = context.params.resolve const { isPaginated, data } = getResult(context) const resolveAndGetDispatch = async (current: any) => { + const currentExistingDispatch = getDispatch(current) + + if (currentExistingDispatch !== null) { + return currentExistingDispatch + } + const resolved = await runResolvers(resolvers, current, context, status) const currentDispatch = Object.keys(resolved).reduce( (res, key) => { diff --git a/packages/schema/test/fixture.ts b/packages/schema/test/fixture.ts index 38f3387843..4456d6aad5 100644 --- a/packages/schema/test/fixture.ts +++ b/packages/schema/test/fixture.ts @@ -17,7 +17,8 @@ import { FromSchema, getValidator, getDataValidator, - virtual + virtual, + resolveExternal } from '../src' const fixtureAjv = new Ajv({ @@ -198,6 +199,13 @@ class MessageService extends MemoryService } } +const findResult = { message: 'Hello' } +class CustomService { + async find() { + return [findResult] + } +} + const customMethodDataResolver = resolve>({ properties: { userId: async () => 0, @@ -209,6 +217,7 @@ type ServiceTypes = { users: MemoryService messages: MessageService paginatedMessages: MemoryService + custom: CustomService } type Application = FeathersApplication @@ -223,6 +232,14 @@ app.use( app.use('messages', new MessageService(), { methods: ['find', 'get', 'create', 'update', 'patch', 'remove', 'customMethod'] }) +app.use('custom', new CustomService()) + +app.service('custom').hooks({ + around: { + all: [resolveExternal(resolve>({}))] + } +}) + app.use('paginatedMessages', memory({ paginate: { default: 10 } })) app.service('messages').hooks({ diff --git a/packages/schema/test/hooks.test.ts b/packages/schema/test/hooks.test.ts index afe1a68eb1..b7a5b1c8a0 100644 --- a/packages/schema/test/hooks.test.ts +++ b/packages/schema/test/hooks.test.ts @@ -187,6 +187,13 @@ describe('@feathersjs/schema/hooks', () => { }) }) + it('resolves safe dispatch with static data', async () => { + const service = app.service('custom') + + await service.find() + assert.deepStrictEqual(await service.find(), [{ message: 'Hello' }]) + }) + it('resolves data for custom methods', async () => { const result = await app.service('messages').customMethod({ message: 'Hello' }) const user = {