chore: getting started - first exercise
This commit is contained in:
parent
a0480fb679
commit
5bed3a9850
|
@ -1,6 +1,9 @@
|
||||||
# Getting started
|
# 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 i
|
||||||
npm run start
|
npm start
|
||||||
```
|
```
|
10
package.json
10
package.json
|
@ -1,16 +1,20 @@
|
||||||
{
|
{
|
||||||
"name": "food-graphql-server",
|
"name": "food-graphql-server",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "The GraphQL server to reserve food.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"start": "nodemon src/index.js"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"apollo-server": "^2.3.1",
|
"apollo-server": "^2.3.1",
|
||||||
"graphql": "^14.1.1"
|
"graphql": "^14.1.1",
|
||||||
|
"uuid": "^3.3.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^1.18.9"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
54
src/index.js
54
src/index.js
|
@ -1,37 +1,35 @@
|
||||||
const { ApolloServer, gql } = require('apollo-server');
|
const { ApolloServer, gql } = require('apollo-server');
|
||||||
|
|
||||||
const stores = [
|
// ⚽️ Goal
|
||||||
{
|
// --------
|
||||||
name: 'Den Olijfboom',
|
|
||||||
},
|
// We want to create a webapp to reserve products (food).
|
||||||
{
|
// Some ingredients we need: a store, products and reservations.
|
||||||
name: 'Pizza Talia'
|
// 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
|
// Type definitions define the "shape" of your data and specify
|
||||||
// which ways the data can be fetched from the GraphQL server.
|
// which ways the data can be fetched from the GraphQL server.
|
||||||
const typeDefs = gql`
|
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 {
|
type Query {
|
||||||
stores: [Store]
|
test: String
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Resolvers define the technique for fetching the types in the
|
// Resolvers define the technique for fetching the types in the
|
||||||
// schema. We'll retrieve books from the "books" array above.
|
// schema.
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
Query: {
|
|
||||||
stores: () => stores,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// In the most basic sense, the ApolloServer can be started
|
// In the most basic sense, the ApolloServer can be started
|
||||||
|
@ -39,8 +37,10 @@ const resolvers = {
|
||||||
// responsible for fetching the data for those types.
|
// responsible for fetching the data for those types.
|
||||||
const server = new ApolloServer({ typeDefs, resolvers });
|
const server = new ApolloServer({ typeDefs, resolvers });
|
||||||
|
|
||||||
// This `listen` method launches a web-server. Existing apps
|
// This `listen` method launches a web-server. Existing apps
|
||||||
// can utilize middleware options, which we'll discuss later.
|
// can utilize middleware options.
|
||||||
server.listen().then(({ url }) => {
|
server.listen()
|
||||||
console.log(`🚀 Food GraphQL Server ready at ${url}`);
|
.then(({ url }) => {
|
||||||
});
|
console.log(`🚀 Food GraphQL Server ready at ${url}.
|
||||||
|
⛄️ Go to this url to play with GraphQL in the GraphQL Playground.`);
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue