From e9655f1055bd9b767b177e3a2e666906b3e72708 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 16 Mar 2020 06:31:29 +1100 Subject: [PATCH] Got it working! --- server/handler.js | 10 ++++- server/server.js | 8 ++-- server/simpleQL/index.js | 88 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 11 deletions(-) diff --git a/server/handler.js b/server/handler.js index 0468582..f44dc77 100644 --- a/server/handler.js +++ b/server/handler.js @@ -45,7 +45,15 @@ const handler = { //if this is a sub-query, use the parent to find the author if (parent && parent.typeName == 'Book') { - return authors.find(a => a.books.filter(b => b.title == parent.context.title).length > 0); + const author = authors.find(a => a.books.filter(b => b.title == parent.context.title).length > 0); + + //ensure only the named scalars are returned (hack) + const ret = {}; + if (scalars.filter(s => s.name == 'name').length > 0) { + ret.name = author.name; + } + + return [ret]; //must return an array } //return all authors diff --git a/server/server.js b/server/server.js index 22b5f46..0b5e5fe 100644 --- a/server/server.js +++ b/server/server.js @@ -12,12 +12,12 @@ const simpleQL = require('./simpleQL'); const simple = simpleQL(schema, handler); //open the end -app.post('/simpleql', (req, res) => { - const [code, result] = simple(req.body); - res.status(code).send(result); +app.post('/simpleql', async (req, res) => { + const [code, result] = await 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}`); + 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 ed7f68b..4bca894 100644 --- a/server/simpleQL/index.js +++ b/server/simpleQL/index.js @@ -5,7 +5,7 @@ const main = (schema, handler) => { console.log(typeGraph); //the receiving function - this will be called multiple times - return reqBody => { + return async reqBody => { //parse the query const tokens = reqBody.split(/(\s+)/).filter(s => s.trim().length > 0); let pos = 0; @@ -22,11 +22,10 @@ const main = (schema, handler) => { //no leading keyword - regular query default: - parseQuery(handler, tokens, pos, typeGraph); + const result = await parseQuery(handler, tokens, pos, typeGraph[tokens[pos]], typeGraph); - return [200, '']; + return [200, result]; - //TODO break; } } @@ -135,8 +134,73 @@ const parseCompoundType = (tokens, pos) => { return compound; }; -const parseQuery = (handler, tokens, pos, typeGraph) => { - //TODO +const parseQuery = async (handler, tokens, pos, typeGraph, superTypeGraph, parent = null) => { + //returns an object result from handler + + //get the "parent object" contents for sub-objects + const queryName = superTypeGraph[tokens[pos]] ? null : tokens[pos]; //if you're a type, name = null + const queryType = superTypeGraph[tokens[pos]] ? tokens[pos] : superTypeGraph[parent.typeName][tokens[pos]].typeName; //use this type or derive the type from the parent + + //move on + pos++; + + //the opening brace + if (tokens[pos++] != '{') { + throw 'Expected \'{\' in query, found ' + tokens[pos - 1]; + } + + //the scalars to pass to the handler + const scalarFields = []; + const deferredCalls = []; //functions (promises) that will be called at the end of this function + + while(tokens[pos] != '}') { //while not at the end of this block + //not the end of the query + if (!tokens[pos]) { + throw 'Expected field in query, got end'; + } + + //prevent using keywords + if (['create', 'update', 'delete', 'set', 'match'].includes(tokens[pos])) { + throw 'Unexpected keyword ' + tokens[pos]; + } + + //type is a scalar, and can be queried + if (superTypeGraph[typeGraph[tokens[pos]].typeName].scalar) { + //push the scalar object to the queryFields + scalarFields.push({ typeName: typeGraph[tokens[pos]].typeName, name: tokens[pos] }); + + pos++; + } else { + const pos2 = pos; //cache the value to keep it from changing + + //recurse + deferredCalls.push(async (result) => [tokens[pos2], await parseQuery( + handler, + tokens, + pos2, + superTypeGraph[typeGraph[tokens[pos2]].typeName], + superTypeGraph, + { typeName: queryType, scalars: scalarFields, context: result } + )]); + + pos = eatBlock(tokens, pos); + } + } + + //eat the end bracket + pos++; + + let results = handler[queryType](parent, scalarFields); + + results = await Promise.all(results.map(async res => { + const tuples = await Promise.all(deferredCalls.map(async call => await call(res))); + + tuples.forEach(tuple => res[tuple[0]] = tuple[1]); + + return res; + })); + + return results; }; //utils @@ -146,5 +210,17 @@ const checkAlphaNumeric = (str) => { } }; +const eatBlock = (tokens, pos) => { + while (tokens[pos] && tokens[pos] != '}') { + if (tokens[pos] == '{') { + pos = eatBlock(tokens, pos+1); + } else { + pos++; + } + } + + return pos; +}; + //return module.exports = main; \ No newline at end of file