Last active
March 3, 2023 12:59
-
-
Save brianmugweru/1cae2726c2b5e0e7cfd9f39dfe5dc210 to your computer and use it in GitHub Desktop.
gql todolist setup and resolves
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DB DATA SOURCE | |
/** START OF DB DATA SOURCE **/ | |
const { DataSource } = require("apollo-datasource"); | |
const sqlite = require("sqlite3"); | |
const createDatabase = () => { | |
return new Promise((resolve, reject) => { | |
const db = new sqlite.Database(process.env.DB_FILE_PATH, (err) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(db); | |
}); | |
}); | |
}; | |
class Db extends DataSource { | |
async initialize() { | |
this.db = await createDatabase(); | |
// do not change schema initialization it is here to provide an overview of data structures | |
await this.executeCommand(`CREATE TABLE IF NOT EXISTS orders ( | |
id INTEGER PRIMARY KEY, | |
deliveryAddress TEXT NOT NULL, | |
total REAL NOT NULL, | |
items TEXT NOT NULL, | |
discountCode TEXT, | |
comment TEXT, | |
status STRING NOT NULL | |
);`); | |
} | |
close() { | |
this.db.close(); | |
} | |
executeQuery(query) { | |
return new Promise((resolve, reject) => { | |
this.db.all(query, (err, rows) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(rows); | |
}); | |
}); | |
} | |
executeCommand(command) { | |
return new Promise((resolve, reject) => { | |
this.db.run(command, (err) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(); | |
}); | |
}); | |
} | |
getOrders(status=null) { | |
return new Promise((resolve, reject) => { | |
const query = status? `SELECT * FROM orders WHERE status='${status}'` : "SELECT * FROM orders" | |
this.db.all(query, (err, rows) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(rows); | |
}); | |
}); | |
} | |
getOrder(id) { | |
return new Promise((resolve, reject) => { | |
this.db.get(`SELECT * FROM orders WHERE id=${id}`, (err, rows) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(rows); | |
}); | |
}); | |
} | |
updateOrderStatus(id, status) { | |
return this.executeCommand( | |
`UPDATE orders SET status='${status}' WHERE id=${id}` | |
); | |
} | |
} | |
const db = new Db(); | |
module.exports = db; | |
/** END OF DB DATA SOURCE **/ | |
// QUERY SCHEMA | |
/** START OF QUERY SCHEMA **/ | |
const { gql } = require("apollo-server"); | |
// TODO write correct schema | |
const typeDefs = gql` | |
type Query { | |
order(id: Int): Order | |
orders(status: Status): [Order] | |
} | |
type Mutation { | |
updateStatus(id: ID!, status: Status!): Order | |
} | |
type Order { | |
id: ID! | |
deliveryAddress: String! | |
items: [String!] | |
total: Float! | |
discountCode: String! | |
comment: String | |
status: Status! | |
} | |
enum Status { | |
PENDING | |
PAID | |
IN_PROGRESS | |
IN_DELIVERY | |
DELIVERED | |
} | |
`; | |
module.exports = typeDefs; | |
/** END OF QUERY SCHEMA **/ | |
// RESOLVERS | |
/** START OF RESOLVERS **/ | |
const resolvers = { | |
Query: { | |
orders: async(_, {status}, {dataSources}) => { | |
const orders = await dataSources.db.getOrders(status) | |
return orders.map((value) => { | |
return { | |
...value, | |
items: value.items.replace(/ /g, '').split(',') | |
} | |
} ) | |
}, | |
order: async(_, {id}, {dataSources}) => { | |
const order = await dataSources.db.getOrder(id) | |
return { | |
...order, | |
items: order.items.replace(/ /g, '').split(',') | |
} | |
} | |
}, | |
Mutation: { | |
updateStatus: async(_, {id, status}, {dataSources}) => { | |
await dataSources.db.updateOrderStatus(id, status) | |
const order = await dataSources.db.getOrder(id) | |
return { | |
...order, | |
items: order.items.replace(/ /g, '').split(',') | |
} | |
} | |
} | |
}; | |
module.exports = resolvers; | |
/** END OF RESOLVERS **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment