Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return early in GraphQL fill for primitive partial #2701

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-goats-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-fixtures': patch
---

Removes calls to set faker seed if it's not needed
10 changes: 10 additions & 0 deletions packages/graphql-fixtures/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ function createValue<Request, T>(
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,
Expand Down
46 changes: 46 additions & 0 deletions packages/graphql-fixtures/src/tests/fill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('createFillers()', () => {
chooseNull.mockReset();
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('fillOperation', () => {
it('fills string fields', () => {
const {fillOperation} = createFillerForSchema(`
Expand Down Expand Up @@ -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!]!
}
`);

Expand Down Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, the seed is reset 8000+ times, with the change it is ~60

});
});

describe('objects', () => {
Expand Down