diff --git a/.changeset/fresh-goats-rule.md b/.changeset/fresh-goats-rule.md new file mode 100644 index 0000000000..15c4ffbad8 --- /dev/null +++ b/.changeset/fresh-goats-rule.md @@ -0,0 +1,5 @@ +--- +'graphql-fixtures': patch +--- + +Removes calls to set faker seed if it's not needed diff --git a/packages/graphql-fixtures/src/fill.ts b/packages/graphql-fixtures/src/fill.ts index 844262a944..a579383918 100644 --- a/packages/graphql-fixtures/src/fill.ts +++ b/packages/graphql-fixtures/src/fill.ts @@ -379,6 +379,16 @@ function createValue( request: Request, details: ResolveDetails, ) { + // If the partial fill value is a primitive type, + // we can just return it directly and avoid reseeding the faker instance + if ( + typeof partialValue === 'string' || + typeof partialValue === 'boolean' || + typeof partialValue === 'number' + ) { + return partialValue; + } + return withRandom( request, details.parentFields, diff --git a/packages/graphql-fixtures/src/tests/fill.test.ts b/packages/graphql-fixtures/src/tests/fill.test.ts index dcc96c80d3..2512d92cab 100644 --- a/packages/graphql-fixtures/src/tests/fill.test.ts +++ b/packages/graphql-fixtures/src/tests/fill.test.ts @@ -22,6 +22,10 @@ describe('createFillers()', () => { chooseNull.mockReset(); }); + afterEach(() => { + jest.restoreAllMocks(); + }); + describe('fillOperation', () => { it('fills string fields', () => { const {fillOperation} = createFillerForSchema(` @@ -348,9 +352,27 @@ describe('createFillers()', () => { parents: [Person!]! } + enum CountryCode { + CA + US + GB + } + + enum CurrencyCode { + CAD + USD + GBP + } + + type LocalizationCollection { + countryCodes: [CountryCode!]! + currencyCodes: [CurrencyCode!]! + } + type Query { self: Person! sibling: Person + localizations: [LocalizationCollection!]! } `); @@ -426,6 +448,30 @@ describe('createFillers()', () => { data.self.parents[1].name, ); }); + + it('does not excessively seed the faker instance for prefilled data', () => { + const spy = jest.spyOn(faker, 'seed'); + + const document = createDocument<{ + self: {parents: {name: string}[]}; + }>(` + query SeedCheck { + localizations { + countryCodes + currencyCodes + } + } + `); + + const data = fillOperation(document, { + localizations: Array.from({length: 20}).map(() => ({ + countryCodes: Array.from({length: 250}).map(() => 'CA'), + currencyCodes: Array.from({length: 150}).map(() => 'CAD'), + })), + }); + + expect(spy.mock.calls.length).toBeLessThan(100); + }); }); describe('objects', () => {