diff --git a/server/handler.js b/server/handler.js index 5d23963..b35d3fe 100644 --- a/server/handler.js +++ b/server/handler.js @@ -11,7 +11,7 @@ const database = require('./database.js'); //the handler routines const handler = { 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' } //must return an array of objects containing the results diff --git a/server/schema.js b/server/schema.js index 41ad1d7..00775b9 100644 --- a/server/schema.js +++ b/server/schema.js @@ -2,13 +2,13 @@ module.exports = ` scalar Date type Book { - !String title + String title Author author Date published } type Author { - !String name - Book[] books + String name + Book books } `; diff --git a/server/simpleQL/index.js b/server/simpleQL/index.js index 2659740..3600c7c 100644 --- a/server/simpleQL/index.js +++ b/server/simpleQL/index.js @@ -1,5 +1,5 @@ //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 const main = (schema, handler) => { @@ -146,42 +146,46 @@ const parseCompoundType = (tokens, pos) => { }; 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 -// const queryName = typeGraph[tokens[pos]] ? null : tokens[pos]; //if you're a type, name = null -// const queryType = typeGraph[tokens[pos]] ? tokens[pos] : typeGraph[parent.typeName][tokens[pos]].typeName; //use this type or derive the type from the parent + //determine this type + let queryType; + + 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 pos++; - //the opening brace if (tokens[pos++] != '{') { - throw 'Expected \'{\' in query, found ' + tokens[pos - 1]; + throw 'Expected \'{\' after queried type'; } //the scalars to pass to the handler const scalarFields = []; 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 - //not the end of the query - if (!tokens[pos]) { - throw 'Expected field in query, got end'; - } - + while(tokens[pos] && tokens[pos] != '}') { //while not at the end of this block //prevent using keywords - if (['create', 'update', 'delete', 'set', 'match'].includes(tokens[pos])) { + if (keywords.includes(tokens[pos])) { throw 'Unexpected keyword ' + tokens[pos]; } //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 - scalarFields.push({ typeName: typeGraph[tokens[pos]].typeName, name: tokens[pos] }); + scalarFields.push({ typeName: typeGraph[queryType][tokens[pos]].typeName, name: tokens[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 //recurse @@ -190,10 +194,14 @@ const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => { tokens, pos2, 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); - //WTF + //WTF: related to the recusion above results = await Promise.all(results.map(async 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