123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = exports.GraphQLConfigKey = exports.GraphQLConfigId = exports.GraphQLConfigClassName = void 0;
- var _requiredParameter = _interopRequireDefault(require("../../lib/requiredParameter"));
- var _DatabaseController = _interopRequireDefault(require("./DatabaseController"));
- var _CacheController = _interopRequireDefault(require("./CacheController"));
- const _excluded = ["enabledForClasses", "disabledForClasses", "classConfigs"],
- _excluded2 = ["className", "type", "query", "mutation"],
- _excluded3 = ["inputFields", "outputFields", "constraintFields", "sortFields"],
- _excluded4 = ["field", "asc", "desc"],
- _excluded5 = ["create", "update"],
- _excluded6 = ["find", "get", "findAlias", "getAlias"],
- _excluded7 = ["create", "update", "destroy", "createAlias", "updateAlias", "destroyAlias"];
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
- function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
- 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 GraphQLConfigClassName = exports.GraphQLConfigClassName = '_GraphQLConfig';
- const GraphQLConfigId = exports.GraphQLConfigId = '1';
- const GraphQLConfigKey = exports.GraphQLConfigKey = 'config';
- class ParseGraphQLController {
- constructor(params = {}) {
- this.databaseController = params.databaseController || (0, _requiredParameter.default)(`ParseGraphQLController requires a "databaseController" to be instantiated.`);
- this.cacheController = params.cacheController;
- this.isMounted = !!params.mountGraphQL;
- this.configCacheKey = GraphQLConfigKey;
- }
- async getGraphQLConfig() {
- if (this.isMounted) {
- const _cachedConfig = await this._getCachedGraphQLConfig();
- if (_cachedConfig) {
- return _cachedConfig;
- }
- }
- const results = await this.databaseController.find(GraphQLConfigClassName, {
- objectId: GraphQLConfigId
- }, {
- limit: 1
- });
- let graphQLConfig;
- if (results.length != 1) {
-
- return {};
- } else {
- graphQLConfig = results[0][GraphQLConfigKey];
- }
- if (this.isMounted) {
- this._putCachedGraphQLConfig(graphQLConfig);
- }
- return graphQLConfig;
- }
- async updateGraphQLConfig(graphQLConfig) {
-
- this._validateGraphQLConfig(graphQLConfig || (0, _requiredParameter.default)('You must provide a graphQLConfig!'));
-
- const update = Object.keys(graphQLConfig).reduce((acc, key) => {
- return {
- [GraphQLConfigKey]: _objectSpread(_objectSpread({}, acc[GraphQLConfigKey]), {}, {
- [key]: graphQLConfig[key]
- })
- };
- }, {
- [GraphQLConfigKey]: {}
- });
- await this.databaseController.update(GraphQLConfigClassName, {
- objectId: GraphQLConfigId
- }, update, {
- upsert: true
- });
- if (this.isMounted) {
- this._putCachedGraphQLConfig(graphQLConfig);
- }
- return {
- response: {
- result: true
- }
- };
- }
- _getCachedGraphQLConfig() {
- return this.cacheController.graphQL.get(this.configCacheKey);
- }
- _putCachedGraphQLConfig(graphQLConfig) {
- return this.cacheController.graphQL.put(this.configCacheKey, graphQLConfig, 60000);
- }
- _validateGraphQLConfig(graphQLConfig) {
- const errorMessages = [];
- if (!graphQLConfig) {
- errorMessages.push('cannot be undefined, null or empty');
- } else if (!isValidSimpleObject(graphQLConfig)) {
- errorMessages.push('must be a valid object');
- } else {
- const {
- enabledForClasses = null,
- disabledForClasses = null,
- classConfigs = null
- } = graphQLConfig,
- invalidKeys = _objectWithoutProperties(graphQLConfig, _excluded);
- if (Object.keys(invalidKeys).length) {
- errorMessages.push(`encountered invalid keys: [${Object.keys(invalidKeys)}]`);
- }
- if (enabledForClasses !== null && !isValidStringArray(enabledForClasses)) {
- errorMessages.push(`"enabledForClasses" is not a valid array`);
- }
- if (disabledForClasses !== null && !isValidStringArray(disabledForClasses)) {
- errorMessages.push(`"disabledForClasses" is not a valid array`);
- }
- if (classConfigs !== null) {
- if (Array.isArray(classConfigs)) {
- classConfigs.forEach(classConfig => {
- const errorMessage = this._validateClassConfig(classConfig);
- if (errorMessage) {
- errorMessages.push(`classConfig:${classConfig.className} is invalid because ${errorMessage}`);
- }
- });
- } else {
- errorMessages.push(`"classConfigs" is not a valid array`);
- }
- }
- }
- if (errorMessages.length) {
- throw new Error(`Invalid graphQLConfig: ${errorMessages.join('; ')}`);
- }
- }
- _validateClassConfig(classConfig) {
- if (!isValidSimpleObject(classConfig)) {
- return 'it must be a valid object';
- } else {
- const {
- className,
- type = null,
- query = null,
- mutation = null
- } = classConfig,
- invalidKeys = _objectWithoutProperties(classConfig, _excluded2);
- if (Object.keys(invalidKeys).length) {
- return `"invalidKeys" [${Object.keys(invalidKeys)}] should not be present`;
- }
- if (typeof className !== 'string' || !className.trim().length) {
-
- return `"className" must be a valid string`;
- }
- if (type !== null) {
- if (!isValidSimpleObject(type)) {
- return `"type" must be a valid object`;
- }
- const {
- inputFields = null,
- outputFields = null,
- constraintFields = null,
- sortFields = null
- } = type,
- invalidKeys = _objectWithoutProperties(type, _excluded3);
- if (Object.keys(invalidKeys).length) {
- return `"type" contains invalid keys, [${Object.keys(invalidKeys)}]`;
- } else if (outputFields !== null && !isValidStringArray(outputFields)) {
- return `"outputFields" must be a valid string array`;
- } else if (constraintFields !== null && !isValidStringArray(constraintFields)) {
- return `"constraintFields" must be a valid string array`;
- }
- if (sortFields !== null) {
- if (Array.isArray(sortFields)) {
- let errorMessage;
- sortFields.every((sortField, index) => {
- if (!isValidSimpleObject(sortField)) {
- errorMessage = `"sortField" at index ${index} is not a valid object`;
- return false;
- } else {
- const {
- field,
- asc,
- desc
- } = sortField,
- invalidKeys = _objectWithoutProperties(sortField, _excluded4);
- if (Object.keys(invalidKeys).length) {
- errorMessage = `"sortField" at index ${index} contains invalid keys, [${Object.keys(invalidKeys)}]`;
- return false;
- } else {
- if (typeof field !== 'string' || field.trim().length === 0) {
- errorMessage = `"sortField" at index ${index} did not provide the "field" as a string`;
- return false;
- } else if (typeof asc !== 'boolean' || typeof desc !== 'boolean') {
- errorMessage = `"sortField" at index ${index} did not provide "asc" or "desc" as booleans`;
- return false;
- }
- }
- }
- return true;
- });
- if (errorMessage) {
- return errorMessage;
- }
- } else {
- return `"sortFields" must be a valid array.`;
- }
- }
- if (inputFields !== null) {
- if (isValidSimpleObject(inputFields)) {
- const {
- create = null,
- update = null
- } = inputFields,
- invalidKeys = _objectWithoutProperties(inputFields, _excluded5);
- if (Object.keys(invalidKeys).length) {
- return `"inputFields" contains invalid keys: [${Object.keys(invalidKeys)}]`;
- } else {
- if (update !== null && !isValidStringArray(update)) {
- return `"inputFields.update" must be a valid string array`;
- } else if (create !== null) {
- if (!isValidStringArray(create)) {
- return `"inputFields.create" must be a valid string array`;
- } else if (className === '_User') {
- if (!create.includes('username') || !create.includes('password')) {
- return `"inputFields.create" must include required fields, username and password`;
- }
- }
- }
- }
- } else {
- return `"inputFields" must be a valid object`;
- }
- }
- }
- if (query !== null) {
- if (isValidSimpleObject(query)) {
- const {
- find = null,
- get = null,
- findAlias = null,
- getAlias = null
- } = query,
- invalidKeys = _objectWithoutProperties(query, _excluded6);
- if (Object.keys(invalidKeys).length) {
- return `"query" contains invalid keys, [${Object.keys(invalidKeys)}]`;
- } else if (find !== null && typeof find !== 'boolean') {
- return `"query.find" must be a boolean`;
- } else if (get !== null && typeof get !== 'boolean') {
- return `"query.get" must be a boolean`;
- } else if (findAlias !== null && typeof findAlias !== 'string') {
- return `"query.findAlias" must be a string`;
- } else if (getAlias !== null && typeof getAlias !== 'string') {
- return `"query.getAlias" must be a string`;
- }
- } else {
- return `"query" must be a valid object`;
- }
- }
- if (mutation !== null) {
- if (isValidSimpleObject(mutation)) {
- const {
- create = null,
- update = null,
- destroy = null,
- createAlias = null,
- updateAlias = null,
- destroyAlias = null
- } = mutation,
- invalidKeys = _objectWithoutProperties(mutation, _excluded7);
- if (Object.keys(invalidKeys).length) {
- return `"mutation" contains invalid keys, [${Object.keys(invalidKeys)}]`;
- }
- if (create !== null && typeof create !== 'boolean') {
- return `"mutation.create" must be a boolean`;
- }
- if (update !== null && typeof update !== 'boolean') {
- return `"mutation.update" must be a boolean`;
- }
- if (destroy !== null && typeof destroy !== 'boolean') {
- return `"mutation.destroy" must be a boolean`;
- }
- if (createAlias !== null && typeof createAlias !== 'string') {
- return `"mutation.createAlias" must be a string`;
- }
- if (updateAlias !== null && typeof updateAlias !== 'string') {
- return `"mutation.updateAlias" must be a string`;
- }
- if (destroyAlias !== null && typeof destroyAlias !== 'string') {
- return `"mutation.destroyAlias" must be a string`;
- }
- } else {
- return `"mutation" must be a valid object`;
- }
- }
- }
- }
- }
- const isValidStringArray = function (array) {
- return Array.isArray(array) ? !array.some(s => typeof s !== 'string' || s.trim().length < 1) : false;
- };
- const isValidSimpleObject = function (obj) {
- return typeof obj === 'object' && !Array.isArray(obj) && obj !== null && obj instanceof Date !== true && obj instanceof Promise !== true;
- };
- var _default = exports.default = ParseGraphQLController;
|