Skip to content

Commit

Permalink
cr fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vejrj committed Nov 6, 2024
1 parent 7c856c5 commit 2cf6281
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/supermassive/src/__tests__/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,4 +1320,35 @@ describe.each([
expect.objectContaining({ hookContext: afterHookContext }),
);
});

it('passes async "before" hook context but "after" hook should already receive resolved promise', async () => {
expect.assertions(2);

const query = `
{
film(id: 1) {
title
}
}`;
const beforeHookContext = {
foo: "foo",
};
const afterHookContext = {
bar: "bar",
};
const hooks: ExecutionHooks = {
beforeFieldResolve: jest.fn(async () => beforeHookContext),
afterFieldResolve: jest.fn(() => afterHookContext),
afterFieldComplete: jest.fn(),
};

await execute(parse(query), resolvers as UserResolvers, hooks);

expect(hooks.afterFieldResolve).toHaveBeenCalledWith(
expect.objectContaining({ hookContext: beforeHookContext }),
);
expect(hooks.afterFieldComplete).toHaveBeenCalledWith(
expect.objectContaining({ hookContext: afterHookContext }),
);
});
});
4 changes: 3 additions & 1 deletion packages/supermassive/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export interface BeforeFieldResolveHook<
ResolveContext = unknown,
BeforeHookContext = unknown,
> {
(args: BaseExecuteFieldHookArgs<ResolveContext>): BeforeHookContext;
(args: BaseExecuteFieldHookArgs<ResolveContext>):
| Promise<BeforeHookContext>
| BeforeHookContext;
}

export interface AfterFieldResolveHook<
Expand Down

0 comments on commit 2cf6281

Please sign in to comment.