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

[graphql-fixtures] Graphql fixtures/seed collision fix #2312

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-ads-fry.md
@@ -0,0 +1,5 @@
---
'graphql-fixtures': patch
---

Change how seeds are generated to reduce the chance of clashes
12 changes: 6 additions & 6 deletions packages/graphql-fixtures/src/fill.ts
Expand Up @@ -495,14 +495,14 @@ function randomEnumValue(enumType: GraphQLEnumType) {
}

function seedFromKeypath(keypath: string[]) {
return keypath.reduce<number>((sum, key) => sum + seedFromKey(key), 0);
return keypath.reduce<number>((sum, key) => hashCode(key, sum), 0);
}

function seedFromKey(key: string) {
return [...key].reduce<number>(
(sum, character) => sum + character.charCodeAt(0),
0,
);
function hashCode(data: string, initialHash = 0) {
let newHash = initialHash;
for (let i = 0; i < data.length; i++)
newHash = (Math.imul(31, newHash) + data.charCodeAt(i)) | 0;
return newHash;
}

export function list<T = {}, Data = {}, Variables = {}, DeepPartial = {}>(
Expand Down
42 changes: 42 additions & 0 deletions packages/graphql-fixtures/src/tests/fill.test.ts
Expand Up @@ -1324,6 +1324,48 @@ describe('createFiller()', () => {
people: [{name: expect.any(String)}, {name: 'Chris'}],
});
});

it('has no duplicate ids in nested fields', () => {
const fill = createFillerForSchema(`
type Pet {
id: ID!
}

type Person {
pets: [Pet]!
}

type Query {
people: [Person!]!
}
`);

const document = createDocument<
{people: {pets: {id: string}[]}[]},
{people?: {pets?: {id?: string | null}[] | null}[] | null}
>(`
query Details {
people {
pets {
id
}
}
}
`);

const result = fill(document, {
people: Array.from(Array(100)).map(() => ({
pets: Array.from(Array(100)).map(() => ({})),
})),
});

const allIds = result.people
.flatMap((person) => person.pets)
.map((pet) => pet.id);

const allIdSet = new Set(allIds);
expect(allIdSet.size).toBe(allIds.length);
});
});
});

Expand Down