123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.load = void 0;
- var _graphql = require("graphql");
- var _graphqlRelay = require("graphql-relay");
- var _graphqlListFields = _interopRequireDefault(require("graphql-list-fields"));
- var _deepcopy = _interopRequireDefault(require("deepcopy"));
- var defaultGraphQLTypes = _interopRequireWildcard(require("./defaultGraphQLTypes"));
- var _parseGraphQLUtils = require("../parseGraphQLUtils");
- var objectsMutations = _interopRequireWildcard(require("../helpers/objectsMutations"));
- var objectsQueries = _interopRequireWildcard(require("../helpers/objectsQueries"));
- var _ParseGraphQLController = require("../../Controllers/ParseGraphQLController");
- var _className = require("../transformers/className");
- var _mutation = require("../transformers/mutation");
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
- function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
- const filterDeletedFields = fields => Object.keys(fields).reduce((acc, key) => {
- var _fields$key;
- if (typeof fields[key] === 'object' && ((_fields$key = fields[key]) === null || _fields$key === void 0 ? void 0 : _fields$key.__op) === 'Delete') {
- acc[key] = null;
- }
- return acc;
- }, fields);
- const getOnlyRequiredFields = (updatedFields, selectedFieldsString, includedFieldsString, nativeObjectFields) => {
- const includedFields = includedFieldsString ? includedFieldsString.split(',') : [];
- const selectedFields = selectedFieldsString ? selectedFieldsString.split(',') : [];
- const missingFields = selectedFields.filter(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, parseClassConfig) {
- const className = parseClass.className;
- const graphQLClassName = (0, _className.transformClassNameToGraphQL)(className);
- const getGraphQLQueryName = graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
- const {
- create: isCreateEnabled = true,
- update: isUpdateEnabled = true,
- destroy: isDestroyEnabled = true,
- createAlias = '',
- updateAlias = '',
- destroyAlias = ''
- } = (0, _parseGraphQLUtils.getParseClassMutationConfig)(parseClassConfig);
- const {
- classGraphQLCreateType,
- classGraphQLUpdateType,
- classGraphQLOutputType
- } = parseGraphQLSchema.parseClassTypes[className];
- if (isCreateEnabled) {
- const createGraphQLMutationName = createAlias || `create${graphQLClassName}`;
- const createGraphQLMutation = (0, _graphqlRelay.mutationWithClientMutationId)({
- name: `Create${graphQLClassName}`,
- description: `The ${createGraphQLMutationName} mutation can be used to create a new object of the ${graphQLClassName} class.`,
- inputFields: {
- fields: {
- description: 'These are the fields that will be used to create the new object.',
- type: classGraphQLCreateType || defaultGraphQLTypes.OBJECT
- }
- },
- outputFields: {
- [getGraphQLQueryName]: {
- description: 'This is the created object.',
- type: new _graphql.GraphQLNonNull(classGraphQLOutputType || defaultGraphQLTypes.OBJECT)
- }
- },
- mutateAndGetPayload: async (args, context, mutationInfo) => {
- try {
- let {
- fields
- } = (0, _deepcopy.default)(args);
- if (!fields) fields = {};
- const {
- config,
- auth,
- info
- } = context;
- const parseFields = await (0, _mutation.transformTypes)('create', fields, {
- className,
- parseGraphQLSchema,
- originalFields: args.fields,
- req: {
- config,
- auth,
- info
- }
- });
- const createdObject = await objectsMutations.createObject(className, parseFields, config, auth, info);
- const selectedFields = (0, _graphqlListFields.default)(mutationInfo).filter(field => field.startsWith(`${getGraphQLQueryName}.`)).map(field => field.replace(`${getGraphQLQueryName}.`, ''));
- const {
- keys,
- include
- } = (0, _parseGraphQLUtils.extractKeysAndInclude)(selectedFields);
- const {
- keys: requiredKeys,
- needGet
- } = getOnlyRequiredFields(fields, keys, include, ['id', 'objectId', 'createdAt', 'updatedAt']);
- const needToGetAllKeys = objectsQueries.needToGetAllKeys(parseClass.fields, keys, parseGraphQLSchema.parseClasses);
- let optimizedObject = {};
- if (needGet && !needToGetAllKeys) {
- optimizedObject = await objectsQueries.getObject(className, createdObject.objectId, requiredKeys, include, undefined, undefined, config, auth, info, parseGraphQLSchema.parseClasses);
- } else if (needToGetAllKeys) {
- optimizedObject = await objectsQueries.getObject(className, createdObject.objectId, undefined, include, undefined, undefined, config, auth, info, parseGraphQLSchema.parseClasses);
- }
- return {
- [getGraphQLQueryName]: _objectSpread(_objectSpread(_objectSpread({}, createdObject), {}, {
- updatedAt: createdObject.createdAt
- }, filterDeletedFields(parseFields)), optimizedObject)
- };
- } catch (e) {
- parseGraphQLSchema.handleError(e);
- }
- }
- });
- if (parseGraphQLSchema.addGraphQLType(createGraphQLMutation.args.input.type.ofType) && parseGraphQLSchema.addGraphQLType(createGraphQLMutation.type)) {
- parseGraphQLSchema.addGraphQLMutation(createGraphQLMutationName, createGraphQLMutation);
- }
- }
- if (isUpdateEnabled) {
- const updateGraphQLMutationName = updateAlias || `update${graphQLClassName}`;
- const updateGraphQLMutation = (0, _graphqlRelay.mutationWithClientMutationId)({
- name: `Update${graphQLClassName}`,
- description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
- inputFields: {
- id: defaultGraphQLTypes.GLOBAL_OR_OBJECT_ID_ATT,
- fields: {
- description: 'These are the fields that will be used to update the object.',
- type: classGraphQLUpdateType || defaultGraphQLTypes.OBJECT
- }
- },
- outputFields: {
- [getGraphQLQueryName]: {
- description: 'This is the updated object.',
- type: new _graphql.GraphQLNonNull(classGraphQLOutputType || defaultGraphQLTypes.OBJECT)
- }
- },
- mutateAndGetPayload: async (args, context, mutationInfo) => {
- try {
- let {
- id,
- fields
- } = (0, _deepcopy.default)(args);
- if (!fields) fields = {};
- const {
- config,
- auth,
- info
- } = context;
- const globalIdObject = (0, _graphqlRelay.fromGlobalId)(id);
- if (globalIdObject.type === className) {
- id = globalIdObject.id;
- }
- const parseFields = await (0, _mutation.transformTypes)('update', fields, {
- className,
- parseGraphQLSchema,
- originalFields: args.fields,
- req: {
- config,
- auth,
- info
- }
- });
- const updatedObject = await objectsMutations.updateObject(className, id, parseFields, config, auth, info);
- const selectedFields = (0, _graphqlListFields.default)(mutationInfo).filter(field => field.startsWith(`${getGraphQLQueryName}.`)).map(field => field.replace(`${getGraphQLQueryName}.`, ''));
- const {
- keys,
- include
- } = (0, _parseGraphQLUtils.extractKeysAndInclude)(selectedFields);
- const {
- keys: requiredKeys,
- needGet
- } = getOnlyRequiredFields(fields, keys, include, ['id', 'objectId', 'updatedAt']);
- const needToGetAllKeys = objectsQueries.needToGetAllKeys(parseClass.fields, keys, parseGraphQLSchema.parseClasses);
- let optimizedObject = {};
- if (needGet && !needToGetAllKeys) {
- optimizedObject = await objectsQueries.getObject(className, id, requiredKeys, include, undefined, undefined, config, auth, info, parseGraphQLSchema.parseClasses);
- } else if (needToGetAllKeys) {
- optimizedObject = await objectsQueries.getObject(className, id, undefined, include, undefined, undefined, config, auth, info, parseGraphQLSchema.parseClasses);
- }
- return {
- [getGraphQLQueryName]: _objectSpread(_objectSpread(_objectSpread({
- objectId: id
- }, updatedObject), filterDeletedFields(parseFields)), optimizedObject)
- };
- } catch (e) {
- parseGraphQLSchema.handleError(e);
- }
- }
- });
- if (parseGraphQLSchema.addGraphQLType(updateGraphQLMutation.args.input.type.ofType) && parseGraphQLSchema.addGraphQLType(updateGraphQLMutation.type)) {
- parseGraphQLSchema.addGraphQLMutation(updateGraphQLMutationName, updateGraphQLMutation);
- }
- }
- if (isDestroyEnabled) {
- const deleteGraphQLMutationName = destroyAlias || `delete${graphQLClassName}`;
- const deleteGraphQLMutation = (0, _graphqlRelay.mutationWithClientMutationId)({
- name: `Delete${graphQLClassName}`,
- description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,
- inputFields: {
- id: defaultGraphQLTypes.GLOBAL_OR_OBJECT_ID_ATT
- },
- outputFields: {
- [getGraphQLQueryName]: {
- description: 'This is the deleted object.',
- type: new _graphql.GraphQLNonNull(classGraphQLOutputType || defaultGraphQLTypes.OBJECT)
- }
- },
- mutateAndGetPayload: async (args, context, mutationInfo) => {
- try {
- let {
- id
- } = (0, _deepcopy.default)(args);
- const {
- config,
- auth,
- info
- } = context;
- const globalIdObject = (0, _graphqlRelay.fromGlobalId)(id);
- if (globalIdObject.type === className) {
- id = globalIdObject.id;
- }
- const selectedFields = (0, _graphqlListFields.default)(mutationInfo).filter(field => field.startsWith(`${getGraphQLQueryName}.`)).map(field => field.replace(`${getGraphQLQueryName}.`, ''));
- const {
- keys,
- include
- } = (0, _parseGraphQLUtils.extractKeysAndInclude)(selectedFields);
- let optimizedObject = {};
- if (keys && keys.split(',').filter(key => !['id', 'objectId'].includes(key)).length > 0) {
- optimizedObject = await objectsQueries.getObject(className, id, keys, include, undefined, undefined, config, auth, info, parseGraphQLSchema.parseClasses);
- }
- await objectsMutations.deleteObject(className, id, config, auth, info);
- return {
- [getGraphQLQueryName]: _objectSpread({
- objectId: id
- }, optimizedObject)
- };
- } catch (e) {
- parseGraphQLSchema.handleError(e);
- }
- }
- });
- if (parseGraphQLSchema.addGraphQLType(deleteGraphQLMutation.args.input.type.ofType) && parseGraphQLSchema.addGraphQLType(deleteGraphQLMutation.type)) {
- parseGraphQLSchema.addGraphQLMutation(deleteGraphQLMutationName, deleteGraphQLMutation);
- }
- }
- };
- exports.load = load;
|