Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
12 changes: 6 additions & 6 deletions packages/graphql-fixtures/src/fill.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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