From 8ef1d0efbdefedfdd4b3402a09f97f91b9c459ba Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Mar 2020 09:25:35 +1100 Subject: [PATCH] Layed out a fake database --- README.md | 4 ++-- server/database.js | 42 ++++++++++++++++++++++++++++++++++++++++ server/handler.js | 30 ++++++++++++++++++++++++++++ server/server.js | 16 +++++++-------- server/simpleQL/index.js | 2 +- 5 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 server/database.js create mode 100644 server/handler.js diff --git a/README.md b/README.md index 0f0094c..ff585ed 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ create Book [ set title "The Prisoner of Aunt Kazban" } { - set title "The Goblet of Fire Cocktail" + set title "The Goblet of the Fire Cocktail" } { set title "The Order for Kleenex" @@ -123,7 +123,7 @@ create Book [ set title "The Half-Priced Pharmacy" } { - set title "Yeah, I got nothing" + set title "Yeah, I Got Nothing" } ] ``` \ No newline at end of file diff --git a/server/database.js b/server/database.js new file mode 100644 index 0000000..59ff0ce --- /dev/null +++ b/server/database.js @@ -0,0 +1,42 @@ +//the authors +let authors = [ + { + name: 'J.K. Roaring', + books: [ + { title: 'The Philosepher\'s Kidney Stone' }, + { title: 'The Chamber Pot of Secrets' }, + { title: 'The Prisoner of Aunt Kazban' }, + { title: 'The Goblet of the Fire Cocktail' }, + { title: 'The Order for Kleenex' }, + { title: 'The Half-Priced Pharmacy' }, + { title: 'Yeah, I Got Nothing' }, + ] + }, + + { + name: 'Kenneth Grahame', + books: [ + { title: 'The Wind in the Willows', published: '1 April 1908' } + ] + }, +]; + +//insert the authors into the books +authors = authors.map(a => { + a.books = a.books.map(b => { + b.author = a; + return b; + }); + return a; +}); + +//get the book array +let books = []; + +authors.forEach(a => books = books.concat(a.books)); + +//the fake database +module.exports = { + authors, + books, +}; \ No newline at end of file diff --git a/server/handler.js b/server/handler.js new file mode 100644 index 0000000..7e767c7 --- /dev/null +++ b/server/handler.js @@ -0,0 +1,30 @@ +const database = require('./database.js'); + +console.log(database); + +//the handler routines +const handler = { + Book: scalars => { + //takes an array of scalar types as objects: { typeName: 'String', name: 'title' } + //must return an array of objects containing the results + }, + + Author: scalars => { + //takes an array of scalar types as objects: { typeName: 'String', name: 'name' } + //must return an array of objects containing the results + }, + + create: (sets, matches) => { + //TODO + }, + + update: (sets, matches) => { + //TODO + }, + + delete: matches => { + //TODO + }, +}; + +module.exports = handler; \ No newline at end of file diff --git a/server/server.js b/server/server.js index 9ad18fd..5a4a666 100644 --- a/server/server.js +++ b/server/server.js @@ -1,21 +1,21 @@ //express for testing const express = require('express'); -const path = require('path'); const app = express(); //test the library const schema = require('./schema.js'); -//const handler = requre('./handler.js'); +const handler = require('./handler.js'); const simpleQL = require('./simpleQL'); -const simple = simpleQL(schema); +const simple = simpleQL(schema, handler); //open the end -app.post('simpleQL', (req, res) => { - res.status(200).send(simple(req.body)); +app.post('/simpleql', (req, res) => { + const [code, result] = simple(req.body); + res.status(code).send(result); }); //startup -app.listen(process.env.WEB_PORT || 3100, (err) => { - console.log(`listening to *:${process.env.WEB_PORT || 3100}`); -}); +app.listen(process.env.WEB_PORT || 3100, err => { + console.log(`listening to *:${process.env.WEB_PORT || 3100}`); +}); \ No newline at end of file diff --git a/server/simpleQL/index.js b/server/simpleQL/index.js index 01d5688..e140a1d 100644 --- a/server/simpleQL/index.js +++ b/server/simpleQL/index.js @@ -6,7 +6,7 @@ const main = (schema, handler) => { //the receiving function - this will be called multiple times return reqBody => { - return ''; + return [200, '']; }; };