Fix query data detection

This commit is contained in:
Chi Vinh Le
2018-03-13 23:32:21 +01:00
parent 414b62ff66
commit 73fce8624c
2 changed files with 13 additions and 44 deletions
+1 -34
View File
@@ -24,34 +24,6 @@ const withSkipOnErrors = reducer => (prev, action, ...rest) => {
return reducer(prev, action, ...rest);
};
/**
* attachFromQueryMarkers will add a `__unsafeFromQuery` to objects
* inside data, to allow other parts of the framework to easily detect
* that the data came from a query.
*/
function attachFromQueryMarkers(data) {
if (typeof data === 'object') {
if (data === null) {
return data;
}
if (Array.isArray(data)) {
const result = [...data];
result.forEach((v, k) => {
result[k] = attachFromQueryMarkers(v);
});
return result;
} else {
const result = { ...data };
result.__unsafeFromQuery = true;
Object.keys(result).forEach(key => {
result[key] = attachFromQueryMarkers(result[key]);
});
return result;
}
}
return data;
}
function networkStatusToString(networkStatus) {
switch (networkStatus) {
case 1:
@@ -323,11 +295,6 @@ const createHOC = (document, config, { notifyOnError = true }) =>
const nextData = this.nextData(args.data);
const { root } = separateDataAndRoot(args.data);
// We attach query markes `__fromQuery` to each node in
// the returned `root` result, so the framework can
// easily detect props coming from the query.
const rootWithQueryMarkers = attachFromQueryMarkers(root);
if (config.props) {
// Custom props, in this case we just pass the wrapped args to it.
return config.props({
@@ -337,7 +304,7 @@ const createHOC = (document, config, { notifyOnError = true }) =>
}
// Return our wrapped data with a separated root.
return { ...args, data: nextData, root: rootWithQueryMarkers };
return { ...args, data: nextData, root };
},
};
+12 -10
View File
@@ -84,20 +84,22 @@ function getSlotComponentProps(component, reduxState, props, queryData) {
}
/**
* splitProps detects props coming from the query and
* returns `queryData` and `rest`.
* splitProps detects objects coming from the query and
* returns `queryData` and `rest`. We use `__typename`
* in order to detect objects from the query.
*/
function splitProps(props) {
const rest = { ...props };
const queryData = {};
if (props.passthrough) {
Object.keys(props).forEach(k => {
if (props[k].__unsafeFromQuery) {
queryData[k] = props[k];
delete rest[k];
}
});
}
Object.keys(props).forEach(k => {
if (
get(props[k], `__typename`) ||
get(props[k], `0.__typename`) // Arrays
) {
queryData[k] = props[k];
delete rest[k];
}
});
return { queryData, rest };
}