From 5bed3a98507774ab9a22eb20dcbe52b199c7e221 Mon Sep 17 00:00:00 2001 From: Kristof Van Miegem Date: Sun, 3 Feb 2019 12:09:03 +0100 Subject: [PATCH] chore: getting started - first exercise --- README.md | 5 ++++- package.json | 10 +++++++--- src/data.js | 22 +++++++++++++++++++++ src/index.js | 54 ++++++++++++++++++++++++++-------------------------- 4 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 src/data.js diff --git a/README.md b/README.md index 8c51e79..4d6234e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # Getting started +To get up-and-running, install the node dependencies and start the server. +🚀 Go to http://localhost:4000/ to play in the GraphQL Playground. + ``` npm i -npm run start +npm start ``` \ No newline at end of file diff --git a/package.json b/package.json index 75ccb2e..1ebadab 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,20 @@ { "name": "food-graphql-server", "version": "1.0.0", - "description": "", + "description": "The GraphQL server to reserve food.", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "nodemon src/index.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "apollo-server": "^2.3.1", - "graphql": "^14.1.1" + "graphql": "^14.1.1", + "uuid": "^3.3.2" + }, + "devDependencies": { + "nodemon": "^1.18.9" } } diff --git a/src/data.js b/src/data.js new file mode 100644 index 0000000..a7eca1e --- /dev/null +++ b/src/data.js @@ -0,0 +1,22 @@ +const uuid = require('uuid'); + +const stores = [ + { + id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79' + name: 'Den Olijfboom', + }, + { + id: '5f2919aa-333a-4745-8166-3002ab30de0e', + name: 'Pizza Talia' + } +]; + +export function createStore({ name }) { + const newStore = { id: uuid(), name }; + stores.push(newStore); + return newStore; +} + +export function getStores() { + return stores; +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 479da44..6f5302b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,35 @@ const { ApolloServer, gql } = require('apollo-server'); -const stores = [ - { - name: 'Den Olijfboom', - }, - { - name: 'Pizza Talia' - } -]; +// ⚽️ Goal +// -------- + +// We want to create a webapp to reserve products (food). +// Some ingredients we need: a store, products and reservations. +// First of all we want a list of stores. After we have chosen +// 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 1 +// -------------- + +// 1) First we create two files. One for our type definitions, `typeDefs.js`, +// and one for our resolver functions, `resolvers.js`. +// 2) Create a GraphQL type definition for our store. A store has an id and a name. +// 3) Create a Query `stores` to get a list of stores. +// 4) Create a resolver function that returns the list of stores. +// 5) Try out the GraphQL query in the GraphQL Playground (🚀 http://localhost:4000/) // Type definitions define the "shape" of your data and specify // which ways the data can be fetched from the GraphQL server. const typeDefs = gql` - # Comments in GraphQL are defined with the hash (#) symbol. - - # This "Store" type can be used in other type declarations. - type Store { - name: String - } - - # The "Query" type is the root of all GraphQL queries. - # (A "Mutation" type will be covered later on.) type Query { - stores: [Store] + test: String } `; // Resolvers define the technique for fetching the types in the -// schema. We'll retrieve books from the "books" array above. +// schema. const resolvers = { - Query: { - stores: () => stores, - }, }; // In the most basic sense, the ApolloServer can be started @@ -39,8 +37,10 @@ const resolvers = { // responsible for fetching the data for those types. const server = new ApolloServer({ typeDefs, resolvers }); -// This `listen` method launches a web-server. Existing apps -// can utilize middleware options, which we'll discuss later. -server.listen().then(({ url }) => { - console.log(`🚀 Food GraphQL Server ready at ${url}`); -}); +// This `listen` method launches a web-server. Existing apps +// can utilize middleware options. +server.listen() + .then(({ url }) => { + console.log(`🚀 Food GraphQL Server ready at ${url}. +⛄️ Go to this url to play with GraphQL in the GraphQL Playground.`); + });