mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
Created the type graph
This commit is contained in:
14
server/schema.js
Normal file
14
server/schema.js
Normal file
@@ -0,0 +1,14 @@
|
||||
module.exports = `
|
||||
scalar Date
|
||||
|
||||
type Book {
|
||||
String title
|
||||
Author author
|
||||
Date published
|
||||
}
|
||||
|
||||
type Author {
|
||||
!String name
|
||||
!Book[] books
|
||||
}
|
||||
`;
|
||||
21
server/server.js
Normal file
21
server/server.js
Normal file
@@ -0,0 +1,21 @@
|
||||
//express for testing
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const app = express();
|
||||
|
||||
//test the library
|
||||
const schema = require('./schema.js');
|
||||
//const handler = requre('./handler.js');
|
||||
const simpleQL = require('./simpleQL');
|
||||
|
||||
const simple = simpleQL(schema);
|
||||
|
||||
//open the end
|
||||
app.post('simpleQL', (req, res) => {
|
||||
res.status(200).send(simple(req.body));
|
||||
});
|
||||
|
||||
//startup
|
||||
app.listen(process.env.WEB_PORT || 3100, (err) => {
|
||||
console.log(`listening to *:${process.env.WEB_PORT || 3100}`);
|
||||
});
|
||||
104
server/simpleQL/index.js
Normal file
104
server/simpleQL/index.js
Normal file
@@ -0,0 +1,104 @@
|
||||
//the main function to be returned
|
||||
const main = (schema, handler) => {
|
||||
const typeGraph = buildTypeGraph(schema);
|
||||
|
||||
console.log(typeGraph);
|
||||
|
||||
//the receiving function - this will be called multiple times
|
||||
return reqBody => {
|
||||
return '';
|
||||
};
|
||||
};
|
||||
|
||||
//parse the schema into a type graph
|
||||
const buildTypeGraph = schema => {
|
||||
//the default graph
|
||||
let graph = {
|
||||
String: { scalar: true },
|
||||
Integer: { scalar: true },
|
||||
Float: { scalar: true },
|
||||
Boolean: { scalar: true },
|
||||
};
|
||||
|
||||
//parse the schema
|
||||
const tokens = schema.split(/(\s+)/).filter(s => s.trim().length > 0);
|
||||
let pos = 0;
|
||||
|
||||
while (tokens[pos]) {
|
||||
//check for keywords
|
||||
switch(tokens[pos++]) {
|
||||
case 'type':
|
||||
graph[tokens[pos]] = parseCompoundType(tokens, pos);
|
||||
|
||||
//advance to the end of the compound type
|
||||
while(tokens[pos] && tokens[pos++] != '}');
|
||||
|
||||
break;
|
||||
|
||||
case 'scalar':
|
||||
graph[tokens[pos++]] = { scalar: true };
|
||||
break;
|
||||
|
||||
default:
|
||||
throw 'Unknown token ' + tokens[pos -1];
|
||||
}
|
||||
}
|
||||
|
||||
return graph;
|
||||
};
|
||||
|
||||
const parseCompoundType = (tokens, pos) => {
|
||||
pos++; //eat the compound name
|
||||
|
||||
//format check (not strictly necessary, but it looks nice)
|
||||
if (tokens[pos++] != '{') {
|
||||
throw 'Expected \'{\' in compound type definition';
|
||||
}
|
||||
|
||||
//graph component to be returned
|
||||
const compound = {};
|
||||
|
||||
//for each line of the compound type
|
||||
while (tokens[pos] && tokens[pos] != '}') {
|
||||
let type = tokens[pos++];
|
||||
const name = tokens[pos++];
|
||||
|
||||
//parse the extra typing data
|
||||
let array = false;
|
||||
let nullable = true;
|
||||
|
||||
//not nullable
|
||||
if (type[0] === '!') {
|
||||
nullable = false;
|
||||
type = type.slice(1);
|
||||
}
|
||||
|
||||
//is array
|
||||
if (type.endsWith('[]')) {
|
||||
array = true;
|
||||
type = type.slice(0, type.length - 2);
|
||||
}
|
||||
|
||||
//no mangled types or names
|
||||
checkAlphaNumeric(type);
|
||||
checkAlphaNumeric(name);
|
||||
|
||||
//finally, push to the compound definition
|
||||
compound[name] = {
|
||||
typeName: type,
|
||||
array: array,
|
||||
nullable: nullable,
|
||||
};
|
||||
}
|
||||
|
||||
return compound;
|
||||
}
|
||||
|
||||
const checkAlphaNumeric = (str) => {
|
||||
if (!/^[a-z0-9]+$/i.test(str)) {
|
||||
throw 'Unexpected string ' + str;
|
||||
}
|
||||
};
|
||||
|
||||
//return
|
||||
module.exports = main;
|
||||
Reference in New Issue
Block a user