feat: start exercise 2
This commit is contained in:
parent
5bed3a9850
commit
c4b07ea259
|
@ -12,7 +12,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"apollo-server": "^2.3.1",
|
"apollo-server": "^2.3.1",
|
||||||
"graphql": "^14.1.1",
|
"graphql": "^14.1.1",
|
||||||
"uuid": "^3.3.2"
|
"uuid": "^3.3.2",
|
||||||
|
"yup": "^0.26.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^1.18.9"
|
"nodemon": "^1.18.9"
|
||||||
|
|
11
src/data.js
11
src/data.js
|
@ -2,7 +2,7 @@ const uuid = require('uuid');
|
||||||
|
|
||||||
const stores = [
|
const stores = [
|
||||||
{
|
{
|
||||||
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79'
|
id: 'cc406ed9-fc02-4185-b073-8c12b61b5c79',
|
||||||
name: 'Den Olijfboom',
|
name: 'Den Olijfboom',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -11,12 +11,17 @@ const stores = [
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
export function createStore({ name }) {
|
function createStore({ name }) {
|
||||||
const newStore = { id: uuid(), name };
|
const newStore = { id: uuid(), name };
|
||||||
stores.push(newStore);
|
stores.push(newStore);
|
||||||
return newStore;
|
return newStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStores() {
|
function getStores() {
|
||||||
return stores;
|
return stores;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createStore,
|
||||||
|
getStores
|
||||||
|
};
|
36
src/index.js
36
src/index.js
|
@ -1,4 +1,6 @@
|
||||||
const { ApolloServer, gql } = require('apollo-server');
|
const { ApolloServer, gql } = require('apollo-server');
|
||||||
|
const resolvers = require('./resolvers');
|
||||||
|
const typeDefs = require('./typeDefs');
|
||||||
|
|
||||||
// ⚽️ Goal
|
// ⚽️ Goal
|
||||||
// --------
|
// --------
|
||||||
|
@ -9,28 +11,24 @@ const { ApolloServer, gql } = require('apollo-server');
|
||||||
// a store, we get a list of products (query) of that store.
|
// 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)
|
// 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`,
|
// Creating a mutation.
|
||||||
// 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
|
// 1) Create a type definition for the Mutation `createStore`, to create a store.
|
||||||
// which ways the data can be fetched from the GraphQL server.
|
// Create an input type `StoreInput`, and use it as first argument of `createStore`.
|
||||||
const typeDefs = gql`
|
// 2) Create a resolver function for the Mutation `createStore`.
|
||||||
type Query {
|
// 3) Use `yup` in the resolver function to validate the input. (https://github.com/jquense/yup)
|
||||||
test: String
|
// 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).
|
||||||
// Resolvers define the technique for fetching the types in the
|
// Afterwards the resolver function will validate the input and call the service layer.
|
||||||
// schema.
|
// 5) Try out the GraphQL mutation in the GraphQL Playground (🚀 http://localhost:4000/)
|
||||||
const resolvers = {
|
// 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
|
// In the most basic sense, the ApolloServer can be started
|
||||||
// by passing type definitions (typeDefs) and the resolvers
|
// by passing type definitions (typeDefs) and the resolvers
|
||||||
|
|
|
@ -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(),
|
||||||
|
},
|
||||||
|
};
|
|
@ -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]
|
||||||
|
}
|
||||||
|
`;
|
Loading…
Reference in New Issue