Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/relay-test-utils/RelayMockPayloadGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,14 @@ class RelayMockPayloadGenerator {
if (fieldDefaultValue === null) {
return null;
}
// `fieldPath` above already indexes plural fields by `index`; the prior
// data must be indexed the same way, or the whole array is passed as each
// element's previous value and the list double-nests (`[[item]]`).
const prevFieldData = field.plural
? index != null && Array.isArray(data[applicationName])
? data[applicationName][index]
: null
: data[applicationName];
return this._traverse(
{
selections: field.selections,
Expand All @@ -819,9 +827,9 @@ class RelayMockPayloadGenerator {
args,
},
fieldPath,
typeof data[applicationName] === 'object'
typeof prevFieldData === 'object' && prevFieldData !== null
? // $FlowFixMe[incompatible-variance]
data[applicationName]
prevFieldData
: null,
// $FlowFixMe[incompatible-type]
fieldDefaultValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2201,3 +2201,63 @@ describe("Aliased linked fields without arguments don't overwrite each other", (
);
});
});

describe('plural linked field selected twice at the same path', () => {
// When the same plural linked field is selected once unconditionally and once
// again in a sibling selection (an `@include`/`@skip` condition or an abstract
// inline fragment), the generator used to pass the whole previously-generated
// array down as the prior value for *each* element, so `_traverse` returned
// the array itself and `generateMockList` wrapped it again — producing
// `[[item]]` instead of `[item]`. The normalizer then could not read the leaf
// fields and warned. `disallowWarnings()` at the top of this file turns that
// warning into a failure, and the snapshot pins the flat shape.
test('does not double-nest under an @include condition', () => {
testGeneratedData(
graphql`
query RelayMockPayloadGeneratorTest70Query($showDetails: Boolean!) {
node(id: "my-id") {
... on User {
allPhones {
isVerified
}
... @include(if: $showDetails) {
allPhones {
phoneNumber {
displayNumber
}
}
}
}
}
}
`,
null,
undefined,
{showDetails: true},
);
});

test('does not double-nest across concrete + abstract inline fragments (#5258)', () => {
// Same root cause, but the second selection lives in an *abstract* inline
// fragment (`... on Actor`) rather than an `@include` condition — the exact
// shape reported in facebook/relay#5258.
testGeneratedData(graphql`
query RelayMockPayloadGeneratorTest71Query {
me {
... on User {
allPhones {
isVerified
}
}
... on Actor {
allPhones {
phoneNumber {
displayNumber
}
}
}
}
}
`);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading