diff --git a/src/data.js b/src/data.js index 5dd8c35..7c75b95 100644 --- a/src/data.js +++ b/src/data.js @@ -51,6 +51,10 @@ const products = [{ storeId: '5f2919aa-333a-4745-8166-3002ab30de0e', }]; +const reservations = []; + +let reservationProducts = []; + function createStore({ city, name, number, postalCode, street }) { const newStore = { city, @@ -74,11 +78,21 @@ function getStores() { function getStoreProducts(storeId) { return products.filter((p) => p.storeId === storeId) -} +} + +function getReservationProducts(reservationId) { + return []; +} + +function createReservation(reservation) { + return {}; +} module.exports = { createStore, + createReservation, getStore, getStores, - getStoreProducts + getStoreProducts, + getReservationProducts }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 66f7b5d..f34c9a4 100644 --- a/src/index.js +++ b/src/index.js @@ -11,21 +11,20 @@ const typeDefs = require('./typeDefs'); // 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 3 +// 🏪 Exercise 4 // -------------- -// Now we want to display the products for a store, we can select the quantity of each product, -// and make a reservation. +// Now we will focus on creating a reservation, which looks like this: +// { id, date, reservationProducts: { product: { id, name price }, quantity })). -// 1) Create a Query `store` to retrieve a store by id. Extend the data store to find the right store. -// 2) Create a list of products with a reference (storeId). -// 3) Extend the Store type with a list of products. A product has a price, name and description. -// `products` becomes a field of the store. Add a resolver function to get the products of a store. -// 4) Go to the GraphQL Playground and use a directive to query a store with or without products, -// name the variable `withProducts`. - -// 1) Check: https://graphql.org/learn/queries/#variables -// 4) products @include(if: $withProducts) { +// 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`), +// named `reservationProducts` (field of `ReservationInput`). +// 2) Now create a resolver function for the mutation and insert it into our in-memory database. +// 3) Create a query field reservationProducts under Reservation, to get the reservationProducts [{ product, quantity }]. +// Similar as stores under Query. +// 4) Go to the GraphQL Playground and try out the createReservation mutation! // In the most basic sense, the ApolloServer can be started // by passing type definitions (typeDefs) and the resolvers diff --git a/src/resolvers.js b/src/resolvers.js index 648c124..402b2c5 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -2,6 +2,8 @@ const { UserInputError } = require('apollo-server'); const yup = require('yup'); const data = require('./data'); +const uuidSchema = yup.string().min(36).max(36); + const createStoreSchema = yup.object() .shape({ city: yup.string().max(255), @@ -15,9 +17,19 @@ const createStoreSchema = yup.object() street: yup.string().max(255), }); -const getStoreSchema = yup.object() +const getStoreSchema = yup.object() .shape({ - id: yup.string().min(36).max(36), + id: uuidSchema, + }); + +const createReservationSchema = yup.object() + .shape({ + reservationProducts: yup.array( + yup.object().shape({ + productId: uuidSchema, + quantity: yup.number().required().positive().integer(), + }) + ), }); // Resolvers define the technique for fetching the types in the @@ -49,6 +61,6 @@ module.exports = { } const { city, name, number, postalCode, street } = input; return data.createStore({ city, name, number, postalCode, street }); - } + }, } }; \ No newline at end of file diff --git a/src/typeDefs.js b/src/typeDefs.js index 8dd3584..25b2345 100644 --- a/src/typeDefs.js +++ b/src/typeDefs.js @@ -32,7 +32,7 @@ module.exports = gql` input StoreInput { city: String - name: String + name: String! number: Int postalCode: String street: String