Skip to content

Commit

Permalink
Return early in graphql fill for primitive partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Flufd committed Dec 7, 2023
1 parent edf7a0a commit 75b01ee
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/graphql-fixtures/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 4.0.1 - 2022-12-07

### Changed

- Removes calls to set faker seed if it's not needed [[#2701]](https://github.com/Shopify/quilt/pull/2701)

## 4.0.0

### Major Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-fixtures/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-fixtures",
"version": "4.0.0",
"version": "4.0.1",
"license": "MIT",
"description": "Utilities for generating fixture objects from GraphQL documents.",
"main": "index.js",
Expand Down
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);
});
});

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

0 comments on commit 75b01ee

Please sign in to comment.