Skip to content

Commit

Permalink
fix: graphql schema cache system
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumouls committed Oct 9, 2021
1 parent 66e25db commit abc0dad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"graphql-tag": "2.12.5",
"graphql-upload": "11.0.0",
"intersect": "1.0.1",
"json-stable-stringify": "1.0.1",
"jsonwebtoken": "8.5.1",
"jwks-rsa": "1.12.3",
"ldapjs": "2.3.1",
Expand Down
36 changes: 21 additions & 15 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Parse from 'parse/node';
import { GraphQLSchema, GraphQLObjectType, DocumentNode, GraphQLNamedType } from 'graphql';
import { stitchSchemas } from '@graphql-tools/stitch';
import stableStringify from 'json-stable-stringify';
import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
import requiredParameter from '../requiredParameter';
import * as defaultGraphQLTypes from './loaders/defaultGraphQLTypes';
Expand Down Expand Up @@ -93,15 +94,12 @@ class ParseGraphQLSchema {
async load() {
const { parseGraphQLConfig } = await this._initializeSchemaAndConfig();
const parseClasses = await this._getClassesForSchema(parseGraphQLConfig);
const parseClassesString = JSON.stringify(parseClasses);
const functionNames = await this._getFunctionNames();
const functionNamesString = JSON.stringify(functionNames);

if (
this.graphQLSchema &&
!this._hasSchemaInputChanged({
parseClasses,
parseClassesString,
parseGraphQLConfig,
functionNamesString,
})
Expand All @@ -110,7 +108,6 @@ class ParseGraphQLSchema {
}

this.parseClasses = parseClasses;
this.parseClassesString = parseClassesString;
this.parseGraphQLConfig = parseGraphQLConfig;
this.functionNames = functionNames;
this.functionNamesString = functionNamesString;
Expand Down Expand Up @@ -497,26 +494,35 @@ class ParseGraphQLSchema {
*/
_hasSchemaInputChanged(params: {
parseClasses: any,
parseClassesString: string,
parseGraphQLConfig: ?ParseGraphQLConfig,
functionNamesString: string,
}): boolean {
const { parseClasses, parseClassesString, parseGraphQLConfig, functionNamesString } = params;
const { parseClasses, parseGraphQLConfig, functionNamesString } = params;

// First init
if (!this.parseClassesString || !this.graphQLSchema) {
const thisParseClassesObj = parseClasses.reduce((acc, clzz) => {
acc[clzz.className] = clzz;
return acc;
}, {});
this.parseClassesString = stableStringify(thisParseClassesObj);
return true;
}

const parseClassesObj = parseClasses.reduce((acc, clzz) => {
acc[clzz.className] = clzz;
return acc;
}, {});
const newParseClassesString = stableStringify(parseClassesObj);
if (
JSON.stringify(this.parseGraphQLConfig) === JSON.stringify(parseGraphQLConfig) &&
this.functionNamesString === functionNamesString
this.functionNamesString === functionNamesString &&
this.parseClassesString === newParseClassesString
) {
if (this.parseClasses === parseClasses) {
return false;
}

if (this.parseClassesString === parseClassesString) {
this.parseClasses = parseClasses;
return false;
}
return false;
}

this.parseClassesString = newParseClassesString;
return true;
}
}
Expand Down

0 comments on commit abc0dad

Please sign in to comment.