From 8dff5bcd8c59ea540012e9877923ab5fad43c62e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 31 Mar 2021 01:32:43 +1100 Subject: [PATCH] Added full support for Boolean and Float types --- source/parse-query-tree.js | 12 +++++------ test/index.js | 44 +++++++++++--------------------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/source/parse-query-tree.js b/source/parse-query-tree.js index 674292c..00f055f 100644 --- a/source/parse-query-tree.js +++ b/source/parse-query-tree.js @@ -7,6 +7,11 @@ const parseQueryTree = (tokens, typeGraph, options) => { throw `Expected a type in the type graph (found ${tokens[current - 1]})`; } + //check that there are the correct number of '{' and '}' + if (tokens.reduce((running, tok) => tok == '{' ? running + 1 : tok == '}' ? running - 1 : running, 0) != 0) { + throw `Unequal number of '{' and '}' found`; + } + //read the block of lines const [block, pos] = readBlock(tokens, current, tokens[current - 1], typeGraph, options); @@ -27,12 +32,7 @@ const readBlock = (tokens, current, superType, typeGraph, options) => { const result = {}; //scan each "line" in this block - while(tokens[current++]) { - //check for end of block - if (tokens[current - 1] == '}') { - break; - } - + while(tokens[current++] && tokens[current - 1] != '}') { //check for block-level keywords (modifiers need to form a chain from the leaf) let modifier = null; if (['match'].includes(tokens[current - 1])) { diff --git a/test/index.js b/test/index.js index 5f2521e..db356a8 100644 --- a/test/index.js +++ b/test/index.js @@ -6,8 +6,8 @@ const Op = { const books = { findAll: async args => { let arr = [ - { id: 1, title: 'The Wind in the Willows', published: '1908-06-15' }, - { id: 2, title: 'The Fart in the Fronds', published: '1908-06-15' } + { id: 1, title: 'The Wind in the Willows', published: '1908-06-15', score: 0.9 }, + { id: 2, title: 'The Fart in the Fronds', published: null, score: 0.45 } ]; const { attributes, where } = args; @@ -18,19 +18,9 @@ const books = { return true; } - if (where.title && where.published) { - return element.title == where.title.eq && element.published == where.published.eq; - } - - if (where.title) { - return element.title == where.title.eq; - } - - if (where.published) { - return element.published == where.published.eq; - } - - return false; + return Object.keys(where).reduce((result, key) => { + return result && (element[key] || 'null').toString() == where[key].eq.toString(); + }, true); }); //filter out non-used attributes @@ -52,9 +42,9 @@ const books = { const authors = { findAll: async args => { let arr = [ - { id: 1, name: 'Kenneth Grahame', books: [1] }, - { id: 2, name: 'Frank', books: [1, 2] }, - { id: 3, name: 'Betty', books: [2] } + { id: 1, name: 'Kenneth Grahame', books: [1], alive: false }, + { id: 2, name: 'Frank', books: [1, 2], alive: true }, + { id: 3, name: 'Betty', books: [2], alive: true } ]; const { attributes, where } = args; @@ -65,19 +55,9 @@ const authors = { return true; } - if (where.name && where.books) { - return element.name == where.name.eq; //books is always true because they are never queried - } - - if (where.name) { - return element.name == where.name.eq; - } - - if (where.books) { - return true; //books is always true because they are never queried - } - - return false; + return Object.keys(where).reduce((result, key) => { + return result && (element[key] || 'null').toString() == where[key].eq.toString(); + }, true); }); //filter out non-used attributes @@ -215,11 +195,13 @@ scalar Date type Book { String title Date published + Float score } type Author { String name Book books + Boolean alive } `;