diff --git a/spec/ParseGraphQLSchema.spec.js b/spec/ParseGraphQLSchema.spec.js index a06a230f6a..0d4cd7d721 100644 --- a/spec/ParseGraphQLSchema.spec.js +++ b/spec/ParseGraphQLSchema.spec.js @@ -57,7 +57,6 @@ describe('ParseGraphQLSchema', () => { it('should load a brand new GraphQL Schema if Parse Schema changes', async () => { await parseGraphQLSchema.load(); const parseClasses = parseGraphQLSchema.parseClasses; - const parseCachedClasses = parseGraphQLSchema.parseCachedClasses; const parseClassTypes = parseGraphQLSchema.parseClassTypes; const graphQLSchema = parseGraphQLSchema.graphQLSchema; const graphQLTypes = parseGraphQLSchema.graphQLTypes; @@ -70,7 +69,6 @@ describe('ParseGraphQLSchema', () => { await new Promise(resolve => setTimeout(resolve, 200)); await parseGraphQLSchema.load(); expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses); - expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses); expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes); expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema); expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes); @@ -94,7 +92,6 @@ describe('ParseGraphQLSchema', () => { }); await parseGraphQLSchema.load(); const parseClasses = parseGraphQLSchema.parseClasses; - const parseCachedClasses = parseGraphQLSchema.parseCachedClasses; const parseClassTypes = parseGraphQLSchema.parseClassTypes; const graphQLSchema = parseGraphQLSchema.graphQLSchema; const graphQLTypes = parseGraphQLSchema.graphQLTypes; @@ -109,7 +106,6 @@ describe('ParseGraphQLSchema', () => { await new Promise(resolve => setTimeout(resolve, 200)); await parseGraphQLSchema.load(); expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses); - expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses); expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes); expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema); expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes); diff --git a/src/GraphQL/ParseGraphQLSchema.js b/src/GraphQL/ParseGraphQLSchema.js index 011905ca2d..7b942463f8 100644 --- a/src/GraphQL/ParseGraphQLSchema.js +++ b/src/GraphQL/ParseGraphQLSchema.js @@ -93,10 +93,14 @@ class ParseGraphQLSchema { async load() { const { parseGraphQLConfig } = await this._initializeSchemaAndConfig(); - const parseClasses = await this._getClassesForSchema(parseGraphQLConfig); + const parseClassesArray = await this._getClassesForSchema(parseGraphQLConfig); const functionNames = await this._getFunctionNames(); const functionNamesString = JSON.stringify(functionNames); + const parseClasses = parseClassesArray.reduce((acc, clazz) => { + acc[clazz.className] = clazz; + return acc; + }, {}); if ( !this._hasSchemaInputChanged({ parseClasses, @@ -127,7 +131,7 @@ class ParseGraphQLSchema { defaultRelaySchema.load(this); schemaTypes.load(this); - this._getParseClassesWithConfig(parseClasses, parseGraphQLConfig).forEach( + this._getParseClassesWithConfig(parseClassesArray, parseGraphQLConfig).forEach( ([parseClass, parseClassConfig]) => { // Some times schema return the _auth_data_ field // it will lead to unstable graphql generation order @@ -155,7 +159,7 @@ class ParseGraphQLSchema { } ); - defaultGraphQLTypes.loadArrayResult(this, parseClasses); + defaultGraphQLTypes.loadArrayResult(this, parseClassesArray); defaultGraphQLQueries.load(this); defaultGraphQLMutations.load(this); @@ -500,29 +504,17 @@ class ParseGraphQLSchema { const { parseClasses, parseGraphQLConfig, functionNamesString } = params; // First init - if (!this.parseCachedClasses || !this.graphQLSchema) { - const thisParseClassesObj = parseClasses.reduce((acc, clzz) => { - acc[clzz.className] = clzz; - return acc; - }, {}); - this.parseCachedClasses = thisParseClassesObj; + if (!this.graphQLSchema) { return true; } - const newParseCachedClasses = parseClasses.reduce((acc, clzz) => { - acc[clzz.className] = clzz; - return acc; - }, {}); - if ( isDeepStrictEqual(this.parseGraphQLConfig, parseGraphQLConfig) && this.functionNamesString === functionNamesString && - isDeepStrictEqual(this.parseCachedClasses, newParseCachedClasses) + isDeepStrictEqual(this.parseClasses, parseClasses) ) { return false; } - - this.parseCachedClasses = newParseCachedClasses; return true; } } diff --git a/src/GraphQL/helpers/objectsQueries.js b/src/GraphQL/helpers/objectsQueries.js index 7b973312ce..ce0b05a2c7 100644 --- a/src/GraphQL/helpers/objectsQueries.js +++ b/src/GraphQL/helpers/objectsQueries.js @@ -12,9 +12,7 @@ const needToGetAllKeys = (fields, keys, parseClasses) => if (fields[key[0]]) { if (fields[key[0]].type === 'Relation') return false; if (fields[key[0]].type === 'Pointer') { - const subClass = parseClasses.find( - ({ className: parseClassName }) => fields[key[0]].targetClass === parseClassName - ); + const subClass = parseClasses[fields[key[0]].targetClass]; if (subClass && subClass.fields[key[1]]) { // Current sub key is not custom return false; @@ -48,13 +46,7 @@ const getObject = async ( ) => { const options = {}; try { - if ( - !needToGetAllKeys( - parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields, - keys, - parseClasses - ) - ) { + if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) { options.keys = keys; } } catch (e) { @@ -165,13 +157,7 @@ const findObjects = async ( // Silently replace the limit on the query with the max configured options.limit = config.maxLimit; } - if ( - !needToGetAllKeys( - parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields, - keys, - parseClasses - ) - ) { + if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) { options.keys = keys; } if (includeAll === true) { diff --git a/src/GraphQL/loaders/defaultGraphQLTypes.js b/src/GraphQL/loaders/defaultGraphQLTypes.js index f340cfb35f..1520f0c53b 100644 --- a/src/GraphQL/loaders/defaultGraphQLTypes.js +++ b/src/GraphQL/loaders/defaultGraphQLTypes.js @@ -1190,8 +1190,8 @@ const ELEMENT = new GraphQLObjectType({ // Default static union type, we update types and resolveType function later let ARRAY_RESULT; -const loadArrayResult = (parseGraphQLSchema, parseClasses) => { - const classTypes = parseClasses +const loadArrayResult = (parseGraphQLSchema, parseClassesArray) => { + const classTypes = parseClassesArray .filter(parseClass => parseGraphQLSchema.parseClassTypes[parseClass.className].classGraphQLOutputType ? true : false ) diff --git a/src/GraphQL/transformers/mutation.js b/src/GraphQL/transformers/mutation.js index 2a21816fe9..5b72d6f05d 100644 --- a/src/GraphQL/transformers/mutation.js +++ b/src/GraphQL/transformers/mutation.js @@ -14,7 +14,7 @@ const transformTypes = async ( classGraphQLUpdateType, config: { isCreateEnabled, isUpdateEnabled }, } = parseGraphQLSchema.parseClassTypes[className]; - const parseClass = parseGraphQLSchema.parseClasses.find(clazz => clazz.className === className); + const parseClass = parseGraphQLSchema.parseClasses[className]; if (fields) { const classGraphQLCreateTypeFields = isCreateEnabled && classGraphQLCreateType ? classGraphQLCreateType.getFields() : null; diff --git a/src/GraphQL/transformers/query.js b/src/GraphQL/transformers/query.js index 92ea7be5fe..a91ee208ad 100644 --- a/src/GraphQL/transformers/query.js +++ b/src/GraphQL/transformers/query.js @@ -51,7 +51,7 @@ const transformQueryConstraintInputToParse = ( parentConstraints, parseClasses ) => { - const fields = parseClasses.find(parseClass => parseClass.className === className).fields; + const fields = parseClasses[className].fields; if (parentFieldName === 'id' && className) { Object.keys(constraints).forEach(constraintName => { const constraintValue = constraints[constraintName];