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 () => {
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);

View File

@@ -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;
}
}

View File

@@ -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) {

View File

@@ -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
)

View File

@@ -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;

View File

@@ -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];