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',
}];
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
};

View File

@ -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

View File

@ -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 });
}
},
}
};

View File

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