Skip to content

Commit

Permalink
fix: prevent double-wrapping of field.resolve (#1773)
Browse files Browse the repository at this point in the history
* fix: prevent double-wrapping of field.resolve fixes #1772

* fix: useOnResolve double-wrap prevention should be on field not field.resolve
  • Loading branch information
jonapgar-groupby authored May 22, 2023
1 parent a36925c commit 7066ce9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eighty-ties-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envelop/on-resolve': patch
---

Prevent re-wrapping field resolvers with useOnResolve plugin. Fixes #1773
5 changes: 5 additions & 0 deletions packages/plugins/on-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type OnResolve<PluginContext extends Record<string, any> = {}> = (
export function useOnResolve<PluginContext extends Record<string, any> = {}>(
onResolve: OnResolve<PluginContext>,
): Plugin<PluginContext> {
const hasWrappedResolveSymbol = Symbol('hasWrappedResolve');
return {
onSchemaChange({ schema: _schema }) {
const schema = _schema as GraphQLSchema;
Expand All @@ -48,6 +49,8 @@ export function useOnResolve<PluginContext extends Record<string, any> = {}>(
for (const type of Object.values(schema.getTypeMap())) {
if (!isIntrospectionType(type) && isObjectType(type)) {
for (const field of Object.values(type.getFields())) {
if (field[hasWrappedResolveSymbol]) continue;

let resolver = (field.resolve || defaultFieldResolver) as Resolver<PluginContext>;

field.resolve = async (root, args, context, info) => {
Expand Down Expand Up @@ -83,6 +86,8 @@ export function useOnResolve<PluginContext extends Record<string, any> = {}>(
}
return result;
};

field[hasWrappedResolveSymbol] = true;
}
}
}
Expand Down
29 changes: 28 additions & 1 deletion packages/plugins/on-resolve/test/use-on-resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OnResolveOptions, useOnResolve } from '@envelop/on-resolve';
import { AfterResolver, OnResolveOptions, useOnResolve } from '@envelop/on-resolve';
import { assertSingleExecutionValue, createTestkit } from '@envelop/testing';
import { makeExecutableSchema } from '@graphql-tools/schema';

Expand Down Expand Up @@ -60,4 +60,31 @@ describe('useOnResolve', () => {

expect(result.data?.value1).toBe('value2');
});

it('should only execute the onResolve function once after the schema has been replaced', async () => {
const afterResolve: AfterResolver = jest.fn(({ setResult }) => {
setResult('value2');
});
const testkit = createTestkit(
[
useOnResolve(() => afterResolve),
// This _should_ trigger another afterResolve call
useOnResolve(() => afterResolve),
// This should _NOT_ trigger another afterResolve call
{
onSchemaChange({ schema, replaceSchema }) {
replaceSchema(schema);
},
},
],
schema,
);

const result = await testkit.execute('{ value1 }');
// Expect two calls, not four.
expect(afterResolve).toBeCalledTimes(2);

assertSingleExecutionValue(result);
expect(result.data?.value1).toBe('value2');
});
});

0 comments on commit 7066ce9

Please sign in to comment.