From 5c2e07e3085196016b0b9a6d4efa98d5972562fa Mon Sep 17 00:00:00 2001 From: Kristof Van Miegem Date: Thu, 7 Feb 2019 14:25:20 +0100 Subject: [PATCH] feat: solution exercise 3 --- src/data.js | 44 +++++++++++++++++++++++++++++++++++++++++++- src/index.js | 1 + src/resolvers.js | 19 +++++++++++++++++++ src/typeDefs.js | 10 ++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/data.js b/src/data.js index 21f7acd..5dd8c35 100644 --- a/src/data.js +++ b/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 }) { 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 }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index a02f211..66f7b5d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/src/resolvers.js b/src/resolvers.js index 620a14b..648c124 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -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 }) => { diff --git a/src/typeDefs.js b/src/typeDefs.js index ba05cbc..8dd3584 100644 --- a/src/typeDefs.js +++ b/src/typeDefs.js @@ -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 {