mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
Stripped this project to it's bones
This commit is contained in:
@@ -21,7 +21,8 @@ const buildTypeGraph = (schema, options) => {
|
||||
//check for keywords
|
||||
switch(tokens[pos - 1]) {
|
||||
case 'type':
|
||||
graph[tokens[pos++]] = parseCompoundType(tokens, pos, options);
|
||||
//delegate
|
||||
graph[tokens[pos++]] = parseCompoundType(tokens, pos, Object.keys(graph), options);
|
||||
|
||||
//advance to the end of the compound type
|
||||
pos = eatBlock(tokens, pos);
|
||||
@@ -29,7 +30,9 @@ const buildTypeGraph = (schema, options) => {
|
||||
break;
|
||||
|
||||
case 'scalar':
|
||||
//check against keyword list
|
||||
if (keywords.includes(graph[tokens[pos - 1]])) {
|
||||
//TODO: test this error
|
||||
throw 'Unexpected keyword ' + graph[tokens[pos - 1]];
|
||||
}
|
||||
|
||||
@@ -49,7 +52,7 @@ const buildTypeGraph = (schema, options) => {
|
||||
};
|
||||
|
||||
//moved this routine to a separate function for clarity
|
||||
const parseCompoundType = (tokens, pos, options) => {
|
||||
const parseCompoundType = (tokens, pos, scalars, options) => {
|
||||
//format check (not strictly necessary, but it looks nice)
|
||||
if (tokens[pos] !== '{') {
|
||||
throw 'Expected \'{\' in compound type definition';
|
||||
@@ -69,12 +72,17 @@ const parseCompoundType = (tokens, pos, options) => {
|
||||
|
||||
//can't use keywords
|
||||
if (keywords.includes(type) || keywords.includes(name)) {
|
||||
throw 'Unexpected keyword found as type field or type name (' + type + ' ' + name + ')';
|
||||
throw `Unexpected keyword found as type field or type name (${type} ${name})`;
|
||||
}
|
||||
|
||||
//can only use existing types (prevents looping tree structure)
|
||||
if (!scalars.includes(type)) { //TODO: test this error
|
||||
throw `Unexpected value found as type field ('${type}' is undefined)`;
|
||||
}
|
||||
|
||||
//check for duplicate fields
|
||||
if (Object.keys(compound).includes(name)) {
|
||||
throw 'Unexpected duplicate field name';
|
||||
throw `Unexpected duplicate field name (${name})`;
|
||||
}
|
||||
|
||||
//finally, push to the compound definition
|
||||
|
||||
@@ -10,12 +10,12 @@ const sineQL = (schema, handler, options = {}) => {
|
||||
typeGraph = buildTypeGraph(schema, options);
|
||||
}
|
||||
catch(e) {
|
||||
console.log('Type Graph Error:', e);
|
||||
console.error('Type Graph Error:', e);
|
||||
return null;
|
||||
}
|
||||
|
||||
//the receiving function (sine()) - this will be called multiple times
|
||||
return async (reqBody) => {
|
||||
return async reqBody => {
|
||||
try {
|
||||
//parse the query
|
||||
const tokens = parseInput(reqBody, true, options);
|
||||
@@ -32,18 +32,12 @@ const sineQL = (schema, handler, options = {}) => {
|
||||
|
||||
//no leading keyword - regular query
|
||||
default:
|
||||
const [result, endPos] = await parseQuery(handler, tokens, pos, typeGraph);
|
||||
|
||||
//reject the request, despite finishing processing it
|
||||
if (tokens[endPos]) {
|
||||
throw 'Unexpected data found at the end of the token list (found ' + tokens[endPos] + ')';
|
||||
}
|
||||
|
||||
return [200, result];
|
||||
//TODO: implement queries
|
||||
return [501, 'Queries not implemented'];
|
||||
}
|
||||
}
|
||||
catch(e) {
|
||||
console.log('Error:', e);
|
||||
console.error('Error:', e);
|
||||
return [400, e.stack || e];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
const keywords = require('./keywords.json');
|
||||
const { eatBlock } = require('./utils');
|
||||
|
||||
//returns an object result from handler for all custom types
|
||||
const parseQuery = async (handler, tokens, pos, typeGraph, parent = null, superMatching = false) => {
|
||||
//only read past tokens
|
||||
pos++;
|
||||
|
||||
//determine this query's supertype
|
||||
let superType;
|
||||
|
||||
if (!parent) { //top-level
|
||||
superType = tokens[pos - 1];
|
||||
}
|
||||
|
||||
else if (typeGraph[parent.typeName][ tokens[pos-1] ]) {
|
||||
superType = typeGraph[parent.typeName][ tokens[pos-1] ].typeName;
|
||||
}
|
||||
|
||||
else {
|
||||
throw `Missing supertype in type graph (pos = ${pos})`;
|
||||
}
|
||||
|
||||
//error handling
|
||||
if (!handler[superType]) {
|
||||
throw 'Unrecognized type ' + superType;
|
||||
}
|
||||
|
||||
if (tokens[pos++] != '{') {
|
||||
throw 'Expected \'{\' after supertype';
|
||||
}
|
||||
|
||||
//the scalars to pass to the handler - components of the compound types
|
||||
const scalarFields = [];
|
||||
const deferredCalls = []; //functions (promises) that will be called at the end of this function
|
||||
|
||||
while(tokens[pos++] && tokens[pos - 1] !== '}') { //while not at the end of this block
|
||||
//check for matching flag
|
||||
let matching = false;
|
||||
|
||||
if (tokens[pos - 1] === 'match') {
|
||||
matching = true;
|
||||
pos++;
|
||||
}
|
||||
|
||||
//prevent using keywords
|
||||
if (keywords.includes(tokens[pos - 1])) {
|
||||
throw 'Unexpected keyword ' + tokens[pos - 1];
|
||||
}
|
||||
|
||||
//type is a scalar
|
||||
if (typeGraph[superType] && typeGraph[superType][tokens[pos - 1]] && typeGraph[typeGraph[superType][tokens[pos - 1]].typeName].scalar) {
|
||||
//push the scalar object to the queryFields
|
||||
scalarFields.push({ typeName: typeGraph[superType][tokens[pos - 1]].typeName, name: tokens[pos - 1], filter: matching ? tokens[pos++] : null });
|
||||
|
||||
//if I am a scalar child of a match and I do not match
|
||||
if (parent && superMatching && !matching) {
|
||||
throw 'Broken match chain in scalar type ' + tokens[pos - 1];
|
||||
}
|
||||
}
|
||||
|
||||
//type is a compound, and must be recursed
|
||||
else if (typeGraph[superType] && typeGraph[superType][tokens[pos - 1]]) {
|
||||
const pos2 = pos; //cache the value to keep it from changing
|
||||
|
||||
//recurse
|
||||
deferredCalls.push(async (result) => {
|
||||
//if I am a compound child of a match amd I do not match
|
||||
if (parent && superMatching && !matching) {
|
||||
throw 'Broken match chain in compound type ' + tokens[pos2 - 1];
|
||||
}
|
||||
|
||||
const [queryResult, dummyPos] = await parseQuery(
|
||||
handler,
|
||||
tokens,
|
||||
pos2 - 1,
|
||||
typeGraph,
|
||||
{ typeName: superType, scalars: scalarFields, context: result }, //parent object (this one)
|
||||
matching
|
||||
);
|
||||
|
||||
return [tokens[pos2 - 1], queryResult, matching]; //HACK: match piggybacking on the tuple
|
||||
});
|
||||
|
||||
pos = eatBlock(tokens, pos + 2);
|
||||
} else {
|
||||
//token is something else?
|
||||
throw 'Found something not in the type graph: ' + tokens[pos - 1] + " " + (pos - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//eat the end bracket
|
||||
if (tokens[pos - 1] !== '}') {
|
||||
throw 'Expected \'}\' at the end of query (found ' + tokens[pos - 1] + ')';
|
||||
}
|
||||
|
||||
let results = handler[superType](parent, scalarFields, superMatching);
|
||||
|
||||
//WTF: related to the recusion above (turning the results inside out?)
|
||||
results = await Promise.all(results.map(async res => {
|
||||
const tuples = await Promise.all(deferredCalls.map(async call => await call(res)));
|
||||
|
||||
if (!tuples.every(tuple => !tuple[2] || tuple[1].length > 0)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
tuples.forEach(tuple => res[tuple[0]] = tuple[1]);
|
||||
|
||||
return res;
|
||||
}));
|
||||
|
||||
results = results.filter(r => !Array.isArray(r) || r.length > 0);
|
||||
|
||||
return [results, pos];
|
||||
};
|
||||
|
||||
module.exports = parseQuery;
|
||||
Reference in New Issue
Block a user