Skip to content

Commit

Permalink
refactor: replace internal GraphQL array classes to object style (#7788)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumouls committed May 6, 2022
1 parent 39fbcde commit 68b15c2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 42 deletions.
4 changes: 0 additions & 4 deletions spec/ParseGraphQLSchema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down
26 changes: 9 additions & 17 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -155,7 +159,7 @@ class ParseGraphQLSchema {
}
);

defaultGraphQLTypes.loadArrayResult(this, parseClasses);
defaultGraphQLTypes.loadArrayResult(this, parseClassesArray);
defaultGraphQLQueries.load(this);
defaultGraphQLMutations.load(this);

Expand Down Expand Up @@ -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;
}
}
Expand Down
20 changes: 3 additions & 17 deletions src/GraphQL/helpers/objectsQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL/loaders/defaultGraphQLTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/transformers/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/transformers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 68b15c2

Please sign in to comment.