2019-07-12 17:58:47 -03:00
|
|
|
import Parse from 'parse/node';
|
2024-03-02 04:06:47 +03:00
|
|
|
import { GraphQLError } from 'graphql';
|
2019-07-12 17:58:47 -03:00
|
|
|
|
2019-09-01 22:11:03 -07:00
|
|
|
export function enforceMasterKeyAccess(auth) {
|
|
|
|
|
if (!auth.isMaster) {
|
2020-10-25 15:06:58 -05:00
|
|
|
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'unauthorized: master key is required');
|
2019-09-01 22:11:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 17:58:47 -03:00
|
|
|
export function toGraphQLError(error) {
|
|
|
|
|
let code, message;
|
|
|
|
|
if (error instanceof Parse.Error) {
|
|
|
|
|
code = error.code;
|
|
|
|
|
message = error.message;
|
|
|
|
|
} else {
|
|
|
|
|
code = Parse.Error.INTERNAL_SERVER_ERROR;
|
|
|
|
|
message = 'Internal server error';
|
|
|
|
|
}
|
2024-03-02 04:06:47 +03:00
|
|
|
return new GraphQLError(message, { extensions: { code } });
|
2019-07-12 17:58:47 -03:00
|
|
|
}
|
2019-08-14 21:25:28 +02:00
|
|
|
|
2020-10-25 15:06:58 -05:00
|
|
|
export const extractKeysAndInclude = selectedFields => {
|
|
|
|
|
selectedFields = selectedFields.filter(field => !field.includes('__typename'));
|
2019-08-30 20:23:46 -03:00
|
|
|
// Handles "id" field for both current and included objects
|
2020-10-25 15:06:58 -05:00
|
|
|
selectedFields = selectedFields.map(field => {
|
2024-10-16 19:57:42 +02:00
|
|
|
if (field === 'id') { return 'objectId'; }
|
2019-08-30 20:23:46 -03:00
|
|
|
return field.endsWith('.id')
|
|
|
|
|
? `${field.substring(0, field.lastIndexOf('.id'))}.objectId`
|
|
|
|
|
: field;
|
|
|
|
|
});
|
2019-08-14 21:25:28 +02:00
|
|
|
let keys = undefined;
|
|
|
|
|
let include = undefined;
|
2020-10-02 00:19:26 +02:00
|
|
|
|
2019-08-30 20:23:46 -03:00
|
|
|
if (selectedFields.length > 0) {
|
2020-10-02 00:19:26 +02:00
|
|
|
keys = [...new Set(selectedFields)].join(',');
|
|
|
|
|
// We can use this shortcut since optimization is handled
|
|
|
|
|
// later on RestQuery, avoid overhead here.
|
|
|
|
|
include = keys;
|
2019-08-14 21:25:28 +02:00
|
|
|
}
|
2020-10-02 00:19:26 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
// If authData is detected keys will not work properly
|
|
|
|
|
// since authData has a special storage behavior
|
|
|
|
|
// so we need to skip keys currently
|
|
|
|
|
keys: keys && keys.indexOf('authData') === -1 ? keys : undefined,
|
|
|
|
|
include,
|
|
|
|
|
};
|
2019-08-14 21:25:28 +02:00
|
|
|
};
|
2019-08-21 23:55:34 +02:00
|
|
|
|
2020-07-13 13:06:52 -05:00
|
|
|
export const getParseClassMutationConfig = function (parseClassConfig) {
|
2019-08-21 23:55:34 +02:00
|
|
|
return (parseClassConfig && parseClassConfig.mutation) || {};
|
|
|
|
|
};
|