Closed chrisapplegate closed 4 years ago
Incidentally, I wrote a hacky shim to override denormalize()
that seems to work:
class DenormalizableResource extends Resource {
static denormalize(entity, unvisit) {
Object.keys(this.schema).forEach((key) => {
if (key in entity) {
entity[key] = unvisit(entity[key], this.schema[key])[0];
}
});
return [entity, true];
}
}
I suspect it might have effects with infinite loops if the child Entity in turn contains a reference to the parent, but it works for my schema types
This should work:
static denormalize<T extends typeof Entity>(
this: T,
entity: AbstractInstanceType<T> | undefined,
unvisit: schemas.UnvisitFunction,
): [AbstractInstanceType<T>, true] {
let found = true;
Object.keys(this.schema).forEach(key => {
const schema = this.schema[key];
const [value, foundItem] = unvisit(entity[key], schema);
if (!foundItem) {
found = false;
}
if (Object.hasOwnProperty.call(entity, key)) {
entity[key] = value;
}
});
return [entity, found];
}
This will be incorporated soon, but there's some typing finesse I need to work out, which is why I haven't landed it yet. I don't want to get it wrong and then have to issue an extra breaking release :p
Basically yours is mostly there, but missing the 'found' logic, which is important for results inferring.
Thanks! I dropped it in, tweaked the types a little bit and got this to work:
static denormalize<T extends typeof Entity>(
this: T,
entity: AbstractInstanceType<T>,
unvisit: schemas.UnvisitFunction,
): [AbstractInstanceType<T>, true] {
let found = true;
Object.keys(this.schema).forEach(key => {
const schema = this.schema[key];
const [value, foundItem] = unvisit(entity[key as keyof T["prototype"]], schema);
if (!foundItem) {
found = false;
}
if (Object.hasOwnProperty.call(entity, key)) {
entity[key as keyof T["prototype"]] = value;
}
});
return [entity, found];
}
React version (e.g., 16.8.5)
16.12
Concurrent mode yes/no
No
Describe the bug
Trying to take advantage of the nesting & normalization features detailed here, however it seems that while data is normalized correctly, it is not denormalized.
To Reproduce
if I have two resources set up as e.g. a
PostResource
and anAuthorResource
:And I then fetch
PostResource
using:const posts = useResource(PostResource.listShape(), {})
, I note that in the Redux store I am using, thePostResource
andAuthorResource
objects are correctly normalized and stored under different stoage paths. But when retrieving thePostResource
, theAuthorResource
is not correctly denormalized and put back into the object. I will just get something like e.g.:I thought I was doing something wrong until I looked at the source code and found in Entity.ts a
TODO: Add denormalizing capability
comment on thedenormalize
static function.Expected behavior
Expect the returned object to be something like:
Additional context
Should I just not bother using normalizing or specifying a schema for nested objects until this is TODO fixed? If so can we get an update to the documentation saying denormalizing is not currently supported?