Adjusted handler API

This commit is contained in:
2020-09-22 11:10:12 +10:00
parent 7f04a656c2
commit 0d245fdeb6
15 changed files with 2137 additions and 28 deletions

View File

@@ -0,0 +1,135 @@
const pokemon = require('./pokemon.json');
//the handler routines
const handler = {
Pokemon: (parent, scalars) => {
//takes an object which is the result of the parent query, if there is one { typeName: String, scalars: [scalars], context: the parent object, match: I am being matched }
//takes an array of scalar types as objects: { typeName: 'String', name: 'String', match: filter }
//must return an array of objects containing the results
let filteredPokemon = pokemon;
//if this is a sub-query of Pokemon (a form), use the parent to narrow the search
if (parent && parent.typeName == 'Pokemon') {
//filter based on parent object
filteredPokemon = filteredPokemon.filter(poke => poke.name == parent.context.name);
filteredPokemon = filteredPokemon[0].forms;
}
//if this query has a matched scalar, filter by that match
filteredPokemon = filteredPokemon.filter(poke => {
return scalars.every(s => {
if (!s.match) {
return true;
}
if (typeof poke[s.name] == 'string') {
return poke[s.name].toUpperCase() === s.match.toUpperCase(); //other filter methods, such as ranges of numbers, can also be implemented
}
if (typeof poke[s.name] == 'number') {
return poke[s.name] == s.match;
}
//handle form-only pokemon
if (typeof poke[s.name] == 'undefined') {
return false;
}
throw 'Unknown match type in Pokemon handler';
});
});
//return all pokemon fields after filtering
const fields = scalars.map(s => s.name);
return filteredPokemon.map(p => {
const ret = {};
if (fields.includes('name')) {
ret.name = p.name;
}
if (fields.includes('height')) {
ret.height = p.height;
}
if (fields.includes('weight')) {
ret.weight = p.weight;
}
return ret;
});
},
Stats: (parent, scalars) => {
if (!parent || parent.typeName != 'Pokemon') {
throw 'Stats must be inside a Pokemon query';
}
let filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
//if this is a sub-query of Pokemon, use the parent to narrow the search
filteredPokemon = filteredPokemon.filter(poke => poke.name == parent.context.name);
//handle forms
if (filteredPokemon.length == 0) {
filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
filteredPokemon = filteredPokemon.filter(poke => {
return poke.forms.some(form => form.name == parent.context.name)
});
filteredPokemon = filteredPokemon[0].forms.filter(form => form.name == parent.context.name);
}
//return all pokemon fields after filtering
const fields = scalars.map(s => s.name);
return filteredPokemon.map(p => {
//BUGFIX
if (!p.base_stats) {
return null;
}
const ret = {};
if (fields.includes('hp')) {
ret.hp = p.base_stats.hp;
}
if (fields.includes('attack')) {
ret.attack = p.base_stats.attack;
}
if (fields.includes('defense')) {
ret.defense = p.base_stats.defense;
}
if (fields.includes('specialAttack')) {
ret.specialAttack = p.base_stats.specialAttack;
}
if (fields.includes('specialDefense')) {
ret.specialDefense = p.base_stats.specialDefense;
}
if (fields.includes('speed')) {
ret.speed = p.base_stats.speed;
}
return ret;
});
},
create: (matches, sets) => {
//TODO
},
update: (matches, sets) => {
//TODO
},
delete: matches => {
//TODO
},
};
module.exports = handler;

1807
test-server-pokemon/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,20 @@
{
"name": "test-server",
"version": "0.2.1",
"description": "",
"main": "server.js",
"scripts": {
"start": "pm2 start server.js",
"restart": "pm2 restart server.js",
"stop": "pm2 stop server.js",
"list": "pm2 list",
"node": "node server.js"
},
"author": "Kayne Ruse",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"pm2": "^4.4.1"
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,18 @@
module.exports = `
type Pokemon {
String name
Integer height
Integer weight
Stats stats
Pokemon forms
}
type Stats {
Integer hp
Integer attack
Integer defense
Integer specialAttack
Integer specialDefense
Integer speed
}
`;

View File

@@ -0,0 +1,27 @@
//express for testing
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.use(bodyParser.text());
//test the library
const schema = require('./schema.js');
const handler = require('./handler.js');
const sineQL = require('../source/index.js');
const sine = sineQL(schema, handler, { debug: true });
//open the end
app.post('/sineql', async (req, res) => {
const [code, result] = await sine(req.body);
res.status(code).send(result);
});
//startup
app.listen(process.env.WEB_PORT || 3100, err => {
console.log(`listening to *:${process.env.WEB_PORT || 3100}`);
});