Skip to content

Commit

Permalink
perf: only clone schema once in addMocksToSchema (#6201)
Browse files Browse the repository at this point in the history
* perf: only clone schema once in `addMocksToSchema`

* chore: add changeset
  • Loading branch information
grxy committed May 21, 2024
1 parent d0da3f7 commit 9d79b3e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-items-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/mock': patch
---

perf: only clone schema once in `addMocksToSchema`
2 changes: 2 additions & 0 deletions packages/mock/src/addMocksToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ export function addMocksToSchema<TResolvers = IResolvers>({
? addResolversToSchema({
schema: schemaWithMocks,
resolvers: resolvers as any,
// This option ensures that schemas are not cloned multiple times, which can be very expensive
updateResolversInPlace: true,
})
: schemaWithMocks;
}
21 changes: 21 additions & 0 deletions packages/mock/tests/addMocksToSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,25 @@ describe('addMocksToSchema', () => {

expect(viewer.name).toEqual('custom mock for String');
});

it('creates a new schema whether or not resolvers are passed in', () => {
expect(
Object.is(
addMocksToSchema({
schema,
resolvers: {},
}),
schema,
),
).toBe(false);

expect(
Object.is(
addMocksToSchema({
schema,
}),
schema,
),
).toBe(false);
});
});

0 comments on commit 9d79b3e

Please sign in to comment.