feat: solution exercise 3
This commit is contained in:
parent
281ea4112a
commit
5c2e07e308
44
src/data.js
44
src/data.js
|
@ -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 }) {
|
function createStore({ city, name, number, postalCode, street }) {
|
||||||
const newStore = {
|
const newStore = {
|
||||||
city,
|
city,
|
||||||
|
@ -32,11 +64,21 @@ function createStore({ city, name, number, postalCode, street }) {
|
||||||
return newStore;
|
return newStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getStore(storeId) {
|
||||||
|
return stores.find((s) => s.id === storeId);
|
||||||
|
}
|
||||||
|
|
||||||
function getStores() {
|
function getStores() {
|
||||||
return stores;
|
return stores;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getStoreProducts(storeId) {
|
||||||
|
return products.filter((p) => p.storeId === storeId)
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createStore,
|
createStore,
|
||||||
getStores
|
getStore,
|
||||||
|
getStores,
|
||||||
|
getStoreProducts
|
||||||
};
|
};
|
|
@ -25,6 +25,7 @@ const typeDefs = require('./typeDefs');
|
||||||
// name the variable `withProducts`.
|
// name the variable `withProducts`.
|
||||||
|
|
||||||
// 1) Check: https://graphql.org/learn/queries/#variables
|
// 1) Check: https://graphql.org/learn/queries/#variables
|
||||||
|
// 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
|
||||||
|
|
|
@ -15,11 +15,30 @@ const createStoreSchema = yup.object()
|
||||||
street: yup.string().max(255),
|
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
|
// Resolvers define the technique for fetching the types in the
|
||||||
// schema.
|
// schema.
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Query: {
|
Query: {
|
||||||
stores: () => data.getStores(),
|
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: {
|
Mutation: {
|
||||||
createStore: async (parent, { input }) => {
|
createStore: async (parent, { input }) => {
|
||||||
|
|
|
@ -4,6 +4,14 @@ const { gql } = require('apollo-server');
|
||||||
// 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`
|
module.exports = gql`
|
||||||
# Comments in GraphQL are defined with the hash (#) symbol.
|
# 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.
|
# This "Store" type can be used in other type declarations.
|
||||||
type Store {
|
type Store {
|
||||||
city: String
|
city: String
|
||||||
|
@ -11,6 +19,7 @@ module.exports = gql`
|
||||||
name: String
|
name: String
|
||||||
number: Int
|
number: Int
|
||||||
postalCode: String
|
postalCode: String
|
||||||
|
products: [Product]
|
||||||
street: String
|
street: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +27,7 @@ module.exports = gql`
|
||||||
# (A "Mutation" type will be covered later on.)
|
# (A "Mutation" type will be covered later on.)
|
||||||
type Query {
|
type Query {
|
||||||
stores: [Store]
|
stores: [Store]
|
||||||
|
store(id: String): Store
|
||||||
}
|
}
|
||||||
|
|
||||||
input StoreInput {
|
input StoreInput {
|
||||||
|
|
Loading…
Reference in New Issue