I tried, and I failed. Mad props to those who wrote graphQL

This commit is contained in:
2020-03-04 16:48:29 +11:00
parent 8ef1d0efbd
commit 66d8cd7630
4 changed files with 100 additions and 7 deletions

View File

@@ -30,7 +30,7 @@ authors = authors.map(a => {
return a; return a;
}); });
//get the book array //get the books array
let books = []; let books = [];
authors.forEach(a => books = books.concat(a.books)); authors.forEach(a => books = books.concat(a.books));

View File

@@ -7,6 +7,24 @@ const handler = {
Book: scalars => { Book: scalars => {
//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
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;
});
}, },
Author: scalars => { Author: scalars => {

View File

@@ -1,6 +1,8 @@
//express for testing //express for testing
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser');
const app = express(); const app = express();
app.use(bodyParser.text());
//test the library //test the library
const schema = require('./schema.js'); const schema = require('./schema.js');

View File

@@ -6,7 +6,34 @@ const main = (schema, handler) => {
//the receiving function - this will be called multiple times //the receiving function - this will be called multiple times
return reqBody => { return reqBody => {
return [200, '']; //parse the query
const tokens = reqBody.split(/(\s+)/).filter(s => s.trim().length > 0);
let pos = 0;
try {
//check for keywords
switch(tokens[pos++]) {
case 'create':
case 'update':
case 'delete':
throw 'keyword not implemented';
//TODO
break;
//no leading keyword - regular query
default:
const [result] = parseQuery(handler, tokens, pos, typeGraph[tokens[pos - 1]], typeGraph);
return [200, result];
//TODO
break;
}
}
catch(e) {
console.log('caught', e);
return [400, e.stack || e];
}
}; };
}; };
@@ -14,10 +41,10 @@ const main = (schema, handler) => {
const buildTypeGraph = schema => { const buildTypeGraph = schema => {
//the default graph //the default graph
let graph = { let graph = {
String: { scalar: true }, String: { scalar: true, nullable: true },
Integer: { scalar: true }, Integer: { scalar: true, nullable: true },
Float: { scalar: true }, Float: { scalar: true, nullable: true },
Boolean: { scalar: true }, Boolean: { scalar: true, nullable: true },
}; };
//parse the schema //parse the schema
@@ -92,8 +119,54 @@ const parseCompoundType = (tokens, pos) => {
} }
return compound; 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) => { const checkAlphaNumeric = (str) => {
if (!/^[a-z0-9]+$/i.test(str)) { if (!/^[a-z0-9]+$/i.test(str)) {
throw 'Unexpected string ' + str; throw 'Unexpected string ' + str;