refactor: replace internal GraphQL array classes to object style (#7788)

This commit is contained in:
Antoine Cormouls
2022-05-06 02:09:09 +02:00
committed by GitHub
parent 39fbcde612
commit 68b15c298e
6 changed files with 16 additions and 42 deletions

View File

@@ -57,7 +57,6 @@ describe('ParseGraphQLSchema', () => {
it('should load a brand new GraphQL Schema if Parse Schema changes', async () => { it('should load a brand new GraphQL Schema if Parse Schema changes', async () => {
await parseGraphQLSchema.load(); await parseGraphQLSchema.load();
const parseClasses = parseGraphQLSchema.parseClasses; const parseClasses = parseGraphQLSchema.parseClasses;
const parseCachedClasses = parseGraphQLSchema.parseCachedClasses;
const parseClassTypes = parseGraphQLSchema.parseClassTypes; const parseClassTypes = parseGraphQLSchema.parseClassTypes;
const graphQLSchema = parseGraphQLSchema.graphQLSchema; const graphQLSchema = parseGraphQLSchema.graphQLSchema;
const graphQLTypes = parseGraphQLSchema.graphQLTypes; const graphQLTypes = parseGraphQLSchema.graphQLTypes;
@@ -70,7 +69,6 @@ describe('ParseGraphQLSchema', () => {
await new Promise(resolve => setTimeout(resolve, 200)); await new Promise(resolve => setTimeout(resolve, 200));
await parseGraphQLSchema.load(); await parseGraphQLSchema.load();
expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses); expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses);
expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses);
expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes); expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes);
expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema); expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema);
expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes); expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes);
@@ -94,7 +92,6 @@ describe('ParseGraphQLSchema', () => {
}); });
await parseGraphQLSchema.load(); await parseGraphQLSchema.load();
const parseClasses = parseGraphQLSchema.parseClasses; const parseClasses = parseGraphQLSchema.parseClasses;
const parseCachedClasses = parseGraphQLSchema.parseCachedClasses;
const parseClassTypes = parseGraphQLSchema.parseClassTypes; const parseClassTypes = parseGraphQLSchema.parseClassTypes;
const graphQLSchema = parseGraphQLSchema.graphQLSchema; const graphQLSchema = parseGraphQLSchema.graphQLSchema;
const graphQLTypes = parseGraphQLSchema.graphQLTypes; const graphQLTypes = parseGraphQLSchema.graphQLTypes;
@@ -109,7 +106,6 @@ describe('ParseGraphQLSchema', () => {
await new Promise(resolve => setTimeout(resolve, 200)); await new Promise(resolve => setTimeout(resolve, 200));
await parseGraphQLSchema.load(); await parseGraphQLSchema.load();
expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses); expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses);
expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses);
expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes); expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes);
expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema); expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema);
expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes); expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes);

View File

@@ -93,10 +93,14 @@ class ParseGraphQLSchema {
async load() { async load() {
const { parseGraphQLConfig } = await this._initializeSchemaAndConfig(); const { parseGraphQLConfig } = await this._initializeSchemaAndConfig();
const parseClasses = await this._getClassesForSchema(parseGraphQLConfig); const parseClassesArray = await this._getClassesForSchema(parseGraphQLConfig);
const functionNames = await this._getFunctionNames(); const functionNames = await this._getFunctionNames();
const functionNamesString = JSON.stringify(functionNames); const functionNamesString = JSON.stringify(functionNames);
const parseClasses = parseClassesArray.reduce((acc, clazz) => {
acc[clazz.className] = clazz;
return acc;
}, {});
if ( if (
!this._hasSchemaInputChanged({ !this._hasSchemaInputChanged({
parseClasses, parseClasses,
@@ -127,7 +131,7 @@ class ParseGraphQLSchema {
defaultRelaySchema.load(this); defaultRelaySchema.load(this);
schemaTypes.load(this); schemaTypes.load(this);
this._getParseClassesWithConfig(parseClasses, parseGraphQLConfig).forEach( this._getParseClassesWithConfig(parseClassesArray, parseGraphQLConfig).forEach(
([parseClass, parseClassConfig]) => { ([parseClass, parseClassConfig]) => {
// Some times schema return the _auth_data_ field // Some times schema return the _auth_data_ field
// it will lead to unstable graphql generation order // 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); defaultGraphQLQueries.load(this);
defaultGraphQLMutations.load(this); defaultGraphQLMutations.load(this);
@@ -500,29 +504,17 @@ class ParseGraphQLSchema {
const { parseClasses, parseGraphQLConfig, functionNamesString } = params; const { parseClasses, parseGraphQLConfig, functionNamesString } = params;
// First init // First init
if (!this.parseCachedClasses || !this.graphQLSchema) { if (!this.graphQLSchema) {
const thisParseClassesObj = parseClasses.reduce((acc, clzz) => {
acc[clzz.className] = clzz;
return acc;
}, {});
this.parseCachedClasses = thisParseClassesObj;
return true; return true;
} }
const newParseCachedClasses = parseClasses.reduce((acc, clzz) => {
acc[clzz.className] = clzz;
return acc;
}, {});
if ( if (
isDeepStrictEqual(this.parseGraphQLConfig, parseGraphQLConfig) && isDeepStrictEqual(this.parseGraphQLConfig, parseGraphQLConfig) &&
this.functionNamesString === functionNamesString && this.functionNamesString === functionNamesString &&
isDeepStrictEqual(this.parseCachedClasses, newParseCachedClasses) isDeepStrictEqual(this.parseClasses, parseClasses)
) { ) {
return false; return false;
} }
this.parseCachedClasses = newParseCachedClasses;
return true; return true;
} }
} }

View File

@@ -12,9 +12,7 @@ const needToGetAllKeys = (fields, keys, parseClasses) =>
if (fields[key[0]]) { if (fields[key[0]]) {
if (fields[key[0]].type === 'Relation') return false; if (fields[key[0]].type === 'Relation') return false;
if (fields[key[0]].type === 'Pointer') { if (fields[key[0]].type === 'Pointer') {
const subClass = parseClasses.find( const subClass = parseClasses[fields[key[0]].targetClass];
({ className: parseClassName }) => fields[key[0]].targetClass === parseClassName
);
if (subClass && subClass.fields[key[1]]) { if (subClass && subClass.fields[key[1]]) {
// Current sub key is not custom // Current sub key is not custom
return false; return false;
@@ -48,13 +46,7 @@ const getObject = async (
) => { ) => {
const options = {}; const options = {};
try { try {
if ( if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) {
!needToGetAllKeys(
parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields,
keys,
parseClasses
)
) {
options.keys = keys; options.keys = keys;
} }
} catch (e) { } catch (e) {
@@ -165,13 +157,7 @@ const findObjects = async (
// Silently replace the limit on the query with the max configured // Silently replace the limit on the query with the max configured
options.limit = config.maxLimit; options.limit = config.maxLimit;
} }
if ( if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) {
!needToGetAllKeys(
parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields,
keys,
parseClasses
)
) {
options.keys = keys; options.keys = keys;
} }
if (includeAll === true) { if (includeAll === true) {

View File

@@ -1190,8 +1190,8 @@ const ELEMENT = new GraphQLObjectType({
// Default static union type, we update types and resolveType function later // Default static union type, we update types and resolveType function later
let ARRAY_RESULT; let ARRAY_RESULT;
const loadArrayResult = (parseGraphQLSchema, parseClasses) => { const loadArrayResult = (parseGraphQLSchema, parseClassesArray) => {
const classTypes = parseClasses const classTypes = parseClassesArray
.filter(parseClass => .filter(parseClass =>
parseGraphQLSchema.parseClassTypes[parseClass.className].classGraphQLOutputType ? true : false parseGraphQLSchema.parseClassTypes[parseClass.className].classGraphQLOutputType ? true : false
) )

View File

@@ -14,7 +14,7 @@ const transformTypes = async (
classGraphQLUpdateType, classGraphQLUpdateType,
config: { isCreateEnabled, isUpdateEnabled }, config: { isCreateEnabled, isUpdateEnabled },
} = parseGraphQLSchema.parseClassTypes[className]; } = parseGraphQLSchema.parseClassTypes[className];
const parseClass = parseGraphQLSchema.parseClasses.find(clazz => clazz.className === className); const parseClass = parseGraphQLSchema.parseClasses[className];
if (fields) { if (fields) {
const classGraphQLCreateTypeFields = const classGraphQLCreateTypeFields =
isCreateEnabled && classGraphQLCreateType ? classGraphQLCreateType.getFields() : null; isCreateEnabled && classGraphQLCreateType ? classGraphQLCreateType.getFields() : null;

View File

@@ -51,7 +51,7 @@ const transformQueryConstraintInputToParse = (
parentConstraints, parentConstraints,
parseClasses parseClasses
) => { ) => {
const fields = parseClasses.find(parseClass => parseClass.className === className).fields; const fields = parseClasses[className].fields;
if (parentFieldName === 'id' && className) { if (parentFieldName === 'id' && className) {
Object.keys(constraints).forEach(constraintName => { Object.keys(constraints).forEach(constraintName => {
const constraintValue = constraints[constraintName]; const constraintValue = constraints[constraintName];