Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuprog committed Mar 15, 2022
1 parent 22c143c commit 52f224e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
9 changes: 3 additions & 6 deletions src/store/middleware/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ function doNormalize(store, object, getObjectFromStore, callbacks, newEntities,

object = { ...object };

// do not use isEntity() as we might have only a __typename
if (object.id && object.__typename) {
if (isEntity(object)) {
const transformResolvers = store.config.transformers[object.__typename]?.data;
if (transformResolvers) {
object = transform(object, transformResolvers);
Expand All @@ -71,8 +70,7 @@ function doNormalize(store, object, getObjectFromStore, callbacks, newEntities,
continue;
}

// do not use isEntity() as we might have only a __typename
if (propValue && propValue.id && propValue.__typename) {
if (isEntity(propValue)) {
let entity = propValue; // renaming for readability

doNormalize(store, entity, () => store.getEntityById(entity.id), callbacks, newEntities, updates);
Expand Down Expand Up @@ -127,8 +125,7 @@ function doNormalize(store, object, getObjectFromStore, callbacks, newEntities,
normalizedObject[propName] = propValue;
}

// do not use isEntity() as we might have only a __typename
if (normalizedObject.id && normalizedObject.__typename) {
if (isEntity(normalizedObject)) {
const normalizedEntity = normalizedObject;
const isNewEntity = !newEntities.byId[normalizedEntity.id];

Expand Down
17 changes: 6 additions & 11 deletions src/store/middleware/proxifyReferences.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
hasObjectProps,
isArray,
isArrayOfEntities,
isArrayOfObjectLiterals,
isEmptyArray,
isEntity,
isObjectLiteral
} from '../../utils';
import createProxy from '../createProxy';
Expand All @@ -23,8 +25,7 @@ async function doProxifyReferences(data, entity, store, callbacks) {
if (isObjectLiteral(data)) {
let object = { ...data };

// do not use isEntity() as we might have an array of ids
if (object.id && object.__typename) {
if (isEntity(object)) {
entity = object;
}

Expand All @@ -51,15 +52,9 @@ async function doProxifyReferences(data, entity, store, callbacks) {
let config = getConfigForField(propName);
if (config && !object[config.reference]) {
if (isArray(propValue)) {
const config = getConfigForField(propName);
if (config && !object[config.reference]) {
object[config.reference] = propValue.map(({ id }) => id);
}
object[config.reference] = propValue.map(({ id }) => id);
} else {
const config = getConfigForField(propName);
if (config && !object[config.reference]) {
object[config.reference] = propValue?.id || null;
}
object[config.reference] = propValue?.id || null;
}
}

Expand All @@ -70,7 +65,7 @@ async function doProxifyReferences(data, entity, store, callbacks) {
if (field && !object[field]) {
object[field] = [];
}
} else if (isArray(propValue) && propValue.length > 0 && propValue[0].__typename) {
} else if (isArrayOfEntities(propValue)) {
object[propName] = await doProxifyReferences(propValue, entity, store, callbacks);
} else if (isArrayOfObjectLiterals(propValue)) {
if (propValue.length > 0) {
Expand Down
8 changes: 3 additions & 5 deletions src/store/middleware/refreshDenormalizedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ function doRefresh(entities, data, updatesToListenTo, nestedEntity = false, getD
return null;
}

// do not use isEntity() as we might have only a __typename
if (data.id && data.__typename && isEntityProxy(data)) {
if (isEntityProxy(data)) {
return data;
}

if (isObjectLiteral(data)) {
let object = { ...data };

// do not use isEntity() as we might have only a __typename
const isEntity_ = object.id && object.__typename;
const isEntity_ = isEntity(object);

if (isEntity_) {
// nested entity, it might have changed ID
Expand All @@ -50,7 +48,7 @@ function doRefresh(entities, data, updatesToListenTo, nestedEntity = false, getD
}

for (let [propName, propValue] of Object.entries(object)) {
object[propName] = doRefresh(entities, propValue, updatesToListenTo, nestedEntity, () => getDataFromStore?.()[propName]);
object[propName] = doRefresh(entities, propValue, updatesToListenTo, nestedEntity, () => getDataFromStore?.()?.[propName]);
}

if (isEntity_) {
Expand Down
22 changes: 22 additions & 0 deletions src/store/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,25 @@ test('store', async () => {

expect(store.getSingleEntity(store.filterEntities({ label: 'foobar' }, entities)).label).toBe('foobar');
});

test('store data having an object containing __typename but no id', async () => {
const success = deepFreeze({
createSuccess: {
__typename: 'CreateSuccess',
person: {
id: 'person2',
__typename: 'Person',
name: 'John'
}
}
});

const entities = { ...store.getEntities() };

await store.store(success);

const newEntities = store.getEntities();

expect(entities['person2']).toBeUndefined();
expect(newEntities['person2'].name).toBe('John');
});
10 changes: 1 addition & 9 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ function unique(array) {
}

function isEntity(o) {
if (isObjectLiteral(o)) {
if (!!o.id !== !!o.__typename) {
throw new Error(`id or __typename not set: ${JSON.stringify(o)}`);
}

return !!(o.id && o.__typename);
}

return false;
return !!(isObjectLiteral(o) && o.id && o.__typename);
}

function isEntityProxy(o) {
Expand Down

0 comments on commit 52f224e

Please sign in to comment.