pokemon example is working

This commit is contained in:
2020-09-07 14:31:17 +10:00
parent d99a99981b
commit 792ea6b23d
6 changed files with 94 additions and 105 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,53 +0,0 @@
//the authors
let authors = [
{
name: 'J.K. Roaring',
books: [
{ title: 'The Philosepher\'s Kidney Stone' },
{ title: 'The Chamber Pot of Secrets' },
{ title: 'The Prisoner of Aunt Kazban' },
{ title: 'The Goblet of the Fire Cocktail' },
{ title: 'The Order for Kleenex' },
{ title: 'The Half-Priced Pharmacy' },
{ title: 'Yeah, I Got Nothing' },
]
},
{
name: 'Kenneth Grahame',
books: [
{ title: 'The Wind in the Willows', published: '1 April 1908' }
]
},
{
name: 'Kayne Ruse',
books: [
{ title: 'alpha', published: "1" },
{ title: 'beta', published: "2" },
{ title: 'gamma', published: "3" },
{ title: 'delta', published: "4" },
{ title: 'epsilon', published: "5" },
]
},
];
//insert the authors into the books (relationship)
authors = authors.map(a => {
a.books = a.books.map(b => {
b.author = a;
return b;
});
return a;
});
//get the books array
let books = [];
authors.forEach(a => books = books.concat(a.books));
//the fake database
module.exports = {
authors,
books,
};

View File

@@ -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
});
});
//return all book fields after filtering
const fields = scalars.map(s => s.name);
return books.map(b => {
const ret = {};
if (fields.includes('title')) {
ret.title = b.title;
if (!s.match) {
return true;
}
if (fields.includes('published')) {
ret.published = b.published;
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;
});
},
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
//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)
});
//return all author fields after filtering
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;

1
server/pokemon.json Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,14 +1,18 @@
module.exports = `
scalar Date
type Book {
String title
Author author
Date published
type Pokemon {
String name
Integer height
Integer weight
Stats stats
Pokemon forms
}
type Author {
String name
Book books
type Stats {
Integer hp
Integer attack
Integer defense
Integer specialAttack
Integer specialDefense
Integer speed
}
`;

View File

@@ -217,7 +217,7 @@ const parseQuery = async (handler, tokens, pos, typeGraph, parent = null) => {
{ typeName: queryType, scalars: scalarFields, context: result, match: match } //parent object (this one)
);
return [tokens[pos2], queryResult, match]; //HACK: match piggybacking on the tuple
return [tokens[pos2 - 1], queryResult, match]; //HACK: match piggybacking on the tuple
});
pos = eatBlock(tokens, pos + 2);