Refined the debugging

This commit is contained in:
2021-03-30 06:48:17 +11:00
parent 9d17d350fa
commit 9f4b869d84
7 changed files with 50 additions and 13 deletions

View File

@@ -2,7 +2,9 @@
sineQL is a web API query language that mimics graphQL, designed solely for fun. sineQL is a web API query language that mimics graphQL, designed solely for fun.
sineQL consists of two languages - the schema language, and the query language. sineQL assumes that the records are related in a non-looping tree-structure, defined by the schema language. sineQL consists of two languages - the schema language, and the query language. sineQL assumes that the records are related in a non-looping tree-structure, defined by the schema language. Also, each non-scalar type queried is returned as an array.
The handler's definition is left up to the user.
## Example Server ## Example Server

View File

@@ -1,11 +1,9 @@
{ {
"name": "sineql", "name": "sineql",
"version": "0.2.1", "version": "0.3.0",
"description": "A simple to use graphQL clone", "description": "A simple to use graphQL clone",
"main": "source/index.js", "main": "source/index.js",
"scripts": { "scripts": {
"test": "cd test-server-books && npm install && npm run node",
"pokemon": "cd test-server-pokemon && npm install && npm run node"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -22,7 +22,7 @@ const buildTypeGraph = (schema, options) => {
switch(tokens[pos - 1]) { switch(tokens[pos - 1]) {
case 'type': case 'type':
//delegate //delegate
graph[tokens[pos++]] = parseCompoundType(tokens, pos, Object.keys(graph), options); graph[tokens[pos++]] = parseCompoundType(tokens, pos, Object.keys(graph), { ...options, debugName: tokens[pos - 1] });
//advance to the end of the compound type //advance to the end of the compound type
pos = eatBlock(tokens, pos); pos = eatBlock(tokens, pos);
@@ -31,12 +31,16 @@ const buildTypeGraph = (schema, options) => {
case 'scalar': case 'scalar':
//check against keyword list //check against keyword list
if (keywords.includes(graph[tokens[pos - 1]])) { if (keywords.includes(tokens[pos])) {
//TODO: test this error throw 'Unexpected keyword ' + tokens[pos];
throw 'Unexpected keyword ' + graph[tokens[pos - 1]];
} }
graph[tokens[pos++]] = { scalar: true }; graph[tokens[pos++]] = { scalar: true };
if (options.debug) {
console.log(`Defined ${tokens[pos - 1]}:\n`, graph[tokens[pos - 1]]);
}
break; break;
default: default:
@@ -45,7 +49,7 @@ const buildTypeGraph = (schema, options) => {
} }
if (options.debug) { if (options.debug) {
console.log('Type Graph:\n', graph, '\n'); console.log('\nType Graph:\n', graph, '\n');
} }
return graph; return graph;
@@ -76,7 +80,7 @@ const parseCompoundType = (tokens, pos, scalars, options) => {
} }
//can only use existing types (prevents looping tree structure) //can only use existing types (prevents looping tree structure)
if (!scalars.includes(type)) { //TODO: test this error if (!scalars.includes(type)) {
throw `Unexpected value found as type field ('${type}' is undefined)`; throw `Unexpected value found as type field ('${type}' is undefined)`;
} }
@@ -92,7 +96,7 @@ const parseCompoundType = (tokens, pos, scalars, options) => {
} }
if (options.debug) { if (options.debug) {
console.log('Compound Type:\n', compound, '\n'); console.log(`Defined ${options.debugName}:\n`, compound);
} }
return compound; return compound;

View File

@@ -1,6 +1,5 @@
const buildTypeGraph = require('./build-type-graph'); const buildTypeGraph = require('./build-type-graph');
const parseInput = require('./parse-input'); const parseInput = require('./parse-input');
const parseQuery = require('./parse-query');
//the main function to be returned (sineQL()) //the main function to be returned (sineQL())
const sineQL = (schema, handler, options = {}) => { const sineQL = (schema, handler, options = {}) => {

View File

@@ -44,7 +44,7 @@ const parseInput = (body, allowStrings, options) => {
} }
if (options.debug) { if (options.debug) {
console.log('Input:\n', tokens, '\n'); console.log('Input:', tokens, '\n');
} }
return tokens; return tokens;

23
test/index.js Normal file
View File

@@ -0,0 +1,23 @@
//the library to test
const sineQL = require('../source/index.js');
//the dummy values
const schema = `
scalar Date
type Book {
String title
Date published
}
type Author {
String name
Book books
}
`;
const handler = null;
//run the function in debug mode (builds type graph)
const sine = sineQL(schema, handler, { debug: true });

11
test/package.json Normal file
View File

@@ -0,0 +1,11 @@
{
"name": "sineql-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Kayne Ruse",
"license": "ISC"
}