food-graphql-server/src/resolvers.js

135 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-02-01 13:36:26 +00:00
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2021-02-09 20:38:29 +00:00
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
2021-02-01 13:36:26 +00:00
};
var __metadata = (this && this.__metadata) || function (k, v) {
2021-02-09 20:38:29 +00:00
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
2021-02-01 13:36:26 +00:00
};
2021-02-02 10:34:34 +00:00
var __param = (this && this.__param) || function (paramIndex, decorator) {
2021-02-09 20:38:29 +00:00
return function (target, key) { decorator(target, key, paramIndex); }
2021-02-02 10:34:34 +00:00
};
2021-02-01 13:36:26 +00:00
import "reflect-metadata";
2021-02-02 14:08:42 +00:00
import { Resolver, Query, Mutation, Arg, FieldResolver, Root } from "type-graphql";
2021-02-02 15:52:57 +00:00
import { Store, StoreInput, Product, Reservation, ReservationProduct, ReservationInput } from "./typeDefs.js";
import { createStore, getStores, getStore, getStoreProducts, createReservation, getReservationProducts } from "./data.js";
2021-02-02 10:34:34 +00:00
import * as yup from 'yup';
import { UserInputError } from "apollo-server";
// define input validations
2019-02-07 09:21:28 +00:00
const createStoreSchema = yup.object()
.shape({
2021-02-02 10:34:34 +00:00
city: yup.string().max(255).required(),
name: yup.string().max(255).required(),
number: yup.number().required().positive().integer(),
postalCode: yup.string().required().max(10),
street: yup.string().required().max(255),
2019-02-07 09:21:28 +00:00
});
2019-02-07 14:55:48 +00:00
const getStoreSchema = yup.object()
.shape({
2021-02-02 14:08:42 +00:00
id: yup.string().length(36).required()
2019-02-07 14:55:48 +00:00
});
const createReservationSchema = yup.object()
2019-02-07 13:25:20 +00:00
.shape({
2021-02-02 15:52:57 +00:00
reservationProducts: yup.array(yup.object().shape({
2021-02-09 20:38:29 +00:00
productId: yup.string().length(36),
quantity: yup.number().required().positive().integer(),
2021-02-02 15:52:57 +00:00
})),
2019-02-07 13:25:20 +00:00
});
2021-02-01 13:36:26 +00:00
let StoreResolver = class StoreResolver {
2021-02-09 20:38:29 +00:00
async stores() {
return await getStores();
}
async store(id /* ,@Arg("withProducts", { nullable: true }) withProducts: boolean*/) {
// check validity
return await getStoreSchema
.validate({ id: id })
.then(validData => {
return getStore(validData.id);
})
.catch(err => {
return new UserInputError('Invalid input', { validationErrors: err.errors });
});
}
//Extend the Store type with a list of its products.
async products(store) {
return await getStoreProducts(store.id);
}
async createStore(input) {
// check validity
return await createStoreSchema
.validate(input)
.then(validData => {
// put in db with data.js and return the value to grapqhl
return createStore(validData);
})
.catch(err => {
return new UserInputError('Invalid input', { validationErrors: err.errors });
});
}
2021-02-01 13:36:26 +00:00
};
__decorate([
2021-02-09 20:38:29 +00:00
Query(() => [Store], { description: "Get all the stores" }),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
2021-02-01 13:36:26 +00:00
], StoreResolver.prototype, "stores", null);
2021-02-02 14:08:42 +00:00
__decorate([
2021-02-09 20:38:29 +00:00
Query(() => Store, { description: "Get a specific store" }),
__param(0, Arg("id")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String /* ,@Arg("withProducts", { nullable: true }) withProducts: boolean*/]),
__metadata("design:returntype", Promise)
2021-02-02 14:08:42 +00:00
], StoreResolver.prototype, "store", null);
__decorate([
2021-02-09 20:38:29 +00:00
FieldResolver(() => [Product]),
__param(0, Root()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Store]),
__metadata("design:returntype", Promise)
2021-02-02 14:08:42 +00:00
], StoreResolver.prototype, "products", null);
2021-02-02 10:34:34 +00:00
__decorate([
2021-02-09 20:38:29 +00:00
Mutation(() => Store, { description: "Create a new store" }),
__param(0, Arg("input")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [StoreInput]),
__metadata("design:returntype", Promise)
2021-02-02 10:34:34 +00:00
], StoreResolver.prototype, "createStore", null);
2021-02-01 13:36:26 +00:00
StoreResolver = __decorate([
2021-02-09 20:38:29 +00:00
Resolver(() => Store)
2021-02-01 13:36:26 +00:00
], StoreResolver);
export { StoreResolver };
2021-02-02 15:52:57 +00:00
let ReservationResolver = class ReservationResolver {
2021-02-09 20:38:29 +00:00
async reservationProducts(reservation) {
return await getReservationProducts(reservation.id);
}
async createReservation(input) {
return await createReservationSchema
.validate(input)
.then(validData => {
return createReservation(validData);
})
.catch(err => {
return new UserInputError('Invalid input', { validationErrors: err.errors });
});
}
2021-02-02 15:52:57 +00:00
};
__decorate([
2021-02-09 20:38:29 +00:00
FieldResolver(() => [ReservationProduct]),
__param(0, Root()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Reservation]),
__metadata("design:returntype", Promise)
2021-02-02 15:52:57 +00:00
], ReservationResolver.prototype, "reservationProducts", null);
__decorate([
2021-02-09 20:38:29 +00:00
Mutation(() => Reservation, { description: "Create a new reservation" }),
__param(0, Arg("input")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [ReservationInput]),
__metadata("design:returntype", Promise)
2021-02-02 15:52:57 +00:00
], ReservationResolver.prototype, "createReservation", null);
ReservationResolver = __decorate([
2021-02-09 20:38:29 +00:00
Resolver(() => Reservation)
2021-02-02 15:52:57 +00:00
], ReservationResolver);
export { ReservationResolver };