From 255cb15c19f662f96d992c3542eba022e68f035a Mon Sep 17 00:00:00 2001 From: Antoine Cormouls Date: Mon, 23 Mar 2020 09:40:04 +0100 Subject: [PATCH] GraphQL: Handle properly keys for pointer fields (#6499) * Fix Unknow type bug on overloaded types * check args too * Additional fix to detect custom fields on pointer --- src/GraphQL/helpers/objectsQueries.js | 47 +++++++++++++++++++--- src/GraphQL/loaders/defaultRelaySchema.js | 4 +- src/GraphQL/loaders/parseClassMutations.js | 16 ++++---- src/GraphQL/loaders/parseClassQueries.js | 20 +++++++-- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/GraphQL/helpers/objectsQueries.js b/src/GraphQL/helpers/objectsQueries.js index e7133d20..e6f09223 100644 --- a/src/GraphQL/helpers/objectsQueries.js +++ b/src/GraphQL/helpers/objectsQueries.js @@ -3,10 +3,32 @@ import { offsetToCursor, cursorToOffset } from 'graphql-relay'; import rest from '../../rest'; import { transformQueryInputToParse } from '../transformers/query'; -const needToGetAllKeys = (fields, keys) => +// Eslint/Prettier conflict +/* eslint-disable*/ +const needToGetAllKeys = (fields, keys, parseClasses) => keys - ? !!keys.split(',').find(keyName => !fields[keyName.split('.')[0]]) + ? keys.split(',').some(keyName => { + const key = keyName.split('.'); + if (fields[key[0]]) { + if (fields[key[0]].type === 'Pointer') { + const subClass = parseClasses.find( + ({ className: parseClassName }) => + fields[key[0]].targetClass === parseClassName + ); + if (subClass && subClass.fields[key[1]]) { + // Current sub key is not custom + return false; + } + } else if (!key[1]) { + // current key is not custom + return false; + } + } + // Key not found into Parse Schema so it's custom + return true; + }) : true; +/* eslint-enable*/ const transformOrder = order => order @@ -32,11 +54,23 @@ const getObject = async ( config, auth, info, - parseClass + parseClasses ) => { const options = {}; - if (!needToGetAllKeys(parseClass.fields, keys)) { - options.keys = keys; + try { + if ( + !needToGetAllKeys( + parseClasses.find( + ({ className: parseClassName }) => className === parseClassName + ).fields, + keys, + parseClasses + ) + ) { + options.keys = keys; + } + } catch (e) { + console.log(e); } if (include) { options.include = include; @@ -158,7 +192,8 @@ const findObjects = async ( parseClasses.find( ({ className: parseClassName }) => className === parseClassName ).fields, - keys + keys, + parseClasses ) ) { options.keys = keys; diff --git a/src/GraphQL/loaders/defaultRelaySchema.js b/src/GraphQL/loaders/defaultRelaySchema.js index c3c2d9cc..b7af16a3 100644 --- a/src/GraphQL/loaders/defaultRelaySchema.js +++ b/src/GraphQL/loaders/defaultRelaySchema.js @@ -31,9 +31,7 @@ const load = parseGraphQLSchema => { config, auth, info, - parseGraphQLSchema.parseClasses.find( - ({ className }) => type === className - ) + parseGraphQLSchema.parseClasses )), }; } catch (e) { diff --git a/src/GraphQL/loaders/parseClassMutations.js b/src/GraphQL/loaders/parseClassMutations.js index f41cccf5..1a6e01df 100644 --- a/src/GraphQL/loaders/parseClassMutations.js +++ b/src/GraphQL/loaders/parseClassMutations.js @@ -114,7 +114,8 @@ const load = function( ); const needToGetAllKeys = objectsQueries.needToGetAllKeys( parseClass.fields, - keys + keys, + parseGraphQLSchema.parseClasses ); let optimizedObject = {}; if (needGet && !needToGetAllKeys) { @@ -128,7 +129,7 @@ const load = function( config, auth, info, - parseClass + parseGraphQLSchema.parseClasses ); } else if (needToGetAllKeys) { optimizedObject = await objectsQueries.getObject( @@ -141,7 +142,7 @@ const load = function( config, auth, info, - parseClass + parseGraphQLSchema.parseClasses ); } return { @@ -232,7 +233,8 @@ const load = function( ); const needToGetAllKeys = objectsQueries.needToGetAllKeys( parseClass.fields, - keys + keys, + parseGraphQLSchema.parseClasses ); let optimizedObject = {}; if (needGet && !needToGetAllKeys) { @@ -246,7 +248,7 @@ const load = function( config, auth, info, - parseClass + parseGraphQLSchema.parseClasses ); } else if (needToGetAllKeys) { optimizedObject = await objectsQueries.getObject( @@ -259,7 +261,7 @@ const load = function( config, auth, info, - parseClass + parseGraphQLSchema.parseClasses ); } return { @@ -337,7 +339,7 @@ const load = function( config, auth, info, - parseClass + parseGraphQLSchema.parseClasses ); } await objectsMutations.deleteObject( diff --git a/src/GraphQL/loaders/parseClassQueries.js b/src/GraphQL/loaders/parseClassQueries.js index 83d90c1d..57b06d06 100644 --- a/src/GraphQL/loaders/parseClassQueries.js +++ b/src/GraphQL/loaders/parseClassQueries.js @@ -14,7 +14,14 @@ const getParseClassQueryConfig = function( return (parseClassConfig && parseClassConfig.query) || {}; }; -const getQuery = async (parseClass, _source, args, context, queryInfo) => { +const getQuery = async ( + parseClass, + _source, + args, + context, + queryInfo, + parseClasses +) => { let { id } = args; const { options } = args; const { readPreference, includeReadPreference } = options || {}; @@ -39,7 +46,7 @@ const getQuery = async (parseClass, _source, args, context, queryInfo) => { config, auth, info, - parseClass + parseClasses ); }; @@ -80,7 +87,14 @@ const load = function( ), async resolve(_source, args, context, queryInfo) { try { - return await getQuery(parseClass, _source, args, context, queryInfo); + return await getQuery( + parseClass, + _source, + args, + context, + queryInfo, + parseGraphQLSchema.parseClasses + ); } catch (e) { parseGraphQLSchema.handleError(e); }