Compare commits
13 commits
FRE-1-gett
...
master
Author | SHA1 | Date | |
---|---|---|---|
Fre Timmerman | d8910ee98d | ||
Fre Timmerman | 3f70e2a67e | ||
Fre Timmerman | c7ee3ffb7c | ||
Fre Timmerman | 54b2e99fd2 | ||
Fre Timmerman | 40d325a318 | ||
b77421c8cd | |||
7ce4e4a7bc | |||
971e366a1b | |||
f5070142cb | |||
5c2e07e308 | |||
281ea4112a | |||
45f7debb77 | |||
c4b07ea259 |
|
@ -3,6 +3,8 @@
|
|||
To get up-and-running, install the node dependencies and start the server.
|
||||
🚀 Go to http://localhost:4000/ to play in the GraphQL Playground.
|
||||
|
||||
Slides: https://app.box.com/s/6l6xi3mm26d7ur9g7n05egguc6dkyz3v
|
||||
|
||||
```
|
||||
npm i
|
||||
npm start
|
||||
|
|
60
gql commands
Normal file
60
gql commands
Normal file
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
stores{
|
||||
id,
|
||||
name,
|
||||
city,
|
||||
number,
|
||||
postalCode,
|
||||
street,
|
||||
products{description,name,price}
|
||||
}
|
||||
}
|
||||
|
||||
mutation{
|
||||
createStore(
|
||||
input:{
|
||||
name: "asdf",
|
||||
city: "asdf",
|
||||
number:2,
|
||||
postalCode: "hallohallo",
|
||||
street: "asdf"
|
||||
}
|
||||
) {
|
||||
id,
|
||||
name,
|
||||
city,
|
||||
number,
|
||||
postalCode,
|
||||
street
|
||||
}
|
||||
}
|
||||
|
||||
query getStore($storeId: String!, $withProducts: Boolean!){
|
||||
store (id:$storeId){
|
||||
id,
|
||||
name,
|
||||
city,
|
||||
number,
|
||||
postalCode,
|
||||
street,
|
||||
products @include(if: $withProducts) {id,description,name,price}
|
||||
}
|
||||
}
|
||||
var {"storeId": "5f2919aa-333a-4745-8166-3002ab30de0e","withProducts":true}
|
||||
|
||||
mutation($productId:String!){
|
||||
createReservation(
|
||||
input:{
|
||||
reservationProducts:
|
||||
[
|
||||
{
|
||||
productId:$productId,
|
||||
quantity:2
|
||||
}
|
||||
]
|
||||
}
|
||||
) {
|
||||
id,date,reservationProducts{product{name,description,price},quantity}
|
||||
}
|
||||
}
|
||||
var {"productId":"5bb3fbcc-7ec2-44fe-a04b-a0251cecf1e6"}
|
|
@ -16,7 +16,9 @@
|
|||
"graphql": "^15.5.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"type-graphql": "^1.1.1",
|
||||
"uuid": "^3.3.2"
|
||||
"typescript": "^4.1.3",
|
||||
"uuid": "^3.3.2",
|
||||
"yup": "^0.32.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsc-watch": "^4.2.9"
|
||||
|
|
66
schema.gql
66
schema.gql
|
@ -3,14 +3,68 @@
|
|||
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
|
||||
# -----------------------------------------------
|
||||
|
||||
"""
|
||||
The javascript `Date` as string. Type represents date and time as the ISO Date string.
|
||||
"""
|
||||
scalar DateTime
|
||||
|
||||
type Mutation {
|
||||
"""Create a new reservation"""
|
||||
createReservation(input: ReservationInput): Reservation
|
||||
|
||||
"""Create a new store"""
|
||||
createStore(input: StoreInput): Store
|
||||
}
|
||||
|
||||
type Product {
|
||||
description: String
|
||||
id: String
|
||||
name: String
|
||||
price: Float
|
||||
}
|
||||
|
||||
type Query {
|
||||
"""Get all the stores """
|
||||
stores: [Store!]!
|
||||
"""Get a specific store"""
|
||||
store(id: String): Store
|
||||
|
||||
"""Get all the stores"""
|
||||
stores: [Store]
|
||||
}
|
||||
|
||||
type Reservation {
|
||||
date: DateTime
|
||||
id: String
|
||||
reservationProducts: [ReservationProduct]
|
||||
}
|
||||
|
||||
input ReservationInput {
|
||||
reservationProducts: [ReservationProductInput]
|
||||
}
|
||||
|
||||
type ReservationProduct {
|
||||
product: Product
|
||||
quantity: Int
|
||||
}
|
||||
|
||||
input ReservationProductInput {
|
||||
productId: String
|
||||
quantity: Int
|
||||
}
|
||||
|
||||
type Store {
|
||||
id: ID!
|
||||
|
||||
"""The name of the store"""
|
||||
name: String!
|
||||
city: String
|
||||
id: String
|
||||
name: String
|
||||
number: Int
|
||||
postalCode: String
|
||||
products: [Product]
|
||||
street: String
|
||||
}
|
||||
|
||||
input StoreInput {
|
||||
city: String
|
||||
name: String
|
||||
number: Int
|
||||
postalCode: String
|
||||
street: String
|
||||
}
|
||||
|
|
104
src/data.js
104
src/data.js
|
@ -1,22 +1,114 @@
|
|||
import * as uuid from 'uuid';
|
||||
import uuid from 'uuid';
|
||||
|
||||
const stores = [
|
||||
{
|
||||
city: 'Aalst',
|
||||
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
|
||||
name: 'Den Olijfboom',
|
||||
number: 38,
|
||||
postalCode: '9300',
|
||||
street: 'Molenstraat',
|
||||
},
|
||||
{
|
||||
city: 'Aalst',
|
||||
id: '5f2919aa-333a-4745-8166-3002ab30de0e',
|
||||
name: 'Pizza Talia'
|
||||
name: 'Pizza Talia',
|
||||
number: 147,
|
||||
postalCode: '9300',
|
||||
street: 'Sint Jobstraat',
|
||||
}
|
||||
];
|
||||
|
||||
export function createStore({ name }) {
|
||||
const newStore = { id: uuid(), name };
|
||||
const products = [{
|
||||
description: 'Een broodje om u tegen te zeggen. Lekker, lekker, lekker!',
|
||||
id: '9e3de707-8e96-45c8-8c1a-75d79fe74768',
|
||||
name: 'Scampi Manis',
|
||||
price: 5,
|
||||
storeId: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
|
||||
}, {
|
||||
description: 'Eentje met kaas en fricandon, voor den groten honger.',
|
||||
id: '1535f2a6-4341-4db0-a9c8-455c2347dcaf',
|
||||
name: 'Gitaar',
|
||||
price: 5,
|
||||
storeId: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
|
||||
}, {
|
||||
description: 'Tomatensaus, kaas, ham, salami, champignons, paprika',
|
||||
id: '5bb3fbcc-7ec2-44fe-a04b-a0251cecf1e6',
|
||||
name: '4 Seizoenen Large',
|
||||
price: 17,
|
||||
storeId: '5f2919aa-333a-4745-8166-3002ab30de0e',
|
||||
}, {
|
||||
description: 'Bolognaisesaus, kaas, paprika, ham, salami, champignons',
|
||||
id: '037e74f6-ae73-47a3-9acf-d1de0cdcd565',
|
||||
name: 'Calzone Large',
|
||||
price: 17,
|
||||
storeId: '5f2919aa-333a-4745-8166-3002ab30de0e',
|
||||
}, {
|
||||
description: 'Tomatensaus, pepperoni, rode ui, paprika gehakt, jalapenos',
|
||||
id: 'e8619184-2e86-4db5-b8ba-596720006a0f',
|
||||
name: 'Hot \'n Spicy',
|
||||
price: 20,
|
||||
storeId: '5f2919aa-333a-4745-8166-3002ab30de0e',
|
||||
}];
|
||||
|
||||
const reservations = [];
|
||||
|
||||
let reservationProducts = [];
|
||||
|
||||
|
||||
export function createStore({ city, name, number, postalCode, street }) {
|
||||
const newStore = {
|
||||
city,
|
||||
id: uuid(),
|
||||
name,
|
||||
number,
|
||||
postalCode,
|
||||
street,
|
||||
};
|
||||
stores.push(newStore);
|
||||
return newStore;
|
||||
}
|
||||
|
||||
export function getStores() {
|
||||
function getStore(storeId) {
|
||||
return stores.find((s) => s.id === storeId);
|
||||
}
|
||||
|
||||
function getStores() {
|
||||
return stores;
|
||||
}
|
||||
}
|
||||
|
||||
export function getStore(storeId) {
|
||||
return stores.find(store => store.id === storeId);
|
||||
}
|
||||
|
||||
export function getStoreProducts(storeId) {
|
||||
return products.filter(prod => prod.storeId === storeId)
|
||||
}
|
||||
|
||||
export function getReservationProducts(reservationId) {
|
||||
return reservationProducts
|
||||
.filter(rp => rp.reservationId === reservationId)
|
||||
.map(rp => ({
|
||||
product: products.find(p => p.id === rp.productId),
|
||||
quantity: rp.quantity
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function createReservation(reservation) {
|
||||
//create new reservation and add it to db
|
||||
const reservationId = uuid();
|
||||
const newReservation = {
|
||||
id: reservationId,
|
||||
date: new Date(),
|
||||
};
|
||||
reservations.push(newReservation);
|
||||
//add the reservationID to the reservationproducts, then add them to the db
|
||||
reservationProducts = reservationProducts.concat(
|
||||
reservation.reservationProducts.map(rp => ({
|
||||
...rp,
|
||||
reservationId
|
||||
})
|
||||
));
|
||||
return newReservation;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import { buildSchema } from "type-graphql";
|
|||
|
||||
import * as path from "path";
|
||||
import { ApolloServer, gql } from "apollo-server";
|
||||
import { StoreResolver } from './resolvers.js';
|
||||
import { StoreResolver, ReservationResolver } from './resolvers.js';
|
||||
|
||||
// ⚽️ Goal
|
||||
// --------
|
||||
|
@ -15,7 +15,7 @@ import { StoreResolver } from './resolvers.js';
|
|||
// a store, we get a list of products (query) of that store.
|
||||
// We pick some products, pick a quantity, and we make a reservation (mutation)
|
||||
|
||||
// 🏪 Exercise 1
|
||||
// 🏪 Exercise 4
|
||||
// --------------
|
||||
|
||||
// 1) First we create two files. One for our type definitions, `typeDefs.js`,
|
||||
|
@ -29,7 +29,8 @@ import { StoreResolver } from './resolvers.js';
|
|||
|
||||
// create the schema using TypeGraphQL, pass the resolver
|
||||
const schema = await buildSchema({
|
||||
resolvers: [StoreResolver],
|
||||
resolvers: [StoreResolver, ReservationResolver],
|
||||
nullableByDefault: true,
|
||||
emitSchemaFile: path.resolve(".", "schema.gql"),
|
||||
});
|
||||
|
||||
|
|
141
src/resolvers.js
141
src/resolvers.js
|
@ -1,31 +1,134 @@
|
|||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
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;
|
||||
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;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
import "reflect-metadata";
|
||||
import { Resolver, Query } from "type-graphql";
|
||||
import { Store } from "./typeDefs.js";
|
||||
import { getStores } from "./data.js";
|
||||
import { Resolver, Query, Mutation, Arg, FieldResolver, Root } from "type-graphql";
|
||||
import { Store, StoreInput, Product, Reservation, ReservationProduct, ReservationInput } from "./typeDefs.js";
|
||||
import { createStore, getStores, getStore, getStoreProducts, createReservation, getReservationProducts } from "./data.js";
|
||||
import * as yup from 'yup';
|
||||
import { UserInputError } from "apollo-server";
|
||||
// define input validations
|
||||
const createStoreSchema = yup.object()
|
||||
.shape({
|
||||
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),
|
||||
});
|
||||
const getStoreSchema = yup.object()
|
||||
.shape({
|
||||
id: yup.string().length(36).required()
|
||||
});
|
||||
const createReservationSchema = yup.object()
|
||||
.shape({
|
||||
reservationProducts: yup.array(yup.object().shape({
|
||||
productId: yup.string().length(36),
|
||||
quantity: yup.number().required().positive().integer(),
|
||||
})),
|
||||
});
|
||||
let StoreResolver = class StoreResolver {
|
||||
constructor() {
|
||||
this.storeCollection = getStores();
|
||||
}
|
||||
async stores() {
|
||||
return await this.storeCollection;
|
||||
}
|
||||
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 });
|
||||
});
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
Query(returns => [Store], { description: "Get all the stores " }),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", Promise)
|
||||
Query(() => [Store], { description: "Get all the stores" }),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", Promise)
|
||||
], StoreResolver.prototype, "stores", null);
|
||||
__decorate([
|
||||
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)
|
||||
], StoreResolver.prototype, "store", null);
|
||||
__decorate([
|
||||
FieldResolver(() => [Product]),
|
||||
__param(0, Root()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Store]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], StoreResolver.prototype, "products", null);
|
||||
__decorate([
|
||||
Mutation(() => Store, { description: "Create a new store" }),
|
||||
__param(0, Arg("input")),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [StoreInput]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], StoreResolver.prototype, "createStore", null);
|
||||
StoreResolver = __decorate([
|
||||
Resolver()
|
||||
Resolver(() => Store)
|
||||
], StoreResolver);
|
||||
export { StoreResolver };
|
||||
let ReservationResolver = class ReservationResolver {
|
||||
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 });
|
||||
});
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
FieldResolver(() => [ReservationProduct]),
|
||||
__param(0, Root()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Reservation]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], ReservationResolver.prototype, "reservationProducts", null);
|
||||
__decorate([
|
||||
Mutation(() => Reservation, { description: "Create a new reservation" }),
|
||||
__param(0, Arg("input")),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [ReservationInput]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], ReservationResolver.prototype, "createReservation", null);
|
||||
ReservationResolver = __decorate([
|
||||
Resolver(() => Reservation)
|
||||
], ReservationResolver);
|
||||
export { ReservationResolver };
|
||||
|
|
|
@ -1,14 +1,95 @@
|
|||
import "reflect-metadata";
|
||||
import { Resolver, Query } from "type-graphql";
|
||||
import { Store } from "./typeDefs.js";
|
||||
import { getStores } from "./data.js";
|
||||
import { Resolver, Query, Mutation, Arg, FieldResolver, Root } from "type-graphql";
|
||||
import { Store, StoreInput, Product, Reservation, ReservationProduct, ReservationInput } from "./typeDefs.js";
|
||||
import { createStore, getStores, getStore, getStoreProducts, createReservation, getReservationProducts } from "./data.js";
|
||||
import * as yup from 'yup';
|
||||
import { UserInputError } from "apollo-server";
|
||||
|
||||
@Resolver()
|
||||
// define input validations
|
||||
const createStoreSchema = yup.object()
|
||||
.shape({
|
||||
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),
|
||||
});
|
||||
|
||||
const getStoreSchema = yup.object()
|
||||
.shape({
|
||||
id: yup.string().length(36).required()
|
||||
});
|
||||
|
||||
const createReservationSchema = yup.object()
|
||||
.shape({
|
||||
reservationProducts: yup.array(
|
||||
yup.object().shape({
|
||||
productId: yup.string().length(36),
|
||||
quantity: yup.number().required().positive().integer(),
|
||||
})
|
||||
),
|
||||
});
|
||||
|
||||
@Resolver(() => Store)
|
||||
export class StoreResolver {
|
||||
private storeCollection: Store[] = getStores();
|
||||
|
||||
@Query(returns => [Store], { description: "Get all the stores " })
|
||||
@Query(() => [Store], { description: "Get all the stores" })
|
||||
async stores(): Promise<Store[]> {
|
||||
return await this.storeCollection;
|
||||
return await getStores();
|
||||
}
|
||||
|
||||
@Query(() => Store, { description: "Get a specific store" })
|
||||
async store(@Arg("id") id: String/* ,@Arg("withProducts", { nullable: true }) withProducts: boolean*/): Promise<Store> {
|
||||
// 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.
|
||||
@FieldResolver(() => [Product])
|
||||
async products(@Root() store: Store): Promise<[Product]> {
|
||||
return await getStoreProducts(store.id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Mutation(() => Store, { description: "Create a new store" })
|
||||
async createStore(@Arg("input") input: StoreInput): Promise<Store | UserInputError> {
|
||||
// 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 });
|
||||
});
|
||||
}
|
||||
}
|
||||
@Resolver(() => Reservation)
|
||||
export class ReservationResolver {
|
||||
|
||||
@FieldResolver(() => [ReservationProduct])
|
||||
async reservationProducts(@Root() reservation: Reservation) {
|
||||
return await getReservationProducts(reservation.id);
|
||||
}
|
||||
|
||||
@Mutation(() => Reservation, { description: "Create a new reservation" })
|
||||
async createReservation(@Arg("input") input: ReservationInput): Promise<Reservation | UserInputError> {
|
||||
return await createReservationSchema
|
||||
.validate(input)
|
||||
.then(validData => {
|
||||
return createReservation(validData);
|
||||
})
|
||||
.catch(err => {
|
||||
return new UserInputError('Invalid input', { validationErrors: err.errors });
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
65
src/scalars/dateTime.js
Normal file
65
src/scalars/dateTime.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const { GraphQLScalarType } = require('graphql');
|
||||
const { GraphQLError } = require('graphql/error')
|
||||
const { Kind } = require('graphql/language');
|
||||
|
||||
module.exports = new GraphQLScalarType({
|
||||
name: 'DateTime',
|
||||
description: 'Use JavaScript Date object for date/time fields.',
|
||||
serialize(value) {
|
||||
let v = value;
|
||||
|
||||
if (!(v instanceof Date) && typeof v !== 'string' && typeof v !== 'number') {
|
||||
throw new TypeError(
|
||||
`Value is not an instance of Date, Date string or number: ${v}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof v === 'string') {
|
||||
v = new Date();
|
||||
|
||||
v.setTime(Date.parse(value));
|
||||
} else if (typeof v === 'number') {
|
||||
v = new Date(v);
|
||||
}
|
||||
|
||||
if (Number.isNaN(v.getTime())) {
|
||||
throw new TypeError(`Value is not a valid Date: ${v}`);
|
||||
}
|
||||
|
||||
return v.toJSON();
|
||||
},
|
||||
|
||||
parseValue(value) {
|
||||
const date = new Date(value);
|
||||
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
throw new TypeError(`Value is not a valid Date: ${value}`);
|
||||
}
|
||||
|
||||
return date;
|
||||
},
|
||||
|
||||
parseLiteral(ast) {
|
||||
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
|
||||
throw new GraphQLError(
|
||||
`Can only parse strings & integers to dates but got a: ${ast.kind}`,
|
||||
);
|
||||
}
|
||||
|
||||
const result = new Date(ast.kind === Kind.INT ? Number(ast.value) : ast.value);
|
||||
|
||||
if (Number.isNaN(result.getTime())) {
|
||||
throw new GraphQLError(`Value is not a valid Date: ${ast.value}`);
|
||||
}
|
||||
|
||||
if (ast.kind === Kind.STRING && ast.value !== result.toJSON()) {
|
||||
throw new GraphQLError(
|
||||
`Value is not a valid Date format (YYYY-MM-DDTHH:MM:SS.SSSZ): ${
|
||||
ast.value
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
130
src/typeDefs.js
130
src/typeDefs.js
|
@ -10,18 +10,142 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
import "reflect-metadata";
|
||||
import { ObjectType, Field, ID } from "type-graphql";
|
||||
import { ObjectType, Field, InputType, Int, Float } from "type-graphql";
|
||||
let Store = class Store {
|
||||
};
|
||||
__decorate([
|
||||
Field(() => ID),
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Store.prototype, "id", void 0);
|
||||
__decorate([
|
||||
Field(() => String, { description: "The name of the store" }),
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Store.prototype, "name", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Store.prototype, "city", void 0);
|
||||
__decorate([
|
||||
Field(type => Int),
|
||||
__metadata("design:type", Number)
|
||||
], Store.prototype, "number", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Store.prototype, "postalCode", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Store.prototype, "street", void 0);
|
||||
__decorate([
|
||||
Field(type => [Product]),
|
||||
__metadata("design:type", Array)
|
||||
], Store.prototype, "products", void 0);
|
||||
Store = __decorate([
|
||||
ObjectType()
|
||||
], Store);
|
||||
export { Store };
|
||||
let StoreInput = class StoreInput {
|
||||
};
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], StoreInput.prototype, "name", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], StoreInput.prototype, "city", void 0);
|
||||
__decorate([
|
||||
Field(type => Int),
|
||||
__metadata("design:type", Number)
|
||||
], StoreInput.prototype, "number", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], StoreInput.prototype, "postalCode", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], StoreInput.prototype, "street", void 0);
|
||||
StoreInput = __decorate([
|
||||
InputType()
|
||||
], StoreInput);
|
||||
export { StoreInput };
|
||||
let Product = class Product {
|
||||
};
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Product.prototype, "id", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Product.prototype, "name", void 0);
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Product.prototype, "description", void 0);
|
||||
__decorate([
|
||||
Field(type => Float),
|
||||
__metadata("design:type", Number)
|
||||
], Product.prototype, "price", void 0);
|
||||
Product = __decorate([
|
||||
ObjectType()
|
||||
], Product);
|
||||
export { Product };
|
||||
let Reservation = class Reservation {
|
||||
};
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], Reservation.prototype, "id", void 0);
|
||||
__decorate([
|
||||
Field(type => [ReservationProduct]),
|
||||
__metadata("design:type", Array)
|
||||
], Reservation.prototype, "reservationProducts", void 0);
|
||||
__decorate([
|
||||
Field(type => Date),
|
||||
__metadata("design:type", Date)
|
||||
], Reservation.prototype, "date", void 0);
|
||||
Reservation = __decorate([
|
||||
ObjectType()
|
||||
], Reservation);
|
||||
export { Reservation };
|
||||
let ReservationProduct = class ReservationProduct {
|
||||
};
|
||||
__decorate([
|
||||
Field(type => Product),
|
||||
__metadata("design:type", Product)
|
||||
], ReservationProduct.prototype, "product", void 0);
|
||||
__decorate([
|
||||
Field(type => Int),
|
||||
__metadata("design:type", Number)
|
||||
], ReservationProduct.prototype, "quantity", void 0);
|
||||
ReservationProduct = __decorate([
|
||||
ObjectType()
|
||||
], ReservationProduct);
|
||||
export { ReservationProduct };
|
||||
let ReservationInput = class ReservationInput {
|
||||
};
|
||||
__decorate([
|
||||
Field(type => [ReservationProductInput]),
|
||||
__metadata("design:type", Array)
|
||||
], ReservationInput.prototype, "reservationProducts", void 0);
|
||||
ReservationInput = __decorate([
|
||||
InputType()
|
||||
], ReservationInput);
|
||||
export { ReservationInput };
|
||||
let ReservationProductInput = class ReservationProductInput {
|
||||
};
|
||||
__decorate([
|
||||
Field(),
|
||||
__metadata("design:type", String)
|
||||
], ReservationProductInput.prototype, "productId", void 0);
|
||||
__decorate([
|
||||
Field(type => Int),
|
||||
__metadata("design:type", Number)
|
||||
], ReservationProductInput.prototype, "quantity", void 0);
|
||||
ReservationProductInput = __decorate([
|
||||
InputType()
|
||||
], ReservationProductInput);
|
||||
export { ReservationProductInput };
|
||||
|
|
|
@ -2,14 +2,81 @@
|
|||
// which ways the data can be fetched from the GraphQL server.
|
||||
|
||||
import "reflect-metadata";
|
||||
import { ObjectType, Field, ID } from "type-graphql";
|
||||
import { ObjectType, Field, InputType, Int, Float } from "type-graphql";
|
||||
|
||||
|
||||
@ObjectType()
|
||||
export class Store {
|
||||
@Field(() => ID)
|
||||
@Field()
|
||||
id: string;
|
||||
|
||||
@Field(() => String, { description: "The name of the store" })
|
||||
@Field()
|
||||
name: string;
|
||||
@Field()
|
||||
city: string;
|
||||
@Field(type => Int)
|
||||
number: number;
|
||||
@Field()
|
||||
postalCode: string
|
||||
@Field()
|
||||
street: string
|
||||
@Field(type => [Product])
|
||||
products?: [Product]
|
||||
}
|
||||
@InputType()
|
||||
export class StoreInput {
|
||||
@Field()
|
||||
name: string;
|
||||
@Field()
|
||||
city: string;
|
||||
@Field(type => Int)
|
||||
number: number;
|
||||
@Field()
|
||||
postalCode: string
|
||||
@Field()
|
||||
street: string
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class Product {
|
||||
@Field()
|
||||
id: string;
|
||||
@Field()
|
||||
name: string;
|
||||
@Field()
|
||||
description: string;
|
||||
@Field(type => Float)
|
||||
price: number;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class Reservation {
|
||||
@Field()
|
||||
id: String;
|
||||
@Field(type => [ReservationProduct])
|
||||
reservationProducts: [ReservationProduct];
|
||||
@Field(type => Date)
|
||||
date: Date;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class ReservationProduct {
|
||||
@Field(type => Product)
|
||||
product: Product;
|
||||
@Field(type => Int)
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
|
||||
@InputType()
|
||||
export class ReservationInput {
|
||||
@Field(type => [ReservationProductInput])
|
||||
reservationProducts: [ReservationProductInput];
|
||||
}
|
||||
|
||||
@InputType()
|
||||
export class ReservationProductInput {
|
||||
@Field()
|
||||
productId: string;
|
||||
@Field(type => Int)
|
||||
quantity: number;
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
],
|
||||
"moduleResolution": "node",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true
|
||||
"emitDecoratorMetadata": true,
|
||||
"allowSyntheticDefaultImports": true
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue