Stripped out the bad code

This commit is contained in:
2020-03-06 16:01:22 +11:00
parent 66d8cd7630
commit fdb88859d1
2 changed files with 17 additions and 75 deletions

View File

@@ -1,35 +1,13 @@
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
return database.books.map(b => {
const ret = {};
if (scalars.some(s => s.name == 'title')) {
ret.title = b.title;
}
if (scalars.some(s => s.name == 'author')) {
ret.author = b.author;
}
if (scalars.some(s => s.name == 'published')) {
ret.published = b.published;
}
return ret;
});
//TODO
},
Author: scalars => {
//takes an array of scalar types as objects: { typeName: 'String', name: 'name' }
//must return an array of objects containing the results
//TODO
},
create: (sets, matches) => {

View File

@@ -22,9 +22,9 @@ const main = (schema, handler) => {
//no leading keyword - regular query
default:
const [result] = parseQuery(handler, tokens, pos, typeGraph[tokens[pos - 1]], typeGraph);
//TODO: parse the query
return [200, result];
return [200, ''];
//TODO
break;
@@ -41,10 +41,10 @@ const main = (schema, handler) => {
const buildTypeGraph = schema => {
//the default graph
let graph = {
String: { scalar: true, nullable: true },
Integer: { scalar: true, nullable: true },
Float: { scalar: true, nullable: true },
Boolean: { scalar: true, nullable: true },
String: { scalar: true },
Integer: { scalar: true },
Float: { scalar: true },
Boolean: { scalar: true },
};
//parse the schema
@@ -63,6 +63,10 @@ const buildTypeGraph = schema => {
break;
case 'scalar':
if (['type', 'scalar'].includes(graph[tokens[pos]])) {
throw 'Unexpected keyword ' + graph[tokens[pos]];
}
graph[tokens[pos++]] = { scalar: true };
break;
@@ -106,6 +110,11 @@ const parseCompoundType = (tokens, pos) => {
type = type.slice(0, type.length - 2);
}
//can't use keywords
if (['type', 'scalar'].includes(type) || ['type', 'scalar'].includes(name)) {
throw 'Unexpected keyword found as type field or type name';
}
//no mangled types or names
checkAlphaNumeric(type);
checkAlphaNumeric(name);
@@ -121,51 +130,6 @@ const parseCompoundType = (tokens, pos) => {
return compound;
};
const parseQuery = (handler, tokens, pos, typeGraph, superTypeGraph) => {
//cache this for later
const startPos = pos;
//the opening brace
if (tokens[pos++] != '{') {
throw 'Expected \'{\' in query, found ' + tokens[pos - 1];
}
//the fields to pass to the handler
const queryFields = [];
//while not at the end
while (tokens[pos] != '}') {
//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
queryFields.push({ typeName: typeGraph[tokens[pos]].typeName, name: tokens[pos] });
pos++;
} else {
//parse the subquery using the correct sub-typegraph
const [result, increment] = parseQuery(handler, tokens, pos + 1, superTypeGraph[typeGraph[tokens[pos]].typeName], superTypeGraph);
queryFields.push({ ...typeGraph[tokens[pos]], name: tokens[pos], subquery: handler[typeGraph[tokens[pos]].typeName](result) });
pos += increment + 1;
}
}
console.log(' handler', queryFields);
return [queryFields, pos - startPos];
};
//utils
const checkAlphaNumeric = (str) => {
if (!/^[a-z0-9]+$/i.test(str)) {