It seems to be working in a mock-environment

This commit is contained in:
2021-03-30 14:38:45 +11:00
parent f4dd51ac31
commit 0f653571d6
3 changed files with 160 additions and 27 deletions

View File

@@ -1,8 +1,9 @@
const buildTypeGraph = require('./build-type-graph');
const parseInput = require('./parse-input');
const parseQueryTree = require('./parse-query-tree');
//the main function to be returned (sineQL())
const sineQL = (schema, handler, options = {}) => {
const sineQL = (schema, { queryHandlers }, options = {}) => {
let typeGraph;
try {
@@ -18,21 +19,27 @@ const sineQL = (schema, handler, options = {}) => {
try {
//parse the query
const tokens = parseInput(reqBody, true, options);
let pos = 0;
const queryTree = parseQueryTree(tokens, typeGraph, options);
switch(tokens[pos]) {
//check for keywords
switch(tokens[0]) {
//check for leading keywords
case 'create':
case 'update':
case 'delete':
return [501, 'Keyword not implemented: ' + tokens[pos]];
return [501, 'Keyword not implemented: ' + tokens[0]];
//TODO: implement these keywords
break;
//no leading keyword - regular query
default:
//TODO: implement queries
return [501, 'Queries not implemented'];
default: {
const result = await queryHandlers[queryTree.typeName](queryTree, typeGraph);
if (options.debug) {
console.log('Query tree results:\n', result);
}
return [200, result];
}
}
}
catch(e) {

View File

@@ -0,0 +1,98 @@
//build the tokens into a single object of types representing the initial query
const parseQueryTree = (tokens, typeGraph, options) => {
let current = 1; //primed
//TODO: check for top-level keywords
//get a token that matches a type
if (!typeGraph[tokens[current - 1]]) {
throw `Expected a type in the type graph (found ${tokens[current - 1]})`;
}
//read the block of lines
const [block, pos] = readBlock(tokens, current, tokens[current - 1], typeGraph, options);
//insert the typename into the top-level block
block['typeName'] = tokens[current - 1];
//finally
return block;
};
const readBlock = (tokens, current, superType, typeGraph, options) => {
//scan the '{'
if (tokens[current++] != '{') {
throw `Expected '{'`;
}
//result
const result = {};
//scan each "line" in this block
while(tokens[current++]) {
//check for end of block
if (tokens[current - 1] == '}') {
break;
}
//check for block-level keywords
let modifier = null;
if (['match'].includes(tokens[current - 1])) {
modifier = tokens[current - 1];
current++;
}
//read field name
const fieldName = tokens[current - 1];
if (options.debug) {
console.log(`Trying to process field ${fieldName}`);
}
//if the field is non-scalar, read the sub-block
if (!typeGraph[typeGraph[superType][fieldName].typeName].scalar) {
//recurse
const [block, pos] = readBlock(tokens, current, typeGraph[superType][fieldName].typeName, typeGraph, options);
//insert the typename into the block
block['typeName'] = typeGraph[superType][fieldName].typeName;
//insert into result
result[fieldName] = block;
//insert the block-level modifier signal
if (modifier) {
result[fieldName][modifier] = true;
}
current = pos; //pos points past the end of the block
if (options.debug) {
console.log(`${fieldName}: ${JSON.stringify(result[fieldName])}`);
}
continue;
}
//scalar
else {
//save the typeGraph type into result
result[fieldName] = JSON.parse(JSON.stringify( typeGraph[ typeGraph[superType][fieldName].typeName ] ));
//insert the block-level modifier signal
if (modifier) {
result[fieldName][modifier] = tokens[current++];
}
if (options.debug) {
console.log(`${fieldName}: `, result[fieldName]);
}
continue;
}
}
return [result, current];
};
module.exports = parseQueryTree;