gqless
gqless copied to clipboard
Add `path` to Normalization API
Given a query for a list of objects:
query {
something {
items(limit: 1) {
name
}
}
}
And we want to normalize an Item to the index by which it appears in items.
normalization: {
identifier(item) {
return item.__typename + index
}
}
One possible way of implementing it, is a second getParent / path argument:
normalization: {
identifier(item, { getParent, path }) {
// path ==
[
{ key: "0", args: null, data: { name: "" } },
{ key: "items", args: { limit: 1 }, data: [{ name: "" }] },
{ key: "something", args: null, data: { items: [{ name: "" }] } },
{ key: null, args: null, data: { something: { items: [{ name: "" }] } } },
];
const { key, args, parent } = getParent(item)
// key == "0"
// args == null
// parent == [{name:""}]
return item.__typename + key // aka the index
}
}
path being an array of the keys / args / data to the root of the response
and getParent being a function which accepts any item, and returns the key/args of the item, along with the parent item (which can be fed back in)