feat: start exercise 2

This commit is contained in:
Kristof Van Miegem 2019-02-04 19:08:19 +01:00
parent 5bed3a9850
commit c4b07ea259
6 changed files with 2355 additions and 24 deletions

View File

@ -12,7 +12,8 @@
"dependencies": {
"apollo-server": "^2.3.1",
"graphql": "^14.1.1",
"uuid": "^3.3.2"
"uuid": "^3.3.2",
"yup": "^0.26.10"
},
"devDependencies": {
"nodemon": "^1.18.9"

View File

@ -2,7 +2,7 @@ const uuid = require('uuid');
const stores = [
{
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79'
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
name: 'Den Olijfboom',
},
{
@ -11,12 +11,17 @@ const stores = [
}
];
export function createStore({ name }) {
function createStore({ name }) {
const newStore = { id: uuid(), name };
stores.push(newStore);
return newStore;
}
export function getStores() {
function getStores() {
return stores;
}
}
module.exports = {
createStore,
getStores
};

View File

@ -1,4 +1,6 @@
const { ApolloServer, gql } = require('apollo-server');
const resolvers = require('./resolvers');
const typeDefs = require('./typeDefs');
// ⚽️ Goal
// --------
@ -9,28 +11,24 @@ const { ApolloServer, gql } = require('apollo-server');
// 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
// 🏪 Exercise 2
// --------------
// 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/)
// Creating a mutation.
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
type Query {
test: String
}
`;
// Resolvers define the technique for fetching the types in the
// schema.
const resolvers = {
};
// 1) Create a type definition for the Mutation `createStore`, to create a store.
// Create an input type `StoreInput`, and use it as first argument of `createStore`.
// 2) Create a resolver function for the Mutation `createStore`.
// 3) Use `yup` in the resolver function to validate the input. (https://github.com/jquense/yup)
// 4) Create a service layer. Create a file `storeService.js` and put all business
// logic and data base logic in the service layer.
// This exercise is focussed on GraphQL but in real-life, middleware will
// validate if the user is authorized (e.g. check the Authorization header).
// Afterwards the resolver function will validate the input and call the service layer.
// 5) Try out the GraphQL mutation in the GraphQL Playground (🚀 http://localhost:4000/)
// 6) Query the stores, and check if the new store is in the list.
// 7) Extend the store with an address (street, number, postalCode, city) and create a fragment
// to query it.
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers

9
src/resolvers.js Normal file
View File

@ -0,0 +1,9 @@
const data = require('./data');
// Resolvers define the technique for fetching the types in the
// schema.
module.exports = {
Query: {
stores: () => data.getStores(),
},
};

18
src/typeDefs.js Normal file
View File

@ -0,0 +1,18 @@
const { gql } = require('apollo-server');
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
module.exports = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Store" type can be used in other type declarations.
type Store {
id: String
name: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
stores: [Store]
}
`;

2300
yarn.lock Normal file

File diff suppressed because it is too large Load Diff