Skip to content

Commit 68b15c2

Browse files
authored
refactor: replace internal GraphQL array classes to object style (#7788)
1 parent 39fbcde commit 68b15c2

File tree

6 files changed

+16
-42
lines changed

6 files changed

+16
-42
lines changed

spec/ParseGraphQLSchema.spec.js

-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ describe('ParseGraphQLSchema', () => {
5757
it('should load a brand new GraphQL Schema if Parse Schema changes', async () => {
5858
await parseGraphQLSchema.load();
5959
const parseClasses = parseGraphQLSchema.parseClasses;
60-
const parseCachedClasses = parseGraphQLSchema.parseCachedClasses;
6160
const parseClassTypes = parseGraphQLSchema.parseClassTypes;
6261
const graphQLSchema = parseGraphQLSchema.graphQLSchema;
6362
const graphQLTypes = parseGraphQLSchema.graphQLTypes;
@@ -70,7 +69,6 @@ describe('ParseGraphQLSchema', () => {
7069
await new Promise(resolve => setTimeout(resolve, 200));
7170
await parseGraphQLSchema.load();
7271
expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses);
73-
expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses);
7472
expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes);
7573
expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema);
7674
expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes);
@@ -94,7 +92,6 @@ describe('ParseGraphQLSchema', () => {
9492
});
9593
await parseGraphQLSchema.load();
9694
const parseClasses = parseGraphQLSchema.parseClasses;
97-
const parseCachedClasses = parseGraphQLSchema.parseCachedClasses;
9895
const parseClassTypes = parseGraphQLSchema.parseClassTypes;
9996
const graphQLSchema = parseGraphQLSchema.graphQLSchema;
10097
const graphQLTypes = parseGraphQLSchema.graphQLTypes;
@@ -109,7 +106,6 @@ describe('ParseGraphQLSchema', () => {
109106
await new Promise(resolve => setTimeout(resolve, 200));
110107
await parseGraphQLSchema.load();
111108
expect(parseClasses).not.toBe(parseGraphQLSchema.parseClasses);
112-
expect(parseCachedClasses).not.toBe(parseGraphQLSchema.parseCachedClasses);
113109
expect(parseClassTypes).not.toBe(parseGraphQLSchema.parseClassTypes);
114110
expect(graphQLSchema).not.toBe(parseGraphQLSchema.graphQLSchema);
115111
expect(graphQLTypes).not.toBe(parseGraphQLSchema.graphQLTypes);

src/GraphQL/ParseGraphQLSchema.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ class ParseGraphQLSchema {
9393

9494
async load() {
9595
const { parseGraphQLConfig } = await this._initializeSchemaAndConfig();
96-
const parseClasses = await this._getClassesForSchema(parseGraphQLConfig);
96+
const parseClassesArray = await this._getClassesForSchema(parseGraphQLConfig);
9797
const functionNames = await this._getFunctionNames();
9898
const functionNamesString = JSON.stringify(functionNames);
9999

100+
const parseClasses = parseClassesArray.reduce((acc, clazz) => {
101+
acc[clazz.className] = clazz;
102+
return acc;
103+
}, {});
100104
if (
101105
!this._hasSchemaInputChanged({
102106
parseClasses,
@@ -127,7 +131,7 @@ class ParseGraphQLSchema {
127131
defaultRelaySchema.load(this);
128132
schemaTypes.load(this);
129133

130-
this._getParseClassesWithConfig(parseClasses, parseGraphQLConfig).forEach(
134+
this._getParseClassesWithConfig(parseClassesArray, parseGraphQLConfig).forEach(
131135
([parseClass, parseClassConfig]) => {
132136
// Some times schema return the _auth_data_ field
133137
// it will lead to unstable graphql generation order
@@ -155,7 +159,7 @@ class ParseGraphQLSchema {
155159
}
156160
);
157161

158-
defaultGraphQLTypes.loadArrayResult(this, parseClasses);
162+
defaultGraphQLTypes.loadArrayResult(this, parseClassesArray);
159163
defaultGraphQLQueries.load(this);
160164
defaultGraphQLMutations.load(this);
161165

@@ -500,29 +504,17 @@ class ParseGraphQLSchema {
500504
const { parseClasses, parseGraphQLConfig, functionNamesString } = params;
501505

502506
// First init
503-
if (!this.parseCachedClasses || !this.graphQLSchema) {
504-
const thisParseClassesObj = parseClasses.reduce((acc, clzz) => {
505-
acc[clzz.className] = clzz;
506-
return acc;
507-
}, {});
508-
this.parseCachedClasses = thisParseClassesObj;
507+
if (!this.graphQLSchema) {
509508
return true;
510509
}
511510

512-
const newParseCachedClasses = parseClasses.reduce((acc, clzz) => {
513-
acc[clzz.className] = clzz;
514-
return acc;
515-
}, {});
516-
517511
if (
518512
isDeepStrictEqual(this.parseGraphQLConfig, parseGraphQLConfig) &&
519513
this.functionNamesString === functionNamesString &&
520-
isDeepStrictEqual(this.parseCachedClasses, newParseCachedClasses)
514+
isDeepStrictEqual(this.parseClasses, parseClasses)
521515
) {
522516
return false;
523517
}
524-
525-
this.parseCachedClasses = newParseCachedClasses;
526518
return true;
527519
}
528520
}

src/GraphQL/helpers/objectsQueries.js

+3-17
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const needToGetAllKeys = (fields, keys, parseClasses) =>
1212
if (fields[key[0]]) {
1313
if (fields[key[0]].type === 'Relation') return false;
1414
if (fields[key[0]].type === 'Pointer') {
15-
const subClass = parseClasses.find(
16-
({ className: parseClassName }) => fields[key[0]].targetClass === parseClassName
17-
);
15+
const subClass = parseClasses[fields[key[0]].targetClass];
1816
if (subClass && subClass.fields[key[1]]) {
1917
// Current sub key is not custom
2018
return false;
@@ -48,13 +46,7 @@ const getObject = async (
4846
) => {
4947
const options = {};
5048
try {
51-
if (
52-
!needToGetAllKeys(
53-
parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields,
54-
keys,
55-
parseClasses
56-
)
57-
) {
49+
if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) {
5850
options.keys = keys;
5951
}
6052
} catch (e) {
@@ -165,13 +157,7 @@ const findObjects = async (
165157
// Silently replace the limit on the query with the max configured
166158
options.limit = config.maxLimit;
167159
}
168-
if (
169-
!needToGetAllKeys(
170-
parseClasses.find(({ className: parseClassName }) => className === parseClassName).fields,
171-
keys,
172-
parseClasses
173-
)
174-
) {
160+
if (!needToGetAllKeys(parseClasses[className].fields, keys, parseClasses)) {
175161
options.keys = keys;
176162
}
177163
if (includeAll === true) {

src/GraphQL/loaders/defaultGraphQLTypes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ const ELEMENT = new GraphQLObjectType({
11901190
// Default static union type, we update types and resolveType function later
11911191
let ARRAY_RESULT;
11921192

1193-
const loadArrayResult = (parseGraphQLSchema, parseClasses) => {
1194-
const classTypes = parseClasses
1193+
const loadArrayResult = (parseGraphQLSchema, parseClassesArray) => {
1194+
const classTypes = parseClassesArray
11951195
.filter(parseClass =>
11961196
parseGraphQLSchema.parseClassTypes[parseClass.className].classGraphQLOutputType ? true : false
11971197
)

src/GraphQL/transformers/mutation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const transformTypes = async (
1414
classGraphQLUpdateType,
1515
config: { isCreateEnabled, isUpdateEnabled },
1616
} = parseGraphQLSchema.parseClassTypes[className];
17-
const parseClass = parseGraphQLSchema.parseClasses.find(clazz => clazz.className === className);
17+
const parseClass = parseGraphQLSchema.parseClasses[className];
1818
if (fields) {
1919
const classGraphQLCreateTypeFields =
2020
isCreateEnabled && classGraphQLCreateType ? classGraphQLCreateType.getFields() : null;

src/GraphQL/transformers/query.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const transformQueryConstraintInputToParse = (
5151
parentConstraints,
5252
parseClasses
5353
) => {
54-
const fields = parseClasses.find(parseClass => parseClass.className === className).fields;
54+
const fields = parseClasses[className].fields;
5555
if (parentFieldName === 'id' && className) {
5656
Object.keys(constraints).forEach(constraintName => {
5757
const constraintValue = constraints[constraintName];

0 commit comments

Comments
 (0)