chore: getting started - first exercise

This commit is contained in:
Kristof Van Miegem 2019-02-03 12:09:03 +01:00
parent a0480fb679
commit 5bed3a9850
4 changed files with 60 additions and 31 deletions

View File

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

View File

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

22
src/data.js Normal file
View File

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

View File

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