UNPKG

iworks-core-api

Version:

iwroks server api module

298 lines (273 loc) 8.84 kB
import { GraphQLObjectType, GraphQLString, GraphQLInputObjectType, GraphQLBoolean, GraphQLInt, GraphQLFloat, GraphQLNonNull, } from 'graphql'; import { omit } from 'lodash'; import { IMaterial, ISelectMaterial } from '../model/material'; import { Material } from 'iworks-db-model'; import { createMaterial, updateMaterial, restoreMaterial, archivingMaterial, deleteMaterial, getSelectorFromMaterialDTO, } from './materialRepository'; import { selectorMaterialType, materialTypeObjectType, inputMaterialType, } from '../materialType/mutations'; import { getSelectorFromMateriaTypelDTO, isValidMaterialTypeSelector, } from '../materialType/materialTypeRepository'; /** * Input type */ interface InputMaterial { input: IMaterial; } /** * */ interface IInputDeleteMaterial { input: ISelectMaterial; } /** * */ interface IInputSelectMaterial { input: ISelectMaterial; } /** * Contains descriptions of common fields. */ const materialFeildsDescription = { id: 'ID of the material', materialType: 'A type of the material', name: 'A name of ther', description: 'A description of the material', price: 'A price of the material', code: 'A code of ther material. Can be used as a ID', modificationCode: 'A modification code of the material. ' + 'This is additinal field. Another type of ther material', imageSrc: 'A image url of the material', thumbnailSrc: 'A thumbnail url of the material', created: 'A date when material was updated', updated: 'A date when material was updated', status: 'A status of the material', }; const materialType = new GraphQLObjectType({ name: 'MaterialReturnType', description: 'Material type', fields: () => ({ id: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.id }, materialType: { type: new GraphQLNonNull(materialTypeObjectType), description: materialFeildsDescription.materialType }, name: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.name }, description: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.description }, price: { type: new GraphQLNonNull(GraphQLFloat), description: materialFeildsDescription.price }, code: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.code }, modificationCode: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.modificationCode }, imageSrc: { type: GraphQLString, description: materialFeildsDescription.imageSrc }, thumbnailSrc: { type: GraphQLString, description: materialFeildsDescription.thumbnailSrc }, created: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.created }, updated: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.updated }, status: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.status }, }), }); export const selectMaterialType = new GraphQLInputObjectType({ name: 'MaterialSelectorType', description: 'Use of one field of this object to archive a material type.', fields: () => ({ id: { type: GraphQLString, description: materialFeildsDescription.id }, code: { type: GraphQLString, description: materialFeildsDescription.code }, modificationCode: { type: GraphQLString, description: materialFeildsDescription.modificationCode }, }), }); const createMaterialType = new GraphQLInputObjectType({ name: 'MaterialInputType', description: 'Use this object to create new material type', fields: () => ({ name: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.name }, materialType: { type: new GraphQLNonNull(selectorMaterialType), description: materialFeildsDescription.materialType }, description: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.description }, price: { type: new GraphQLNonNull(GraphQLFloat), description: materialFeildsDescription.price }, code: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.code }, modificationCode: { type: new GraphQLNonNull(GraphQLString), description: materialFeildsDescription.modificationCode }, imageSrc: { type: GraphQLString, description: materialFeildsDescription.imageSrc }, thumbnailSrc: { type: GraphQLString, description: materialFeildsDescription.thumbnailSrc }, }), }); const updateMaterialType = new GraphQLInputObjectType({ name: 'UpdateMaterialInputType', description: 'Use this object to change existing material type', fields: () => ({ id: { type:GraphQLString, description: materialFeildsDescription.id }, name: { type: GraphQLString, description: materialFeildsDescription.name }, materialType: { type: selectorMaterialType, description: materialFeildsDescription.materialType }, description: { type: GraphQLString, description: materialFeildsDescription.description }, price: { type: GraphQLFloat, description: materialFeildsDescription.price }, code: { type: GraphQLString, description: materialFeildsDescription.code }, modificationCode: { type: GraphQLString, description: materialFeildsDescription.modificationCode }, imageSrc: { type: GraphQLString, description: materialFeildsDescription.imageSrc }, thumbnailSrc: { type: GraphQLString, description: materialFeildsDescription.thumbnailSrc }, }), }); export default { createMaterial: { description: 'Creates a new material', type: materialType, args: { input: { type: new GraphQLNonNull(createMaterialType) }, }, resolve: (_ : any, inputMaterial : InputMaterial) => { const input = inputMaterial.input; if (!input) { throw new Error('Input is not specified for createMaterial'); } const materialType = input.materialType; if (!materialType) { throw new Error('materialType is not specified for createMaterial'); } const selector = getSelectorFromMaterialDTO(input); const materialSelector = getSelectorFromMateriaTypelDTO(materialType); const cleanType = omit(input, ['materialType']); return createMaterial(cleanType, materialSelector); }, }, archivingMaterial: { description: 'Archiving a material', type: materialType, args: { input: { type: new GraphQLNonNull(selectMaterialType) }, }, resolve: (_ : any, inputSelect : IInputSelectMaterial) => { const input = inputSelect.input; if (!input) { throw new Error('Input is not specified for createMaterial'); } return archivingMaterial(input); }, }, restoreMaterial: { description: 'Restore a material', type: materialType, args: { input: { type: new GraphQLNonNull(selectMaterialType) }, }, resolve: (_ : any, inputSelect : IInputSelectMaterial) => { const input = inputSelect.input; if (!input) { throw new Error('Input is not specified for createMaterial'); } return restoreMaterial(input); }, }, updateMaterial: { description: 'Changes exiting material', type: materialType, args: { input: { type: new GraphQLNonNull(updateMaterialType) }, }, resolve: (_: any, inputMaterial: InputMaterial) => { const input = inputMaterial.input; if (!input) { throw new Error('Input is not specified for updateMaterial'); } if (input.materialType && !isValidMaterialTypeSelector(input.materialType)) { throw new Error('MaterialType is existing, but selector is incorrect.'); } const selector = getSelectorFromMaterialDTO(input); let materialSelector = undefined; if (input.materialType) { materialSelector = getSelectorFromMateriaTypelDTO(input.materialType); } const cleanType = omit(input, ['materialType', 'status']); return updateMaterial(selector, cleanType, materialSelector); }, }, deleteMaterial: { description: 'Delete a material', type: GraphQLBoolean, args: { input: { type: new GraphQLNonNull(selectMaterialType) }, }, resolve: (_: any, data: IInputDeleteMaterial) => { const input = data.input; if (!input) { throw new Error('Input is not specified for delete material'); } return deleteMaterial(input); }, }, };