feat: start exercise 4

This commit is contained in:
Kristof Van Miegem 2019-02-07 15:55:48 +01:00
parent 5c2e07e308
commit f5070142cb
4 changed files with 43 additions and 18 deletions

View File

@ -51,6 +51,10 @@ const products = [{
storeId: '5f2919aa-333a-4745-8166-3002ab30de0e', storeId: '5f2919aa-333a-4745-8166-3002ab30de0e',
}]; }];
const reservations = [];
let reservationProducts = [];
function createStore({ city, name, number, postalCode, street }) { function createStore({ city, name, number, postalCode, street }) {
const newStore = { const newStore = {
city, city,
@ -74,11 +78,21 @@ function getStores() {
function getStoreProducts(storeId) { function getStoreProducts(storeId) {
return products.filter((p) => p.storeId === storeId) return products.filter((p) => p.storeId === storeId)
} }
function getReservationProducts(reservationId) {
return [];
}
function createReservation(reservation) {
return {};
}
module.exports = { module.exports = {
createStore, createStore,
createReservation,
getStore, getStore,
getStores, getStores,
getStoreProducts getStoreProducts,
getReservationProducts
}; };

View File

@ -11,21 +11,20 @@ const typeDefs = require('./typeDefs');
// a store, we get a list of products (query) of that store. // 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) // 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, // Now we will focus on creating a reservation, which looks like this:
// and make a reservation. // { 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. // 1) Create a Mutation to create a reservation `createReservation`
// 2) Create a list of products with a reference (storeId). // that takes an object as input. The input type is named `ReservationInput`.
// 3) Extend the Store type with a list of products. A product has a price, name and description. // ReservationInput contains a list of { productId, quantity } (input type is named `ReservationProductInput`),
// `products` becomes a field of the store. Add a resolver function to get the products of a store. // named `reservationProducts` (field of `ReservationInput`).
// 4) Go to the GraphQL Playground and use a directive to query a store with or without products, // 2) Now create a resolver function for the mutation and insert it into our in-memory database.
// name the variable `withProducts`. // 3) Create a query field reservationProducts under Reservation, to get the reservationProducts [{ product, quantity }].
// Similar as stores under Query.
// 1) Check: https://graphql.org/learn/queries/#variables // 4) Go to the GraphQL Playground and try out the createReservation mutation!
// 4) products @include(if: $withProducts) {
// 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

View File

@ -2,6 +2,8 @@ const { UserInputError } = require('apollo-server');
const yup = require('yup'); const yup = require('yup');
const data = require('./data'); const data = require('./data');
const uuidSchema = yup.string().min(36).max(36);
const createStoreSchema = yup.object() const createStoreSchema = yup.object()
.shape({ .shape({
city: yup.string().max(255), city: yup.string().max(255),
@ -15,9 +17,19 @@ const createStoreSchema = yup.object()
street: yup.string().max(255), street: yup.string().max(255),
}); });
const getStoreSchema = yup.object() const getStoreSchema = yup.object()
.shape({ .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 // Resolvers define the technique for fetching the types in the
@ -49,6 +61,6 @@ module.exports = {
} }
const { city, name, number, postalCode, street } = input; const { city, name, number, postalCode, street } = input;
return data.createStore({ city, name, number, postalCode, street }); return data.createStore({ city, name, number, postalCode, street });
} },
} }
}; };

View File

@ -32,7 +32,7 @@ module.exports = gql`
input StoreInput { input StoreInput {
city: String city: String
name: String name: String!
number: Int number: Int
postalCode: String postalCode: String
street: String street: String