mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
turned test into a prompt
This commit is contained in:
20
README.md
20
README.md
@@ -20,9 +20,9 @@ app.use(express.text());
|
||||
//test the library
|
||||
const sineQL = require('sineql');
|
||||
const schema = require('./schema.js');
|
||||
const handler = require('./handler.js');
|
||||
const queryHandler = require('./query-handler.js');
|
||||
|
||||
const sine = sineQL(schema, handler, { debug: true });
|
||||
const sine = sineQL(schema, { queryHandler }, { debug: true });
|
||||
|
||||
//open the endpoint
|
||||
app.post('/sineql', async (req, res) => {
|
||||
@@ -56,13 +56,17 @@ module.exports = schema;
|
||||
```
|
||||
|
||||
```js
|
||||
//TODO: define the handler object's API properly
|
||||
const handler = {
|
||||
Book: () => null,
|
||||
Author: () => null
|
||||
//there's a different handler object for query, create, update and delete
|
||||
const queryHandler = {
|
||||
Author: (query, graph) => {
|
||||
//TODO: implement this
|
||||
},
|
||||
Book: (query, graph) => {
|
||||
//TODO: implement this
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = handler;
|
||||
module.exports = queryHandler;
|
||||
```
|
||||
|
||||
Create a matching client-side function pointing to the server.
|
||||
@@ -140,7 +144,7 @@ The fields can be altered as well, using the query language's built-in keywords:
|
||||
* set
|
||||
* typeName
|
||||
|
||||
`create`, `update` and `delete` are still to be defined properly, but they'll probably work like this:
|
||||
`create`, `update` and `delete` are still to be defined properly, but they'll probably work as follows.
|
||||
|
||||
### Create
|
||||
|
||||
|
||||
@@ -52,9 +52,9 @@ const books = {
|
||||
const authors = {
|
||||
findAll: async args => {
|
||||
let arr = [
|
||||
{ name: 'Kenneth Grahame', books: [1] },
|
||||
{ name: 'Frank', books: [1, 2] },
|
||||
{ name: 'Betty', books: [2] }
|
||||
{ id: 1, name: 'Kenneth Grahame', books: [1] },
|
||||
{ id: 2, name: 'Frank', books: [1, 2] },
|
||||
{ id: 3, name: 'Betty', books: [2] }
|
||||
];
|
||||
|
||||
const { attributes, where } = args;
|
||||
@@ -129,6 +129,9 @@ const queryHandlers = {
|
||||
//get the fields alone
|
||||
const { typeName, ...fields } = query;
|
||||
|
||||
//hack the id into the fields list (if it's not there already)
|
||||
fields['id'] = fields['id'] || { typeName: 'Integer', scalar: true };
|
||||
|
||||
//get the names of matched fields (fields to find)
|
||||
const matchedNames = Object.keys(fields).filter(field => fields[field].match);
|
||||
|
||||
@@ -158,7 +161,7 @@ const queryHandlers = {
|
||||
|
||||
//for each author, update this non-scalar field with the non-scalar's recursed value
|
||||
authorResults.forEach(author => {
|
||||
author[nonScalar] = nonScalarArray.filter(ns => author[nonScalar].includes(ns.id)); //using the hacked-in ID field
|
||||
author[nonScalar] = nonScalarArray.filter(ns => author[nonScalar].includes(ns.id)); //using the hacked-in ID field (JOIN)
|
||||
});
|
||||
|
||||
//prune the authors when matching, but their results are empty
|
||||
@@ -180,8 +183,8 @@ const queryHandlers = {
|
||||
//get the fields alone
|
||||
const { typeName, ...fields } = query;
|
||||
|
||||
//hack the book id into the fields list (if it's not there already)
|
||||
fields.id = fields.id || { typeName: 'Integer', scalar: true };
|
||||
//hack the id into the fields list (if it's not there already)
|
||||
fields['id'] = fields['id'] || { typeName: 'Integer', scalar: true };
|
||||
|
||||
//get the names of matched fields
|
||||
const matchedNames = Object.keys(fields).filter(field => fields[field].match);
|
||||
@@ -220,14 +223,39 @@ type Author {
|
||||
}
|
||||
`;
|
||||
|
||||
//input tool
|
||||
const readline = require('readline');
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: false
|
||||
});
|
||||
|
||||
const question = (prompt, def = null) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
rl.question(`${prompt}${def ? ` (${def})` : ''}> `, answer => {
|
||||
//loop on required
|
||||
if (def === null && !answer) {
|
||||
return resolve(question(prompt, def));
|
||||
}
|
||||
|
||||
return resolve(answer || def);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
//the library to test
|
||||
const sineQL = require('../source/index.js');
|
||||
|
||||
//run the function in debug mode (builds type graph)
|
||||
const sine = sineQL(schema, { queryHandlers }, { debug: false });
|
||||
|
||||
//actually ask the question
|
||||
(async () => {
|
||||
const [code, result] = await sine('Author { match name "Frank" books { title published } }');
|
||||
|
||||
while(true) {
|
||||
const answer = await question('sineQL');
|
||||
const [code, result] = await sine(answer);
|
||||
console.dir(result, { depth: null });
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user