mirror of
https://github.com/wassname/talk.git
synced 2026-08-11 11:27:10 +08:00
Fix query data detection
This commit is contained in:
@@ -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 };
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user