Finished refactoring, it still dumps everything

This commit is contained in:
2020-09-05 02:12:36 +10:00
parent 7fa4c56bcc
commit d52b63bcf4
3 changed files with 33 additions and 25 deletions

View File

@@ -11,7 +11,7 @@ const database = require('./database.js');
//the handler routines //the handler routines
const handler = { const handler = {
Book: (parent, scalars) => { Book: (parent, scalars) => {
//takes an object which is the result of the parent query, if there is one { typeName: 'Author', scalars: [scalars] } //takes an object which is the result of the parent query, if there is one { typeName: 'Author', scalars: [scalars], context: the parent object }
//takes an array of scalar types as objects: { typeName: 'String', name: 'title' } //takes an array of scalar types as objects: { typeName: 'String', name: 'title' }
//must return an array of objects containing the results //must return an array of objects containing the results

View File

@@ -2,13 +2,13 @@ module.exports = `
scalar Date scalar Date
type Book { type Book {
!String title String title
Author author Author author
Date published Date published
} }
type Author { type Author {
!String name String name
Book[] books Book books
} }
`; `;

View File

@@ -1,5 +1,5 @@
//reserved keywords that can't be used as identifiers //reserved keywords that can't be used as identifiers
const keywords = ['type', 'scalar']; const keywords = ['type', 'scalar', 'create', 'update', 'delete', 'set', 'match'];
//the main function to be returned //the main function to be returned
const main = (schema, handler) => { const main = (schema, handler) => {
@@ -146,42 +146,46 @@ const parseCompoundType = (tokens, pos) => {
}; };
const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => { const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => {
//returns an object result from handler //returns an object result from handler for all custom types
//get the "parent object" contents for sub-objects //determine this type
// const queryName = typeGraph[tokens[pos]] ? null : tokens[pos]; //if you're a type, name = null let queryType;
// const queryType = typeGraph[tokens[pos]] ? tokens[pos] : typeGraph[parent.typeName][tokens[pos]].typeName; //use this type or derive the type from the parent
if (typeGraph[tokens[pos]] && typeGraph[tokens[pos]].scalar) {
queryType = tokens[pos];
}
else if (parent && typeGraph[parent.typeName][tokens[pos]]) {
queryType = typeGraph[parent.typeName][tokens[pos]].typeName;
} else {
queryType = tokens[pos];
}
//move on //move on
pos++; pos++;
//the opening brace
if (tokens[pos++] != '{') { if (tokens[pos++] != '{') {
throw 'Expected \'{\' in query, found ' + tokens[pos - 1]; throw 'Expected \'{\' after queried type';
} }
//the scalars to pass to the handler //the scalars to pass to the handler
const scalarFields = []; const scalarFields = [];
const deferredCalls = []; //functions (promises) that will be called at the end of this function 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 while(tokens[pos] && 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 //prevent using keywords
if (['create', 'update', 'delete', 'set', 'match'].includes(tokens[pos])) { if (keywords.includes(tokens[pos])) {
throw 'Unexpected keyword ' + tokens[pos]; throw 'Unexpected keyword ' + tokens[pos];
} }
//type is a scalar, and can be queried //type is a scalar, and can be queried
if (typeGraph[typeGraph[tokens[pos]].typeName].scalar) { if (typeGraph[queryType] && typeGraph[queryType][tokens[pos]] && typeGraph[typeGraph[queryType][tokens[pos]].typeName].scalar) {
//push the scalar object to the queryFields //push the scalar object to the queryFields
scalarFields.push({ typeName: typeGraph[tokens[pos]].typeName, name: tokens[pos] }); scalarFields.push({ typeName: typeGraph[queryType][tokens[pos]].typeName, name: tokens[pos] });
pos++; pos++;
} else { }
else if (typeGraph[queryType] && typeGraph[queryType][tokens[pos]] && !typeGraph[typeGraph[queryType][tokens[pos]].typeName].scalar) {
const pos2 = pos; //cache the value to keep it from changing const pos2 = pos; //cache the value to keep it from changing
//recurse //recurse
@@ -190,10 +194,14 @@ const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => {
tokens, tokens,
pos2, pos2,
typeGraph, typeGraph,
{ typeName: queryType, scalars: scalarFields } //parent object { typeName: queryType, scalars: scalarFields, context: result } //parent object
)]); )]);
pos = eatBlock(tokens, pos); pos = eatBlock(tokens, pos + 2);
} else {
//token is something else?
console.log('something else: ', tokens[pos], pos);
pos++;
} }
} }
@@ -202,7 +210,7 @@ const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => {
let results = handler[queryType](parent, scalarFields); let results = handler[queryType](parent, scalarFields);
//WTF //WTF: related to the recusion above
results = await Promise.all(results.map(async res => { results = await Promise.all(results.map(async res => {
const tuples = await Promise.all(deferredCalls.map(async call => await call(res))); const tuples = await Promise.all(deferredCalls.map(async call => await call(res)));
@@ -230,7 +238,7 @@ const eatBlock = (tokens, pos) => {
} }
} }
return ++pos; return ++pos; //eat the final '}'
}; };
//return //return