From 73fce8624c66f157cee92f15f5d3b8241d3808b0 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 13 Mar 2018 23:32:21 +0100 Subject: [PATCH] Fix query data detection --- client/coral-framework/hocs/withQuery.js | 35 +--------------------- client/coral-framework/services/plugins.js | 22 +++++++------- 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/client/coral-framework/hocs/withQuery.js b/client/coral-framework/hocs/withQuery.js index 807989af5..77fb7380e 100644 --- a/client/coral-framework/hocs/withQuery.js +++ b/client/coral-framework/hocs/withQuery.js @@ -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 }; }, }; diff --git a/client/coral-framework/services/plugins.js b/client/coral-framework/services/plugins.js index b390f1a1b..4c09c1047 100644 --- a/client/coral-framework/services/plugins.js +++ b/client/coral-framework/services/plugins.js @@ -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 }; }