feat: solution exercise 3

This commit is contained in:
Kristof Van Miegem 2019-02-07 14:25:20 +01:00
parent 281ea4112a
commit 5c2e07e308
4 changed files with 73 additions and 1 deletions

View File

@ -19,6 +19,38 @@ const stores = [
}
];
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',
}];
function createStore({ city, name, number, postalCode, street }) {
const newStore = {
city,
@ -32,11 +64,21 @@ function createStore({ city, name, number, postalCode, street }) {
return newStore;
}
function getStore(storeId) {
return stores.find((s) => s.id === storeId);
}
function getStores() {
return stores;
}
function getStoreProducts(storeId) {
return products.filter((p) => p.storeId === storeId)
}
module.exports = {
createStore,
getStores
getStore,
getStores,
getStoreProducts
};

View File

@ -25,6 +25,7 @@ const typeDefs = require('./typeDefs');
// name the variable `withProducts`.
// 1) Check: https://graphql.org/learn/queries/#variables
// 4) products @include(if: $withProducts) {
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers

View File

@ -15,11 +15,30 @@ const createStoreSchema = yup.object()
street: yup.string().max(255),
});
const getStoreSchema = yup.object()
.shape({
id: yup.string().min(36).max(36),
});
// Resolvers define the technique for fetching the types in the
// schema.
module.exports = {
Query: {
stores: () => data.getStores(),
store: async (parent, input) => {
try {
await getStoreSchema.validate(input);
} catch (e) {
throw new UserInputError('Invalid input.', { validationErrors: e.errors });
}
const { id } = input;
return data.getStore(id);
},
},
Store: {
products: async ({ id: storeId }) => {
return data.getStoreProducts(storeId);
}
},
Mutation: {
createStore: async (parent, { input }) => {

View File

@ -4,6 +4,14 @@ const { gql } = require('apollo-server');
// which ways the data can be fetched from the GraphQL server.
module.exports = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
type Product {
description: String
id: String
name: String
price: Float
}
# This "Store" type can be used in other type declarations.
type Store {
city: String
@ -11,6 +19,7 @@ module.exports = gql`
name: String
number: Int
postalCode: String
products: [Product]
street: String
}
@ -18,6 +27,7 @@ module.exports = gql`
# (A "Mutation" type will be covered later on.)
type Query {
stores: [Store]
store(id: String): Store
}
input StoreInput {