2019-08-12 10:38:59 +02:00
|
|
|
import { GraphQLNonNull } from 'graphql';
|
|
|
|
|
import getFieldNames from 'graphql-list-fields';
|
2019-06-19 17:19:47 -07:00
|
|
|
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
2019-08-21 23:55:34 +02:00
|
|
|
import {
|
|
|
|
|
extractKeysAndInclude,
|
|
|
|
|
getParseClassMutationConfig,
|
|
|
|
|
} from '../parseGraphQLUtils';
|
2019-09-01 22:11:03 -07:00
|
|
|
import * as objectsMutations from '../helpers/objectsMutations';
|
|
|
|
|
import * as objectsQueries from '../helpers/objectsQueries';
|
2019-07-25 20:46:25 +01:00
|
|
|
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
|
2019-08-15 23:23:41 +02:00
|
|
|
import { transformClassNameToGraphQL } from '../transformers/className';
|
2019-08-21 23:55:34 +02:00
|
|
|
import { transformTypes } from '../transformers/mutation';
|
2019-07-25 20:46:25 +01:00
|
|
|
|
2019-08-12 10:38:59 +02:00
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
const load = function(
|
|
|
|
|
parseGraphQLSchema,
|
|
|
|
|
parseClass,
|
|
|
|
|
parseClassConfig: ?ParseGraphQLClassConfig
|
|
|
|
|
) {
|
2019-08-15 23:23:41 +02:00
|
|
|
const className = parseClass.className;
|
|
|
|
|
const graphQLClassName = transformClassNameToGraphQL(className);
|
|
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
const {
|
|
|
|
|
create: isCreateEnabled = true,
|
|
|
|
|
update: isUpdateEnabled = true,
|
|
|
|
|
destroy: isDestroyEnabled = true,
|
|
|
|
|
} = getParseClassMutationConfig(parseClassConfig);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
classGraphQLCreateType,
|
|
|
|
|
classGraphQLUpdateType,
|
2019-08-12 10:38:59 +02:00
|
|
|
classGraphQLOutputType,
|
2019-07-25 20:46:25 +01:00
|
|
|
} = parseGraphQLSchema.parseClassTypes[className];
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (isCreateEnabled) {
|
2019-08-15 23:23:41 +02:00
|
|
|
const createGraphQLMutationName = `create${graphQLClassName}`;
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLMutation(createGraphQLMutationName, {
|
2019-08-15 23:23:41 +02:00
|
|
|
description: `The ${createGraphQLMutationName} mutation can be used to create a new object of the ${graphQLClassName} class.`,
|
2019-07-25 20:46:25 +01:00
|
|
|
args: {
|
2019-08-15 23:23:41 +02:00
|
|
|
fields: {
|
|
|
|
|
description: 'These are the fields used to create the object.',
|
|
|
|
|
type: classGraphQLCreateType || defaultGraphQLTypes.OBJECT,
|
|
|
|
|
},
|
2019-07-25 20:46:25 +01:00
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
type: new GraphQLNonNull(
|
|
|
|
|
classGraphQLOutputType || defaultGraphQLTypes.OBJECT
|
|
|
|
|
),
|
2019-08-12 10:38:59 +02:00
|
|
|
async resolve(_source, args, context, mutationInfo) {
|
2019-07-25 20:46:25 +01:00
|
|
|
try {
|
2019-08-12 10:38:59 +02:00
|
|
|
let { fields } = args;
|
|
|
|
|
if (!fields) fields = {};
|
2019-07-25 20:46:25 +01:00
|
|
|
const { config, auth, info } = context;
|
2019-08-21 23:55:34 +02:00
|
|
|
|
|
|
|
|
const parseFields = await transformTypes('create', fields, {
|
|
|
|
|
className,
|
|
|
|
|
parseGraphQLSchema,
|
|
|
|
|
req: { config, auth, info },
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-12 10:38:59 +02:00
|
|
|
const createdObject = await objectsMutations.createObject(
|
2019-07-25 20:46:25 +01:00
|
|
|
className,
|
2019-08-21 23:55:34 +02:00
|
|
|
parseFields,
|
2019-07-25 20:46:25 +01:00
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
2019-08-12 10:38:59 +02:00
|
|
|
const selectedFields = getFieldNames(mutationInfo);
|
2019-08-14 21:25:28 +02:00
|
|
|
const { keys, include } = extractKeysAndInclude(selectedFields);
|
2019-08-12 10:38:59 +02:00
|
|
|
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
|
|
|
|
fields,
|
|
|
|
|
keys,
|
|
|
|
|
include,
|
2019-08-30 20:23:46 -03:00
|
|
|
['id', 'createdAt', 'updatedAt']
|
2019-08-12 10:38:59 +02:00
|
|
|
);
|
|
|
|
|
let optimizedObject = {};
|
|
|
|
|
if (needGet) {
|
|
|
|
|
optimizedObject = await objectsQueries.getObject(
|
|
|
|
|
className,
|
|
|
|
|
createdObject.objectId,
|
|
|
|
|
requiredKeys,
|
|
|
|
|
include,
|
|
|
|
|
undefined,
|
|
|
|
|
undefined,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...createdObject,
|
|
|
|
|
updatedAt: createdObject.createdAt,
|
2019-10-02 06:47:56 +02:00
|
|
|
...parseFields,
|
2019-08-12 10:38:59 +02:00
|
|
|
...optimizedObject,
|
|
|
|
|
};
|
2019-07-25 20:46:25 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
});
|
2019-07-25 20:46:25 +01:00
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (isUpdateEnabled) {
|
2019-08-15 23:23:41 +02:00
|
|
|
const updateGraphQLMutationName = `update${graphQLClassName}`;
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLMutation(updateGraphQLMutationName, {
|
2019-08-15 23:23:41 +02:00
|
|
|
description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
|
2019-07-25 20:46:25 +01:00
|
|
|
args: {
|
2019-08-30 20:23:46 -03:00
|
|
|
id: defaultGraphQLTypes.OBJECT_ID_ATT,
|
2019-08-15 23:23:41 +02:00
|
|
|
fields: {
|
|
|
|
|
description: 'These are the fields used to update the object.',
|
|
|
|
|
type: classGraphQLUpdateType || defaultGraphQLTypes.OBJECT,
|
|
|
|
|
},
|
2019-07-25 20:46:25 +01:00
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
type: new GraphQLNonNull(
|
|
|
|
|
classGraphQLOutputType || defaultGraphQLTypes.OBJECT
|
|
|
|
|
),
|
2019-08-12 10:38:59 +02:00
|
|
|
async resolve(_source, args, context, mutationInfo) {
|
2019-07-25 20:46:25 +01:00
|
|
|
try {
|
2019-08-30 20:23:46 -03:00
|
|
|
const { id, fields } = args;
|
2019-07-25 20:46:25 +01:00
|
|
|
const { config, auth, info } = context;
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-21 23:55:34 +02:00
|
|
|
const parseFields = await transformTypes('update', fields, {
|
|
|
|
|
className,
|
|
|
|
|
parseGraphQLSchema,
|
|
|
|
|
req: { config, auth, info },
|
|
|
|
|
});
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-12 10:38:59 +02:00
|
|
|
const updatedObject = await objectsMutations.updateObject(
|
2019-07-25 20:46:25 +01:00
|
|
|
className,
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-08-21 23:55:34 +02:00
|
|
|
parseFields,
|
2019-07-25 20:46:25 +01:00
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
2019-10-02 06:47:56 +02:00
|
|
|
|
2019-08-12 10:38:59 +02:00
|
|
|
const selectedFields = getFieldNames(mutationInfo);
|
2019-08-14 21:25:28 +02:00
|
|
|
const { keys, include } = extractKeysAndInclude(selectedFields);
|
2019-08-12 10:38:59 +02:00
|
|
|
|
|
|
|
|
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
|
|
|
|
fields,
|
|
|
|
|
keys,
|
|
|
|
|
include,
|
2019-08-30 20:23:46 -03:00
|
|
|
['id', 'updatedAt']
|
2019-08-12 10:38:59 +02:00
|
|
|
);
|
|
|
|
|
let optimizedObject = {};
|
|
|
|
|
if (needGet) {
|
|
|
|
|
optimizedObject = await objectsQueries.getObject(
|
|
|
|
|
className,
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-08-12 10:38:59 +02:00
|
|
|
requiredKeys,
|
|
|
|
|
include,
|
|
|
|
|
undefined,
|
|
|
|
|
undefined,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-08-21 23:55:34 +02:00
|
|
|
return {
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-08-21 23:55:34 +02:00
|
|
|
...updatedObject,
|
2019-10-02 06:47:56 +02:00
|
|
|
...parseFields,
|
2019-08-21 23:55:34 +02:00
|
|
|
...optimizedObject,
|
|
|
|
|
};
|
2019-07-25 20:46:25 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
});
|
2019-07-25 20:46:25 +01:00
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (isDestroyEnabled) {
|
2019-08-15 23:23:41 +02:00
|
|
|
const deleteGraphQLMutationName = `delete${graphQLClassName}`;
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLMutation(deleteGraphQLMutationName, {
|
2019-08-15 23:23:41 +02:00
|
|
|
description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,
|
2019-07-25 20:46:25 +01:00
|
|
|
args: {
|
2019-08-30 20:23:46 -03:00
|
|
|
id: defaultGraphQLTypes.OBJECT_ID_ATT,
|
2019-07-25 20:46:25 +01:00
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
type: new GraphQLNonNull(
|
|
|
|
|
classGraphQLOutputType || defaultGraphQLTypes.OBJECT
|
|
|
|
|
),
|
2019-08-12 10:38:59 +02:00
|
|
|
async resolve(_source, args, context, mutationInfo) {
|
2019-07-25 20:46:25 +01:00
|
|
|
try {
|
2019-08-30 20:23:46 -03:00
|
|
|
const { id } = args;
|
2019-07-25 20:46:25 +01:00
|
|
|
const { config, auth, info } = context;
|
2019-08-12 10:38:59 +02:00
|
|
|
const selectedFields = getFieldNames(mutationInfo);
|
2019-08-14 21:25:28 +02:00
|
|
|
const { keys, include } = extractKeysAndInclude(selectedFields);
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-12 10:38:59 +02:00
|
|
|
let optimizedObject = {};
|
|
|
|
|
const splitedKeys = keys.split(',');
|
2019-08-30 20:23:46 -03:00
|
|
|
if (splitedKeys.length > 1 || splitedKeys[0] !== 'id') {
|
2019-08-12 10:38:59 +02:00
|
|
|
optimizedObject = await objectsQueries.getObject(
|
|
|
|
|
className,
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-08-12 10:38:59 +02:00
|
|
|
keys,
|
|
|
|
|
include,
|
|
|
|
|
undefined,
|
|
|
|
|
undefined,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
await objectsMutations.deleteObject(
|
2019-07-25 20:46:25 +01:00
|
|
|
className,
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-07-25 20:46:25 +01:00
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
2019-08-30 20:23:46 -03:00
|
|
|
return { id, ...optimizedObject };
|
2019-07-25 20:46:25 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
});
|
2019-07-25 20:46:25 +01:00
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { load };
|