Merge branch 'FRE-extra'

This commit is contained in:
Fre Timmerman 2021-02-09 21:38:29 +01:00
commit d8910ee98d
10 changed files with 649 additions and 181 deletions

60
gql commands Normal file
View 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"}

View File

@ -1,21 +1,26 @@
{ {
"name": "food-graphql-server", "name": "food-graphql-server",
"type": "module",
"version": "1.0.0", "version": "1.0.0",
"description": "The GraphQL server to reserve food.", "description": "The GraphQL server to reserve food.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "nodemon src/index.js" "start": "tsc-watch --onsuccess \"node ./src/index.js\""
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"apollo-server": "^2.3.1", "apollo-server": "^2.19.2",
"graphql": "^14.1.1", "class-validator": "^0.13.1",
"graphql": "^15.5.0",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1",
"typescript": "^4.1.3",
"uuid": "^3.3.2", "uuid": "^3.3.2",
"yup": "^0.26.10" "yup": "^0.32.8"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^1.18.9" "tsc-watch": "^4.2.9"
} }
} }

70
schema.gql Normal file
View File

@ -0,0 +1,70 @@
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! 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 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 {
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
}

View File

@ -1,4 +1,4 @@
const uuid = require('uuid'); import uuid from 'uuid';
const stores = [ const stores = [
{ {
@ -55,7 +55,8 @@ const reservations = [];
let reservationProducts = []; let reservationProducts = [];
function createStore({ city, name, number, postalCode, street }) {
export function createStore({ city, name, number, postalCode, street }) {
const newStore = { const newStore = {
city, city,
id: uuid(), id: uuid(),
@ -76,41 +77,38 @@ function getStores() {
return stores; return stores;
} }
function getStoreProducts(storeId) { export function getStore(storeId) {
return products.filter((p) => p.storeId === storeId) return stores.find(store => store.id === storeId);
} }
function getReservationProducts(reservationId) { export function getStoreProducts(storeId) {
return products.filter(prod => prod.storeId === storeId)
}
export function getReservationProducts(reservationId) {
return reservationProducts return reservationProducts
// Join .filter(rp => rp.reservationId === reservationId)
.filter((rp) => rp.reservationId === reservationId) .map(rp => ({
.map((rp) => ({
product: products.find(p => p.id === rp.productId), product: products.find(p => p.id === rp.productId),
quantity: rp.quantity quantity: rp.quantity
})); })
);
} }
function createReservation(reservation) { export function createReservation(reservation) {
//create new reservation and add it to db
const reservationId = uuid(); const reservationId = uuid();
const newReservation = { const newReservation = {
date: new Date(),
id: reservationId, id: reservationId,
date: new Date(),
}; };
reservations.push(newReservation); reservations.push(newReservation);
//add the reservationID to the reservationproducts, then add them to the db
reservationProducts = reservationProducts.concat( reservationProducts = reservationProducts.concat(
reservation.reservationProducts.map((reservationProduct) => ({ reservation.reservationProducts.map(rp => ({
...reservationProduct, ...rp,
reservationId reservationId
}) })
)); ));
return newReservation; return newReservation;
} }
module.exports = {
createStore,
createReservation,
getStore,
getStores,
getStoreProducts,
getReservationProducts
};

View File

@ -1,6 +1,10 @@
const { ApolloServer, gql } = require('apollo-server');
const resolvers = require('./resolvers'); import "reflect-metadata";
const typeDefs = require('./typeDefs'); import { buildSchema } from "type-graphql";
import * as path from "path";
import { ApolloServer, gql } from "apollo-server";
import { StoreResolver, ReservationResolver } from './resolvers.js';
// ⚽️ Goal // ⚽️ Goal
// -------- // --------
@ -14,22 +18,26 @@ const typeDefs = require('./typeDefs');
// 🏪 Exercise 4 // 🏪 Exercise 4
// -------------- // --------------
// Now we will focus on creating a reservation, which looks like this: // 1) First we create two files. One for our type definitions, `typeDefs.js`,
// { id, date, reservationProducts: { product: { id, name price }, quantity })). // and one for our resolver functions, `resolvers.js`.
// 2) Create a GraphQL type definition for our store. A store has an id and a name.
// 3) Create a Query `stores` to get a list of stores.
// 4) Create a resolver function that returns the list of stores.
// 5) Try out the GraphQL query in the GraphQL Playground (🚀 http://localhost:4000/)
// 1) Create a Mutation to create a reservation `createReservation`
// that takes an object as input. The input type is named `ReservationInput`.
// ReservationInput contains a list of { productId, quantity } (input type is named `ReservationProductInput`), // create the schema using TypeGraphQL, pass the resolver
// named `reservationProducts` (field of `ReservationInput`). const schema = await buildSchema({
// 2) Now create a resolver function for the mutation and insert it into our in-memory database. resolvers: [StoreResolver, ReservationResolver],
// 3) Create a query field reservationProducts under Reservation, to get the reservationProducts [{ product, quantity }]. nullableByDefault: true,
// Similar as stores under Query. emitSchemaFile: path.resolve(".", "schema.gql"),
// 4) Go to the GraphQL Playground and try out the createReservation mutation! });
// In the most basic sense, the ApolloServer can be started // In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers // by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types. // responsible for fetching the data for those types.
const server = new ApolloServer({ typeDefs, resolvers }); const server = new ApolloServer({ schema });
// This `listen` method launches a web-server. Existing apps // This `listen` method launches a web-server. Existing apps
// can utilize middleware options. // can utilize middleware options.

View File

@ -1,82 +1,134 @@
const { UserInputError } = require('apollo-server'); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
const yup = require('yup'); var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
const dateTimeScalar = require('./scalars/dateTime'); if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
const data = require('./data'); 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;
const uuidSchema = yup.string().min(36).max(36); };
var __metadata = (this && this.__metadata) || function (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, 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() const createStoreSchema = yup.object()
.shape({ .shape({
city: yup.string().max(255), city: yup.string().max(255).required(),
name: yup.string().min(1).max(255).required(), name: yup.string().max(255).required(),
number: yup number: yup.number().required().positive().integer(),
.number() postalCode: yup.string().required().max(10),
.required() street: yup.string().required().max(255),
.positive()
.integer(),
postalCode: yup.string().max(10),
street: yup.string().max(255),
}); });
const getStoreSchema = yup.object() const getStoreSchema = yup.object()
.shape({ .shape({
id: uuidSchema, id: yup.string().length(36).required()
}); });
const createReservationSchema = yup.object() const createReservationSchema = yup.object()
.shape({ .shape({
reservationProducts: yup.array( reservationProducts: yup.array(yup.object().shape({
yup.object().shape({ productId: yup.string().length(36),
productId: uuidSchema, quantity: yup.number().required().positive().integer(),
quantity: yup.number().required().positive().integer(), })),
})
),
}); });
let StoreResolver = class StoreResolver {
// Resolvers define the technique for fetching the types in the async stores() {
// schema. return await getStores();
module.exports = { }
Query: { async store(id /* ,@Arg("withProducts", { nullable: true }) withProducts: boolean*/) {
stores: () => data.getStores(), // check validity
store: async (parent, input) => { return await getStoreSchema
try { .validate({ id: id })
await getStoreSchema.validate(input); .then(validData => {
} catch (e) { return getStore(validData.id);
throw new UserInputError('Invalid input.', { validationErrors: e.errors }); })
} .catch(err => {
const { id } = input; return new UserInputError('Invalid input', { validationErrors: err.errors });
return data.getStore(id); });
}, }
}, //Extend the Store type with a list of its products.
Reservation: { async products(store) {
reservationProducts: async ({ id }) => { return await getStoreProducts(store.id);
return data.getReservationProducts(id); }
} async createStore(input) {
}, // check validity
Store: { return await createStoreSchema
products: async ({ id: storeId }) => { .validate(input)
return data.getStoreProducts(storeId); .then(validData => {
} // put in db with data.js and return the value to grapqhl
}, return createStore(validData);
Mutation: { })
createStore: async (parent, { input }) => { .catch(err => {
try { return new UserInputError('Invalid input', { validationErrors: err.errors });
await createStoreSchema.validate(input); });
} catch (e) { }
throw new UserInputError('Invalid input.', { validationErrors: e.errors }); };
} __decorate([
const { city, name, number, postalCode, street } = input; Query(() => [Store], { description: "Get all the stores" }),
return data.createStore({ city, name, number, postalCode, street }); __metadata("design:type", Function),
}, __metadata("design:paramtypes", []),
createReservation: async (parent, { input }) => { __metadata("design:returntype", Promise)
try { ], StoreResolver.prototype, "stores", null);
await createReservationSchema.validate(input); __decorate([
} catch (e) { Query(() => Store, { description: "Get a specific store" }),
throw new UserInputError('Invalid input.', { validationErrors: e.errors }); __param(0, Arg("id")),
} __metadata("design:type", Function),
const { reservationProducts } = input; __metadata("design:paramtypes", [String /* ,@Arg("withProducts", { nullable: true }) withProducts: boolean*/]),
return data.createReservation({ reservationProducts }); __metadata("design:returntype", Promise)
} ], StoreResolver.prototype, "store", null);
}, __decorate([
DateTime: dateTimeScalar, 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(() => 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 };

95
src/resolvers.ts Normal file
View File

@ -0,0 +1,95 @@
import "reflect-metadata";
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(),
})
),
});
@Resolver(() => Store)
export class StoreResolver {
@Query(() => [Store], { description: "Get all the stores" })
async stores(): Promise<Store[]> {
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 });
})
}
}

View File

@ -1,67 +1,151 @@
const { gql } = require('apollo-server');
// Type definitions define the "shape" of your data and specify // Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server. // which ways the data can be fetched from the GraphQL server.
module.exports = gql` var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
scalar DateTime 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);
# Comments in GraphQL are defined with the hash (#) symbol. 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;
type Product { };
description: String var __metadata = (this && this.__metadata) || function (k, v) {
id: ID if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
name: String };
price: Float import "reflect-metadata";
} import { ObjectType, Field, InputType, Int, Float } from "type-graphql";
let Store = class Store {
# This "Store" type can be used in other type declarations. };
type Store { __decorate([
city: String Field(),
id: ID __metadata("design:type", String)
name: String ], Store.prototype, "id", void 0);
number: Int __decorate([
postalCode: String Field(),
products: [Product] __metadata("design:type", String)
street: String ], Store.prototype, "name", void 0);
} __decorate([
Field(),
type ReservationProduct { __metadata("design:type", String)
product: Product ], Store.prototype, "city", void 0);
quantity: Int __decorate([
} Field(type => Int),
__metadata("design:type", Number)
type Reservation { ], Store.prototype, "number", void 0);
date: DateTime __decorate([
id: ID Field(),
reservationProducts: [ReservationProduct] __metadata("design:type", String)
} ], Store.prototype, "postalCode", void 0);
__decorate([
# The "Query" type is the root of all GraphQL queries. Field(),
# (A "Mutation" type will be covered later on.) __metadata("design:type", String)
type Query { ], Store.prototype, "street", void 0);
stores: [Store] __decorate([
store(id: ID): Store Field(type => [Product]),
} __metadata("design:type", Array)
], Store.prototype, "products", void 0);
input StoreInput { Store = __decorate([
city: String ObjectType()
name: String! ], Store);
number: Int export { Store };
postalCode: String let StoreInput = class StoreInput {
street: String };
} __decorate([
Field(),
input ReservationProductInput { __metadata("design:type", String)
productId: ID ], StoreInput.prototype, "name", void 0);
quantity: Int __decorate([
} Field(),
__metadata("design:type", String)
input ReservationInput { ], StoreInput.prototype, "city", void 0);
reservationProducts: [ReservationProductInput] __decorate([
} Field(type => Int),
__metadata("design:type", Number)
type Mutation { ], StoreInput.prototype, "number", void 0);
createStore(input: StoreInput): Store __decorate([
createReservation(input: ReservationInput): Reservation 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 };

82
src/typeDefs.ts Normal file
View File

@ -0,0 +1,82 @@
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
import "reflect-metadata";
import { ObjectType, Field, InputType, Int, Float } from "type-graphql";
@ObjectType()
export class Store {
@Field()
id: string;
@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;
}

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"lib": [
"es2018",
"esnext.asynciterable"
],
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true
}
}