mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
throw 'Nothing here is working';
This commit is contained in:
@@ -4,7 +4,8 @@
|
|||||||
"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"
|
"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",
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
/* DOCS: parameter types
|
/* DOCS: handler parameter types
|
||||||
parent: Type | null
|
parent: Type | null
|
||||||
scalars: [{ typeName: String, name: String, filter: any | null }, ...]
|
scalars: [{ typeName: String, name: String, filter: any | null }, ...]
|
||||||
matching: Boolean
|
matching: Boolean
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
//BUG: Book { author { name } } - this gives a weird result
|
||||||
|
|
||||||
const database = require('./database.js');
|
const database = require('./database.js');
|
||||||
|
|
||||||
//the handler routines
|
//the handler routines
|
||||||
const handler = {
|
const handler = {
|
||||||
//type queries
|
//type queries
|
||||||
Author: (parent, scalars, matching) => {
|
Author: (parent, scalars, matching) => {
|
||||||
console.log("Author", parent ? parent.context : null);
|
|
||||||
|
|
||||||
let authors;
|
let authors;
|
||||||
|
|
||||||
//check parentage
|
//check parentage
|
||||||
@@ -92,8 +92,6 @@ const handler = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Book: (parent, scalars, matching) => {
|
Book: (parent, scalars, matching) => {
|
||||||
console.log("Book", parent ? parent.context : null);
|
|
||||||
|
|
||||||
let books;
|
let books;
|
||||||
|
|
||||||
//check parentage
|
//check parentage
|
||||||
|
|||||||
@@ -1,135 +1,179 @@
|
|||||||
const pokemon = require('./pokemon.json');
|
/* DOCS: handler parameter types
|
||||||
|
parent: Type | null
|
||||||
|
scalars: [{ typeName: String, name: String, filter: any | null }, ...]
|
||||||
|
matching: Boolean
|
||||||
|
*/
|
||||||
|
|
||||||
|
const database = require('./pokemon.json');
|
||||||
|
|
||||||
//the handler routines
|
//the handler routines
|
||||||
const handler = {
|
const handler = {
|
||||||
Pokemon: (parent, scalars) => {
|
Pokemon: (parent, scalars, matching) => {
|
||||||
//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 }
|
throw 'Nothing here is working';
|
||||||
//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;
|
let pokemon = database;
|
||||||
|
|
||||||
//if this is a sub-query of Pokemon (a form), use the parent to narrow the search
|
//if this is a sub-query of Pokemon (a form), use the parent to narrow the search
|
||||||
if (parent && parent.typeName == 'Pokemon') {
|
if (parent && parent.typeName == 'Pokemon') {
|
||||||
//filter based on parent object
|
//filter based on parent object + scalars
|
||||||
filteredPokemon = filteredPokemon.filter(poke => poke.name == parent.context.name);
|
pokemon = pokemon.filter(poke => {
|
||||||
filteredPokemon = filteredPokemon[0].forms;
|
return scalars.every(scalar => poke[scalar.name] == parent.context[scalar.name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
pokemon = pokemon.map(poke => poke.forms)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
//if this query has a matched scalar, filter by that match
|
//I am being matched (if true, ALL present scalars will have a filter field)
|
||||||
filteredPokemon = filteredPokemon.filter(poke => {
|
if (matching) {
|
||||||
return scalars.every(s => {
|
//check every scalar to every poke - a single false match is a miss on that poke
|
||||||
if (!s.match) {
|
pokemon = pokemon.filter(poke => {
|
||||||
return true;
|
return scalars.every(scalar => {
|
||||||
}
|
//handle each type of scalar
|
||||||
|
switch (scalar.typeName) {
|
||||||
|
case 'String':
|
||||||
|
case 'Integer':
|
||||||
|
case 'Float':
|
||||||
|
case 'Boolean':
|
||||||
|
return poke[scalar.name].toString() == scalar.filter; //dumb comparison for now
|
||||||
|
|
||||||
if (typeof poke[s.name] == 'string') {
|
default:
|
||||||
return poke[s.name].toUpperCase() === s.match.toUpperCase(); //other filter methods, such as ranges of numbers, can also be implemented
|
throw `Unknown scalar typeName in handler: ${scalar.typeName} (${scalar.name})`;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
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
|
//if there are no pokemon left, then the filters missed matches
|
||||||
const fields = scalars.map(s => s.name);
|
if (pokemon.length == 0) {
|
||||||
return filteredPokemon.map(p => {
|
return [];
|
||||||
const ret = {};
|
|
||||||
|
|
||||||
if (fields.includes('name')) {
|
|
||||||
ret.name = p.name;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (fields.includes('height')) {
|
//scalars are being matched on their own
|
||||||
ret.height = p.height;
|
if (scalars.some(s => s.filter)) {
|
||||||
}
|
//check every scalar to every poke - a single match is a hit
|
||||||
|
pokemon = pokemon.filter(poke => {
|
||||||
|
return scalars.some(scalar => {
|
||||||
|
//handle each type of scalar
|
||||||
|
switch (scalar.typeName) {
|
||||||
|
case 'String':
|
||||||
|
case 'Integer':
|
||||||
|
case 'Float':
|
||||||
|
case 'Boolean':
|
||||||
|
console.log(scalar, poke[scalar.name]);
|
||||||
|
return poke[scalar.name].toString() == scalar.filter; //dumb comparison for now
|
||||||
|
|
||||||
if (fields.includes('weight')) {
|
default:
|
||||||
ret.weight = p.weight;
|
throw `Unknown scalar typeName in handler: ${scalar.typeName} (${scalar.name})`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//if there are no pokemon left, then the filters missed matches
|
||||||
|
if (pokemon.length == 0) {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//process (filter out unwanted fields) and return the array of pokemon
|
||||||
|
return pokemon.map(poke => {
|
||||||
|
let ret = {};
|
||||||
|
|
||||||
|
//that's a big O(damn)
|
||||||
|
scalars.forEach(scalar => {
|
||||||
|
ret[scalar.name] = poke[scalar.name];
|
||||||
|
});
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
Stats: (parent, scalars) => {
|
Stats: (parent, scalars, matching) => {
|
||||||
|
throw 'Nothing here is working';
|
||||||
|
|
||||||
if (!parent || parent.typeName != 'Pokemon') {
|
if (!parent || parent.typeName != 'Pokemon') {
|
||||||
throw 'Stats must be inside a Pokemon query';
|
throw 'Stats must be inside a Pokemon query';
|
||||||
}
|
}
|
||||||
|
|
||||||
let filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
|
console.log(parent.scalars, scalars);
|
||||||
|
|
||||||
//if this is a sub-query of Pokemon, use the parent to narrow the search
|
//skip unknown/empty pokemon stats
|
||||||
filteredPokemon = filteredPokemon.filter(poke => poke.name == parent.context.name);
|
let pokemon = database.filter(poke => poke.base_stats != null);
|
||||||
|
|
||||||
//handle forms
|
//if this is a sub-query of Pokemon (already checked), use the parent to narrow the search
|
||||||
if (filteredPokemon.length == 0) {
|
pokemon = pokemon.filter(poke => {
|
||||||
filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
|
return scalars.every(scalar => poke[scalar.name] == parent.context[scalar.name]);
|
||||||
filteredPokemon = filteredPokemon.filter(poke => {
|
});
|
||||||
return poke.forms.some(form => form.name == parent.context.name)
|
|
||||||
|
//handle forms instead of normal queries
|
||||||
|
if (pokemon.length == 0) {
|
||||||
|
pokemon = database.filter(poke => poke.base_stats != null);//skip unknown/empty pokemon stats
|
||||||
|
|
||||||
|
pokemon = pokemon.map(p => p.forms);
|
||||||
|
pokemon = [].concat(...pokemon);
|
||||||
|
|
||||||
|
pokemon = pokemon.filter(poke => {
|
||||||
|
return poke.forms.some(form => {
|
||||||
|
return scalars.every(scalar => form[scalar.name] == parent.context[scalar.name]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
filteredPokemon = filteredPokemon[0].forms.filter(form => form.name == parent.context.name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//return all pokemon fields after filtering
|
//I am being matched (if true, ALL present scalars will have a filter field)
|
||||||
const fields = scalars.map(s => s.name);
|
if (matching) {
|
||||||
return filteredPokemon.map(p => {
|
//check every scalar to every poke - a single false match is a miss on that poke
|
||||||
//BUGFIX
|
pokemon = pokemon.filter(poke => {
|
||||||
if (!p.base_stats) {
|
return scalars.every(scalar => {
|
||||||
return null;
|
//handle each type of scalar
|
||||||
}
|
switch (scalar.typeName) {
|
||||||
|
case 'Integer':
|
||||||
|
return poke.base_stats[scalar.name] === parseInt(scalar.filter); //dumb comparison for now
|
||||||
|
|
||||||
const ret = {};
|
default:
|
||||||
|
throw `Unhandled scalar typeName in Stats handler: ${scalar.typeName} (${scalar.name})`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (fields.includes('hp')) {
|
//if there are no pokemon left, then the filters missed matches
|
||||||
ret.hp = p.base_stats.hp;
|
if (pokemon.length == 0) {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (fields.includes('attack')) {
|
//scalars are being matched on their own
|
||||||
ret.attack = p.base_stats.attack;
|
if (scalars.some(s => s.filter)) {
|
||||||
}
|
//check every scalar to every poke - a single match is a hit
|
||||||
|
pokemon = pokemon.filter(poke => {
|
||||||
|
return scalars.some(scalar => {
|
||||||
|
//handle each type of scalar
|
||||||
|
switch (scalar.typeName) {
|
||||||
|
case 'Integer':
|
||||||
|
return poke.base_stats[scalar.name] === parseInt(scalar.filter); //dumb comparison for now
|
||||||
|
|
||||||
if (fields.includes('defense')) {
|
default:
|
||||||
ret.defense = p.base_stats.defense;
|
throw `Unhandled scalar typeName in Stats handler: ${scalar.typeName} (${scalar.name})`;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (fields.includes('specialAttack')) {
|
//if there are no pokemon left, then the filters missed matches
|
||||||
ret.specialAttack = p.base_stats.specialAttack;
|
if (pokemon.length == 0) {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (fields.includes('specialDefense')) {
|
//process (filter out unwanted fields) and return the array of pokemon
|
||||||
ret.specialDefense = p.base_stats.specialDefense;
|
return pokemon.map(poke => {
|
||||||
}
|
let ret = {};
|
||||||
|
|
||||||
if (fields.includes('speed')) {
|
//that's a big O(damn)
|
||||||
ret.speed = p.base_stats.speed;
|
scalars.forEach(scalar => {
|
||||||
}
|
ret[scalar.name] = poke.base_stats[scalar.name];
|
||||||
|
});
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
create: (matches, sets) => {
|
|
||||||
//TODO
|
|
||||||
},
|
|
||||||
|
|
||||||
update: (matches, sets) => {
|
|
||||||
//TODO
|
|
||||||
},
|
|
||||||
|
|
||||||
delete: matches => {
|
|
||||||
//TODO
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = handler;
|
module.exports = handler;
|
||||||
|
|||||||
Reference in New Issue
Block a user