@sparklink-pro/apant
Version:
Apollo & Antd tools
111 lines • 4.61 kB
JavaScript
import { __awaiter } from "tslib";
import { lowerFirst, defaultsDeep, get, mapValues } from 'lodash-es';
import { OPERATION_GET, OPERATION_LIST, OPERATION_CREATE, OPERATION_UPDATE, OPERATION_DELETE, } from '../definitions';
const operationResolversDefault = {
[OPERATION_GET]: (t) => t,
[OPERATION_LIST]: (t) => `${t}List`,
[OPERATION_CREATE]: (t) => `${t}Create`,
[OPERATION_UPDATE]: (t) => `${t}Update`,
[OPERATION_DELETE]: (t) => `${t}Delete`,
};
const operationKeyResolverDefault = (operationName) => `${operationName}Document`;
const defaultOperationConfiguration = {
operationResolvers: operationResolversDefault,
operationKeyResolver: operationKeyResolverDefault,
[OPERATION_LIST]: {
getVariables: () => { },
getResult: (payload) => get(payload, 'res.items', []),
},
[OPERATION_GET]: {
getVariables: () => { },
getResult: (payload) => get(payload, 'res'),
},
[OPERATION_CREATE]: {
getVariables: (values) => ({ input: values }),
getResult: (payload) => get(payload, 'res'),
},
[OPERATION_UPDATE]: {
getVariables: (item, values) => ({ item: item.id, input: values }),
getResult: (payload) => get(payload, 'res'),
},
[OPERATION_DELETE]: {
getVariables: (item) => ({ item: item.id }),
getResult: (payload) => get(payload, 'res'),
},
};
const defaultTypeConfiguration = {
id: 'id',
};
/**
* This is the registry of all the types is it used to get the types configuration and to resolve graphql operations for a given type.
*/
export class TypesRegistry {
constructor({ types, apollo, operations = {}, configuration }) {
this.operations = {};
this.apollo = apollo;
this.operations = operations;
this.configuration = defaultsDeep(configuration, defaultOperationConfiguration);
this.types = mapValues(types, (typeConfiguration) => defaultsDeep(typeConfiguration, Object.assign(Object.assign({}, defaultTypeConfiguration), { operations: this.configuration })));
}
getTypes() {
return Object.keys(this.types);
}
hasType(type) {
return !!this.types[type];
}
getType(type) {
if (!this.types[type]) {
throw new Error(`GraphQL Type ${type} doesn't exists in registry. Available types: ${this.getTypes().join(', ')}`);
}
return this.types[type];
}
queryList(type_1) {
return __awaiter(this, arguments, void 0, function* (type, options = {}) {
if (!this.apollo) {
throw new Error(`Apollo client is not defined.`);
}
const { operations: { list: { getResult }, }, } = this.getType(type);
const query = this.getOperation(type, OPERATION_LIST);
if (query === null) {
return [];
}
const res = yield this.apollo.query(Object.assign({ query }, options));
return Object.assign(Object.assign({}, res), { items: getResult(res.data) });
});
}
queryItem(type_1, id_1) {
return __awaiter(this, arguments, void 0, function* (type, id, options = {}) {
if (!this.apollo) {
throw new Error(`Apollo client is not defined.`);
}
const { operations: { get: { getResult }, }, } = this.getType(type);
const query = this.getOperation(type, OPERATION_GET);
if (query === null) {
return [];
}
const res = yield this.apollo.query(Object.assign({ query, variables: { id } }, options));
return Object.assign(Object.assign({}, res), { item: getResult(res.data) });
});
}
getTypeFragmentName(type) {
return `_${lowerFirst(type)}`;
}
getOperation(type, operation) {
const configuration = this.getType(type).operations;
const operationName = configuration.operationResolvers[operation](type);
const operationKey = configuration.operationKeyResolver(operationName, operation);
if (!this.operations[operationKey]) {
this.error(type, `Missing ${operation} "${operationName}" (looked index in operations file was "${operationKey}").`);
}
return this.operations[operationKey];
}
error(type, message, throwException = false) {
const logMessage = `[Types Registry] [${type}] : ${message}`;
console.error(logMessage);
if (throwException) {
throw new Error(logMessage);
}
}
}
export default TypesRegistry;
//# sourceMappingURL=TypesRegistry.js.map