feat: alphabetical graphql api, fix internal reassign, enhanced Graphql schema cache system (#7344)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Parse from 'parse/node';
|
||||
import { GraphQLSchema, GraphQLObjectType, DocumentNode, GraphQLNamedType } from 'graphql';
|
||||
import { stitchSchemas } from '@graphql-tools/stitch';
|
||||
import { isDeepStrictEqual } from 'util';
|
||||
import { SchemaDirectiveVisitor } from '@graphql-tools/utils';
|
||||
import requiredParameter from '../requiredParameter';
|
||||
import * as defaultGraphQLTypes from './loaders/defaultGraphQLTypes';
|
||||
@@ -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,
|
||||
})
|
||||
@@ -110,7 +108,6 @@ class ParseGraphQLSchema {
|
||||
}
|
||||
|
||||
this.parseClasses = parseClasses;
|
||||
this.parseClassesString = parseClassesString;
|
||||
this.parseGraphQLConfig = parseGraphQLConfig;
|
||||
this.functionNames = functionNames;
|
||||
this.functionNamesString = functionNamesString;
|
||||
@@ -132,6 +129,26 @@ class ParseGraphQLSchema {
|
||||
|
||||
this._getParseClassesWithConfig(parseClasses, parseGraphQLConfig).forEach(
|
||||
([parseClass, parseClassConfig]) => {
|
||||
// Some times schema return the _auth_data_ field
|
||||
// it will lead to unstable graphql generation order
|
||||
if (parseClass.className === '_User') {
|
||||
Object.keys(parseClass.fields).forEach(fieldName => {
|
||||
if (fieldName.startsWith('_auth_data_')) {
|
||||
delete parseClass.fields[fieldName];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Fields order inside the schema seems to not be consistent across
|
||||
// restart so we need to ensure an alphabetical order
|
||||
// also it's better for the playground documentation
|
||||
const orderedFields = {};
|
||||
Object.keys(parseClass.fields)
|
||||
.sort()
|
||||
.forEach(fieldName => {
|
||||
orderedFields[fieldName] = parseClass.fields[fieldName];
|
||||
});
|
||||
parseClass.fields = orderedFields;
|
||||
parseClassTypes.load(this, parseClass, parseClassConfig);
|
||||
parseClassQueries.load(this, parseClass, parseClassConfig);
|
||||
parseClassMutations.load(this, parseClass, parseClassConfig);
|
||||
@@ -183,16 +200,17 @@ class ParseGraphQLSchema {
|
||||
schemaDirectives.load(this);
|
||||
|
||||
if (typeof this.graphQLCustomTypeDefs.getTypeMap === 'function') {
|
||||
const customGraphQLSchemaTypeMap = this.graphQLCustomTypeDefs.getTypeMap();
|
||||
// In following code we use underscore attr to avoid js var un ref
|
||||
const customGraphQLSchemaTypeMap = this.graphQLCustomTypeDefs._typeMap;
|
||||
const findAndReplaceLastType = (parent, key) => {
|
||||
if (parent[key].name) {
|
||||
if (
|
||||
this.graphQLAutoSchema.getType(parent[key].name) &&
|
||||
this.graphQLAutoSchema.getType(parent[key].name) !== parent[key]
|
||||
this.graphQLAutoSchema._typeMap[parent[key].name] &&
|
||||
this.graphQLAutoSchema._typeMap[parent[key].name] !== parent[key]
|
||||
) {
|
||||
// To avoid unresolved field on overloaded schema
|
||||
// replace the final type with the auto schema one
|
||||
parent[key] = this.graphQLAutoSchema.getType(parent[key].name);
|
||||
parent[key] = this.graphQLAutoSchema._typeMap[parent[key].name];
|
||||
}
|
||||
} else {
|
||||
if (parent[key].ofType) {
|
||||
@@ -200,47 +218,59 @@ class ParseGraphQLSchema {
|
||||
}
|
||||
}
|
||||
};
|
||||
Object.values(customGraphQLSchemaTypeMap).forEach(customGraphQLSchemaType => {
|
||||
if (
|
||||
!customGraphQLSchemaType ||
|
||||
!customGraphQLSchemaType.name ||
|
||||
customGraphQLSchemaType.name.startsWith('__')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const autoGraphQLSchemaType = this.graphQLAutoSchema.getType(
|
||||
customGraphQLSchemaType.name
|
||||
);
|
||||
if (!autoGraphQLSchemaType) {
|
||||
this.graphQLAutoSchema._typeMap[customGraphQLSchemaType.name] = customGraphQLSchemaType;
|
||||
}
|
||||
});
|
||||
Object.values(customGraphQLSchemaTypeMap).forEach(customGraphQLSchemaType => {
|
||||
if (
|
||||
!customGraphQLSchemaType ||
|
||||
!customGraphQLSchemaType.name ||
|
||||
customGraphQLSchemaType.name.startsWith('__')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const autoGraphQLSchemaType = this.graphQLAutoSchema.getType(
|
||||
customGraphQLSchemaType.name
|
||||
);
|
||||
// Add non shared types from custom schema to auto schema
|
||||
// note: some non shared types can use some shared types
|
||||
// so this code need to be ran before the shared types addition
|
||||
// we use sort to ensure schema consistency over restarts
|
||||
Object.keys(customGraphQLSchemaTypeMap)
|
||||
.sort()
|
||||
.forEach(customGraphQLSchemaTypeKey => {
|
||||
const customGraphQLSchemaType = customGraphQLSchemaTypeMap[customGraphQLSchemaTypeKey];
|
||||
if (
|
||||
!customGraphQLSchemaType ||
|
||||
!customGraphQLSchemaType.name ||
|
||||
customGraphQLSchemaType.name.startsWith('__')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const autoGraphQLSchemaType = this.graphQLAutoSchema._typeMap[
|
||||
customGraphQLSchemaType.name
|
||||
];
|
||||
if (!autoGraphQLSchemaType) {
|
||||
this.graphQLAutoSchema._typeMap[
|
||||
customGraphQLSchemaType.name
|
||||
] = customGraphQLSchemaType;
|
||||
}
|
||||
});
|
||||
// Handle shared types
|
||||
// We pass through each type and ensure that all sub field types are replaced
|
||||
// we use sort to ensure schema consistency over restarts
|
||||
Object.keys(customGraphQLSchemaTypeMap)
|
||||
.sort()
|
||||
.forEach(customGraphQLSchemaTypeKey => {
|
||||
const customGraphQLSchemaType = customGraphQLSchemaTypeMap[customGraphQLSchemaTypeKey];
|
||||
if (
|
||||
!customGraphQLSchemaType ||
|
||||
!customGraphQLSchemaType.name ||
|
||||
customGraphQLSchemaType.name.startsWith('__')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const autoGraphQLSchemaType = this.graphQLAutoSchema._typeMap[
|
||||
customGraphQLSchemaType.name
|
||||
];
|
||||
|
||||
if (autoGraphQLSchemaType && typeof customGraphQLSchemaType.getFields === 'function') {
|
||||
Object.values(customGraphQLSchemaType.getFields()).forEach(field => {
|
||||
findAndReplaceLastType(field, 'type');
|
||||
});
|
||||
autoGraphQLSchemaType._fields = {
|
||||
...autoGraphQLSchemaType.getFields(),
|
||||
...customGraphQLSchemaType.getFields(),
|
||||
};
|
||||
}
|
||||
});
|
||||
this.graphQLSchema = stitchSchemas({
|
||||
schemas: [this.graphQLSchemaDirectivesDefinitions, this.graphQLAutoSchema],
|
||||
mergeDirectives: true,
|
||||
});
|
||||
if (autoGraphQLSchemaType && typeof customGraphQLSchemaType.getFields === 'function') {
|
||||
Object.keys(customGraphQLSchemaType._fields)
|
||||
.sort()
|
||||
.forEach(fieldKey => {
|
||||
const field = customGraphQLSchemaType._fields[fieldKey];
|
||||
findAndReplaceLastType(field, 'type');
|
||||
autoGraphQLSchemaType._fields[field.name] = field;
|
||||
});
|
||||
}
|
||||
});
|
||||
this.graphQLSchema = this.graphQLAutoSchema;
|
||||
} else if (typeof this.graphQLCustomTypeDefs === 'function') {
|
||||
this.graphQLSchema = await this.graphQLCustomTypeDefs({
|
||||
directivesDefinitionsSchema: this.graphQLSchemaDirectivesDefinitions,
|
||||
@@ -258,6 +288,7 @@ class ParseGraphQLSchema {
|
||||
});
|
||||
}
|
||||
|
||||
// Only merge directive when string schema provided
|
||||
const graphQLSchemaTypeMap = this.graphQLSchema.getTypeMap();
|
||||
Object.keys(graphQLSchemaTypeMap).forEach(graphQLSchemaTypeName => {
|
||||
const graphQLSchemaType = graphQLSchemaTypeMap[graphQLSchemaTypeName];
|
||||
@@ -463,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;
|
||||
|
||||
if (
|
||||
JSON.stringify(this.parseGraphQLConfig) === JSON.stringify(parseGraphQLConfig) &&
|
||||
this.functionNamesString === functionNamesString
|
||||
) {
|
||||
if (this.parseClasses === parseClasses) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.parseClassesString === parseClassesString) {
|
||||
this.parseClasses = parseClasses;
|
||||
return false;
|
||||
}
|
||||
// First init
|
||||
if (!this.parseCachedClasses || !this.graphQLSchema) {
|
||||
const thisParseClassesObj = parseClasses.reduce((acc, clzz) => {
|
||||
acc[clzz.className] = clzz;
|
||||
return acc;
|
||||
}, {});
|
||||
this.parseCachedClasses = thisParseClassesObj;
|
||||
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)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.parseCachedClasses = newParseCachedClasses;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
|
||||
import deepcopy from 'deepcopy';
|
||||
import { mutationWithClientMutationId } from 'graphql-relay';
|
||||
import { FunctionsRouter } from '../../Routers/FunctionsRouter';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
@@ -43,7 +44,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context) => {
|
||||
try {
|
||||
const { functionName, params } = args;
|
||||
const { functionName, params } = deepcopy(args);
|
||||
const { config, auth, info } = context;
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import { fromGlobalId, mutationWithClientMutationId } from 'graphql-relay';
|
||||
import getFieldNames from 'graphql-list-fields';
|
||||
import deepcopy from 'deepcopy';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import { extractKeysAndInclude, getParseClassMutationConfig } from '../parseGraphQLUtils';
|
||||
import * as objectsMutations from '../helpers/objectsMutations';
|
||||
@@ -66,7 +67,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
let { fields } = args;
|
||||
let { fields } = deepcopy(args);
|
||||
if (!fields) fields = {};
|
||||
const { config, auth, info } = context;
|
||||
|
||||
@@ -168,7 +169,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
let { id, fields } = args;
|
||||
let { id, fields } = deepcopy(args);
|
||||
if (!fields) fields = {};
|
||||
const { config, auth, info } = context;
|
||||
|
||||
@@ -273,7 +274,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
let { id } = args;
|
||||
let { id } = deepcopy(args);
|
||||
const { config, auth, info } = context;
|
||||
|
||||
const globalIdObject = fromGlobalId(id);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import { fromGlobalId } from 'graphql-relay';
|
||||
import getFieldNames from 'graphql-list-fields';
|
||||
import deepcopy from 'deepcopy';
|
||||
import pluralize from 'pluralize';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import * as objectsQueries from '../helpers/objectsQueries';
|
||||
@@ -74,7 +75,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
||||
return await getQuery(
|
||||
parseClass,
|
||||
_source,
|
||||
args,
|
||||
deepcopy(args),
|
||||
context,
|
||||
queryInfo,
|
||||
parseGraphQLSchema.parseClasses
|
||||
@@ -97,7 +98,8 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
||||
type: new GraphQLNonNull(classGraphQLFindResultType || defaultGraphQLTypes.OBJECT),
|
||||
async resolve(_source, args, context, queryInfo) {
|
||||
try {
|
||||
const { where, order, skip, first, after, last, before, options } = args;
|
||||
// Deep copy args to avoid internal re assign issue
|
||||
const { where, order, skip, first, after, last, before, options } = deepcopy(args);
|
||||
const { readPreference, includeReadPreference, subqueryReadPreference } = options || {};
|
||||
const { config, auth, info } = context;
|
||||
const selectedFields = getFieldNames(queryInfo);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import Parse from 'parse/node';
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import deepcopy from 'deepcopy';
|
||||
import { mutationWithClientMutationId } from 'graphql-relay';
|
||||
import * as schemaTypes from './schemaTypes';
|
||||
import { transformToParse, transformToGraphQL } from '../transformers/schemaFields';
|
||||
@@ -26,7 +27,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context) => {
|
||||
try {
|
||||
const { name, schemaFields } = args;
|
||||
const { name, schemaFields } = deepcopy(args);
|
||||
const { config, auth } = context;
|
||||
|
||||
enforceMasterKeyAccess(auth);
|
||||
@@ -75,7 +76,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context) => {
|
||||
try {
|
||||
const { name, schemaFields } = args;
|
||||
const { name, schemaFields } = deepcopy(args);
|
||||
const { config, auth } = context;
|
||||
|
||||
enforceMasterKeyAccess(auth);
|
||||
@@ -126,7 +127,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context) => {
|
||||
try {
|
||||
const { name } = args;
|
||||
const { name } = deepcopy(args);
|
||||
const { config, auth } = context;
|
||||
|
||||
enforceMasterKeyAccess(auth);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import Parse from 'parse/node';
|
||||
import deepcopy from 'deepcopy';
|
||||
import { GraphQLNonNull, GraphQLList } from 'graphql';
|
||||
import { transformToGraphQL } from '../transformers/schemaFields';
|
||||
import * as schemaTypes from './schemaTypes';
|
||||
@@ -27,7 +28,7 @@ const load = parseGraphQLSchema => {
|
||||
type: new GraphQLNonNull(schemaTypes.CLASS),
|
||||
resolve: async (_source, args, context) => {
|
||||
try {
|
||||
const { name } = args;
|
||||
const { name } = deepcopy(args);
|
||||
const { config, auth } = context;
|
||||
|
||||
enforceMasterKeyAccess(auth);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { GraphQLNonNull, GraphQLString, GraphQLBoolean, GraphQLInputObjectType } from 'graphql';
|
||||
import { mutationWithClientMutationId } from 'graphql-relay';
|
||||
import deepcopy from 'deepcopy';
|
||||
import UsersRouter from '../../Routers/UsersRouter';
|
||||
import * as objectsMutations from '../helpers/objectsMutations';
|
||||
import { OBJECT } from './defaultGraphQLTypes';
|
||||
@@ -31,7 +32,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
const { fields } = args;
|
||||
const { fields } = deepcopy(args);
|
||||
const { config, auth, info } = context;
|
||||
|
||||
const parseFields = await transformTypes('create', fields, {
|
||||
@@ -101,7 +102,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
const { fields, authData } = args;
|
||||
const { fields, authData } = deepcopy(args);
|
||||
const { config, auth, info } = context;
|
||||
|
||||
const parseFields = await transformTypes('create', fields, {
|
||||
@@ -154,7 +155,7 @@ const load = parseGraphQLSchema => {
|
||||
},
|
||||
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
||||
try {
|
||||
const { username, password } = args;
|
||||
const { username, password } = deepcopy(args);
|
||||
const { config, auth, info } = context;
|
||||
|
||||
const { sessionToken, objectId } = (
|
||||
|
||||
Reference in New Issue
Block a user