Despite spread the fragment in the parent that is passing the ref to the function, you get a runtime error when attempting to use readInlineData on a Fragment that uses the plural: true directive such as:
function filter(itemsRef) {
const items = readInlineData(
graphql`
fragment ItemList_items on Item @relay(plural: true) {
name
}
`,
itemsRef
);
// process
}
Error:
readInlineData(): Expected fragment `ItemList_items` to be spread in the parent fragment.
I also tried mapping with readInlineData, but that didn't work either:
function filter(itemsRef) {
const items = itemsRef.map(i => readInlineData(
graphql`
fragment ItemList_items on Item @relay(plural: true) {
name
}
`,
i
);
// process
}
Presumably readInlineData is expecting an object that contains a specific fragment ref key rather than an array of fragments.
We're hitting this too, but I wonder if the answer here isn't just "don't use @relay(plural)"? The plural annotation seems like it'd mostly just be a signal to the fragment container to expect an array.
But in this case, couldn't you just make the fragment non-plural, then iterate over each instance?
I hit the same issue. It should work if you iterate over the array (as per your second code snippet) if you remove @relay(plural: true) from the fragment.
const items = itemsRef.map(i => readInlineData(
graphql`
fragment ItemList_items on Item {
name
}
`,
i
);
The other way I've worked around this issue is by passing in a connection rather than a raw array, for example:
const items = readInlineData(
graphql`
fragment ItemList_items on ItemConnection {
nodes {
name
}
}
`,
itemsRef,
);
// use items.nodes
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
We're hitting this too, but I wonder if the answer here isn't just "don't use
@relay(plural)"? The plural annotation seems like it'd mostly just be a signal to the fragment container to expect an array.But in this case, couldn't you just make the fragment non-plural, then iterate over each instance?