Return specific Type on specific Mutation (#5893)
* Return specific Type on specific Mutation * Add Optimization on Mutation * Optimize SignUp
This commit is contained in:
committed by
Antonio Davi Macedo Coelho de Castro
parent
6f6210387e
commit
9031961e86
@@ -1,6 +1,9 @@
|
||||
import { GraphQLNonNull, GraphQLBoolean } from 'graphql';
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import getFieldNames from 'graphql-list-fields';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import * as parseClassTypes from './parseClassTypes';
|
||||
import * as objectsMutations from './objectsMutations';
|
||||
import * as objectsQueries from './objectsQueries';
|
||||
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
|
||||
|
||||
const getParseClassMutationConfig = function(
|
||||
@@ -9,6 +12,28 @@ const getParseClassMutationConfig = function(
|
||||
return (parseClassConfig && parseClassConfig.mutation) || {};
|
||||
};
|
||||
|
||||
const getOnlyRequiredFields = (
|
||||
updatedFields,
|
||||
selectedFieldsString,
|
||||
includedFieldsString,
|
||||
nativeObjectFields
|
||||
) => {
|
||||
const includedFields = includedFieldsString.split(',');
|
||||
const selectedFields = selectedFieldsString.split(',');
|
||||
const missingFields = selectedFields
|
||||
.filter(
|
||||
field =>
|
||||
(!updatedFields[field] && !nativeObjectFields.includes(field)) ||
|
||||
includedFields.includes(field)
|
||||
)
|
||||
.join(',');
|
||||
if (!missingFields.length) {
|
||||
return { needGet: false, keys: '' };
|
||||
} else {
|
||||
return { needGet: true, keys: missingFields };
|
||||
}
|
||||
};
|
||||
|
||||
const load = function(
|
||||
parseGraphQLSchema,
|
||||
parseClass,
|
||||
@@ -24,6 +49,7 @@ const load = function(
|
||||
const {
|
||||
classGraphQLCreateType,
|
||||
classGraphQLUpdateType,
|
||||
classGraphQLOutputType,
|
||||
} = parseGraphQLSchema.parseClassTypes[className];
|
||||
|
||||
const createFields = {
|
||||
@@ -78,21 +104,50 @@ const load = function(
|
||||
args: {
|
||||
fields: createFields,
|
||||
},
|
||||
type: new GraphQLNonNull(defaultGraphQLTypes.CREATE_RESULT),
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { fields } = args;
|
||||
let { fields } = args;
|
||||
if (!fields) fields = {};
|
||||
const { config, auth, info } = context;
|
||||
|
||||
transformTypes('create', fields);
|
||||
|
||||
return await objectsMutations.createObject(
|
||||
const createdObject = await objectsMutations.createObject(
|
||||
className,
|
||||
fields,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
||||
fields,
|
||||
keys,
|
||||
include,
|
||||
['objectId', 'createdAt', 'updatedAt']
|
||||
);
|
||||
let optimizedObject = {};
|
||||
if (needGet) {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
createdObject.objectId,
|
||||
requiredKeys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
return {
|
||||
...createdObject,
|
||||
updatedAt: createdObject.createdAt,
|
||||
...fields,
|
||||
...optimizedObject,
|
||||
};
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -108,15 +163,15 @@ const load = function(
|
||||
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
|
||||
fields: updateFields,
|
||||
},
|
||||
type: defaultGraphQLTypes.UPDATE_RESULT,
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { objectId, fields } = args;
|
||||
const { config, auth, info } = context;
|
||||
|
||||
transformTypes('update', fields);
|
||||
|
||||
return await objectsMutations.updateObject(
|
||||
const updatedObject = await objectsMutations.updateObject(
|
||||
className,
|
||||
objectId,
|
||||
fields,
|
||||
@@ -124,6 +179,32 @@ const load = function(
|
||||
auth,
|
||||
info
|
||||
);
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
|
||||
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
||||
fields,
|
||||
keys,
|
||||
include,
|
||||
['objectId', 'updatedAt']
|
||||
);
|
||||
let optimizedObject = {};
|
||||
if (needGet) {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
objectId,
|
||||
requiredKeys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
return { ...updatedObject, ...fields, ...optimizedObject };
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -138,19 +219,39 @@ const load = function(
|
||||
args: {
|
||||
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
|
||||
},
|
||||
type: new GraphQLNonNull(GraphQLBoolean),
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { objectId } = args;
|
||||
const { config, auth, info } = context;
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
|
||||
return await objectsMutations.deleteObject(
|
||||
let optimizedObject = {};
|
||||
const splitedKeys = keys.split(',');
|
||||
if (splitedKeys.length > 1 || splitedKeys[0] !== 'objectId') {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
objectId,
|
||||
keys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
await objectsMutations.deleteObject(
|
||||
className,
|
||||
objectId,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
return { objectId: objectId, ...optimizedObject };
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user