Skip to content

Commit 5006fd9

Browse files
committed
commenting resolver and schema files
1 parent 337bf02 commit 5006fd9

File tree

8 files changed

+24
-4
lines changed

8 files changed

+24
-4
lines changed

server/address/addressSchema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ const Address = `
6060
}
6161
`;
6262

63+
// return a thunk so hoisting can handle unordered schema imports in the schema.js file
64+
6365
// The address and base schemas are exported together because the address extends the base schema
6466
module.exports = () => [Address, Base];

server/cart/cartSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ const Cart = `
3434
}
3535
`;
3636

37+
// return a thunk so hoisting can handle unordered schema imports in the schema.js file
3738
module.exports = () => [Cart, Base];

server/order/orderResolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ module.exports = {
3737
// declare client outside of try block to enable calling release() in catch block
3838
let client;
3939
// wrapping all async in one try block
40-
// TODO: is there a better way to do this?
4140
try {
4241
// assign client to awaited returned value of invoking connect on psqlPool
4342
client = await context.psqlPool.connect();
@@ -55,7 +54,6 @@ module.exports = {
5554
// insert into orderProducts for each product
5655
return client.query(orderProductQuery, values)
5756
// still using then here because it allows for more specific error messaging and isn't too nested
58-
// TODO: is this even necessary?
5957
.then((orderProductData) => {
6058
console.log(orderProductData.rows);
6159
})
@@ -75,6 +73,8 @@ module.exports = {
7573
return null;
7674
},
7775
},
76+
77+
// type resolver for nested types: Customer and Products
7878
Order: {
7979
customer: (parent, args, context) => {
8080
const query = 'SELECT * FROM customers WHERE id = $1 LIMIT 1';

server/order/orderSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ const Order = `
3838
}
3939
`;
4040

41+
// return a thunk so hoisting can handle unordered schema imports in the schema.js file
4142
module.exports = () => [Order, Customer, Product, Base];

server/product/productResolver.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66

77
module.exports = {
88
Query: {
9+
// query for a single product from the database by productId
910
product: (parent, args, context) => {
11+
// create the query and values arguments that are needed for the psql query
1012
const query = 'SELECT * FROM products WHERE "productId"=$1 LIMIT 1';
1113
const values = [args.productId];
1214

15+
// query the database and return a promise (graphql handles promises by resolvnig them and
16+
// using the default resolver to match up key:value pairs as the final response)
1317
return context.psqlPool.query(query, values)
1418
.then((data) => data.rows[0])
1519
.catch((err) => console.log('ERROR LOOKING UP PRODUCT', err));
1620
},
21+
// query for all products in the database
1722
products: (parent, args, context) => {
1823
const query = 'SELECT * FROM products';
1924

@@ -23,6 +28,7 @@ module.exports = {
2328
},
2429
},
2530
Mutation: {
31+
// mutation to add a product to the database
2632
addProduct: (parent, args, context) => {
2733
const query = 'INSERT INTO products (name, description, price, weight) VALUES ($1, $2, $3, $4) RETURNING *';
2834
const values = [args.name, args.description, args.price, args.weight];
@@ -36,6 +42,7 @@ module.exports = {
3642
.catch((err) => console.log('ERROR INSERTING PRODUCT', err)))
3743
.catch((err) => console.log('ERROR CONNECTING WHILE ADDING PRODUCT', err));
3844
},
45+
// mutation to update a product
3946
updateProduct: (parent, args, context) => {
4047
let query = 'UPDATE products SET ';
4148
const values = [args.productId];
@@ -61,6 +68,7 @@ module.exports = {
6168
.catch((err) => console.log('ERROR UPDATING PRODUCT', err)))
6269
.catch((err) => console.log('ERROR CONNECTING WHILE UPDATING PRODUCT', err));
6370
},
71+
// mutation to delete a product
6472
deleteProduct: (parent, args, context) => {
6573
const query = 'DELETE FROM products WHERE "productId"=$1 RETURNING *';
6674
const values = [args.productId];

server/product/productSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ const Product = `
5050
}
5151
`;
5252

53+
// return a thunk so hoisting can handle unordered schema imports in the schema.js file
5354
module.exports = () => [Product, Base];

server/resolvers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const {
4646
Mutation: CartMutation,
4747
} = require('./cart/cartResolver');
4848

49-
// export all of those combined (Query and Mutation are standard & build into gql, Customer
50-
// is a type resolver)
49+
// export all of those combined (Query and Mutation are standard & built into GraphQL,
50+
// Customer, Order and Warehouse are type resolvers)
5151
module.exports = {
5252
Query: {
5353
...CustomerQuery,

server/warehouse/warehouseResolver.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
module.exports = {
88
Query: {
9+
// query to look up data on an individual warehouse
910
warehouse: (parent, args, context) => {
1011
const query = 'SELECT * FROM warehouses WHERE "warehouseId" = $1 LIMIT 1';
1112
const values = [args.warehouseId];
1213
return context.psqlPool.query(query, values)
1314
.then((data) => data.rows[0])
1415
.catch((err) => console.log('ERROR LOOKING UP WAREHOUSE', err));
1516
},
17+
// query to return data on all warehouses
1618
warehouses: (parent, args, context) => {
1719
const query = 'SELECT * FROM warehouses';
1820
return context.psqlPool.query(query)
@@ -21,6 +23,7 @@ module.exports = {
2123
},
2224
},
2325
Mutation: {
26+
// mutation to add a warehouse to the database
2427
addWarehouse: (parent, args, context) => {
2528
const query = 'INSERT INTO warehouses (name, "addressId") VALUES ($1, $2) RETURNING *';
2629
const values = [args.name, args.addressId];
@@ -34,6 +37,7 @@ module.exports = {
3437
.catch((err) => console.log('ERROR INSERTING WAREHOUSE', err)))
3538
.catch((err) => console.log('ERROR CONNECTING WHILE ADDING WAREHOUSE', err));
3639
},
40+
// mutation to update details of a warehouse
3741
updateWarehouse: (parent, args, context) => {
3842
let query = 'UPDATE warehouses SET ';
3943
const values = [args.warehouseId];
@@ -59,6 +63,7 @@ module.exports = {
5963
.catch((err) => console.log('ERROR UPDATING WAREHOUSE', err)))
6064
.catch((err) => console.log('ERROR CONNECTING WHILE UPDATING WAREHOUSE', err));
6165
},
66+
// mutation to delete a warehouse from the database
6267
deleteWarehouse: (parent, args, context) => {
6368
const query = 'DELETE FROM warehouses WHERE "warehouseId"=$1 RETURNING *';
6469
const values = [args.warehouseId];
@@ -73,6 +78,8 @@ module.exports = {
7378
.catch((err) => console.log('ERROR CONNECTING WHILE DELETING WAREHOUSE', err));
7479
},
7580
},
81+
82+
// type resolver for nested type: Address
7683
Warehouse: {
7784
address: (parent, args, context) => {
7885
const query = 'SELECT * FROM addresses WHERE id=$1 LIMIT 1';

0 commit comments

Comments
 (0)