mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
pokemon example is working
This commit is contained in:
@@ -1,75 +1,113 @@
|
||||
const database = require('./database.js');
|
||||
|
||||
/* DOCS: The handler module connects the database to simpleQL.
|
||||
* this will be user-implemented, but for now uses a fake database provided in database.js
|
||||
* The 'scalars' will be objects containing a name and theh type for each scalar field requested by a query.
|
||||
* This can be used in several ways...
|
||||
*
|
||||
* The handlers should only handle scalar fields - compound fields will be queried separately by simpleQL.
|
||||
*/
|
||||
const pokemon = require('./pokemon.json');
|
||||
|
||||
//the handler routines
|
||||
const handler = {
|
||||
Book: (parent, scalars) => {
|
||||
//takes an object which is the result of the parent query, if there is one { typeName: 'Author', scalars: [scalars], context: the parent object, match: I am being matched }
|
||||
//takes an array of scalar types as objects: { typeName: 'String', name: 'title', match: filter }
|
||||
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 books = database.books;
|
||||
let filteredPokemon = pokemon;
|
||||
|
||||
//if this is a sub-query of Author, use the parent to narrow the search
|
||||
if (parent && parent.typeName == 'Author') {
|
||||
//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
|
||||
books = books.filter(b => b.author.name == parent.context.name);
|
||||
filteredPokemon = filteredPokemon.filter(poke => poke.name == parent.context.name);
|
||||
filteredPokemon = filteredPokemon[0].forms;
|
||||
}
|
||||
|
||||
//if this query has a matched scalar, filter by that match
|
||||
books = books.filter(b => {
|
||||
filteredPokemon = filteredPokemon.filter(poke => {
|
||||
return scalars.every(s => {
|
||||
return !s.match || b[s.name].toUpperCase() === s.match.toUpperCase(); //other filter methods, such as ranges of numbers, can also be implemented
|
||||
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 book fields after filtering
|
||||
//return all pokemon fields after filtering
|
||||
const fields = scalars.map(s => s.name);
|
||||
return books.map(b => {
|
||||
return filteredPokemon.map(p => {
|
||||
const ret = {};
|
||||
|
||||
if (fields.includes('title')) {
|
||||
ret.title = b.title;
|
||||
if (fields.includes('name')) {
|
||||
ret.name = p.name;
|
||||
}
|
||||
|
||||
if (fields.includes('published')) {
|
||||
ret.published = b.published;
|
||||
if (fields.includes('height')) {
|
||||
ret.height = p.height;
|
||||
}
|
||||
|
||||
if (fields.includes('weight')) {
|
||||
ret.weight = p.weight;
|
||||
}
|
||||
|
||||
return ret;
|
||||
});
|
||||
},
|
||||
|
||||
Author: (parent, scalars) => {
|
||||
let authors = database.authors;
|
||||
|
||||
//if this is a sub-query of Book, use the parent to find the author
|
||||
if (parent && parent.typeName == 'Book') {
|
||||
//filter based on parent object
|
||||
authors = authors.filter(a => a.books.some(b => b.title === parent.context.title));
|
||||
Stats: (parent, scalars) => {
|
||||
if (!parent || parent.typeName != 'Pokemon') {
|
||||
throw 'Stats must be inside a Pokemon query';
|
||||
}
|
||||
|
||||
//if this query has a matched scalar, filter by that match
|
||||
authors = authors.filter(a => {
|
||||
return scalars.every(s => {
|
||||
return !s.match || a[s.name].toUpperCase() === s.match.toUpperCase(); //other filter methods, such as ranges of numbers, can also be implemented
|
||||
});
|
||||
});
|
||||
let filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
|
||||
|
||||
//return all author fields after filtering
|
||||
//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 authors.map(a => {
|
||||
return filteredPokemon.map(p => {
|
||||
const ret = {};
|
||||
|
||||
if (fields.includes('name')) {
|
||||
ret.name = a.name;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user