food-graphql-server/src/index.js

41 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-01-26 13:06:08 +00:00
const { ApolloServer, gql } = require('apollo-server');
2019-02-04 18:08:19 +00:00
const resolvers = require('./resolvers');
const typeDefs = require('./typeDefs');
2019-01-26 13:06:08 +00:00
// ⚽️ 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)
2019-02-07 14:55:48 +00:00
// 🏪 Exercise 4
// --------------
2019-02-07 14:55:48 +00:00
// Now we will focus on creating a reservation, which looks like this:
// { id, date, reservationProducts: { product: { id, name price }, quantity })).
// 1) Create a Mutation to create a reservation `createReservation`
// that takes an object as input. The input type is named `ReservationInput`.
// ReservationInput contains a list of { productId, quantity } (input type is named `ReservationProductInput`),
// named `reservationProducts` (field of `ReservationInput`).
// 2) Now create a resolver function for the mutation and insert it into our in-memory database.
// 3) Create a query field reservationProducts under Reservation, to get the reservationProducts [{ product, quantity }].
// Similar as stores under Query.
// 4) Go to the GraphQL Playground and try out the createReservation mutation!
2019-01-26 13:06:08 +00:00
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the 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.
server.listen()
.then(({ url }) => {
console.log(`🚀 Food GraphQL Server ready at ${url}.
Go to this url to play with GraphQL in the GraphQL Playground.`);
});