Shopify / quilt

[⚠️ Deprecated] A loosely related set of packages for JavaScript/TypeScript projects at Shopify
MIT License
1.7k stars 220 forks source link

[graphql-fixtures] Seed collision for nested arrays on fill #2311

Open Flufd opened 2 years ago

Flufd commented 2 years ago

Overview

When using the graphQL filler to generate data with nested arrays, a faker seed collision occurs when generating the data for the elements in the arrays.

This is because the seedFromKeypath generates a seed from the keypath of the node that is being filled, based on the collective sum of all of the characters that make up that keypath.

Example: Given this schema:

type Pet {
  id: ID!
}

type Person {
  pets: [Pet]!
}

type Query {
  people: [Person!]!
}

If we have two people with two pets each, the pets at person[0].pet[1] and person[1].pet[0] have the same seed generated, because their "sum" of the chars in the keypath is the same. Because of this, their IDs are the same.

Failing test here: https://github.com/Shopify/quilt/compare/graphql-fixtures/seed-collision

This becomes a problematic issue when combined with the Apollo cache. By default, Apollo will normalise the items based on their id fields and so we get a cache collision and the test data we receive in a call to the Apollo client does not match the data we passed to the createGraphQLFactory result.

Potentially we could use some other stable method for generating the seed based on the keypath. Maybe some quick hash of the string? https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0

BPScott commented 2 years ago

This crops up with a single entity too, you don't even need nesting for this to fail. These key paths will result in the same seed: person[12] and person[21] (and thus the 12th and 21st person will be the same)