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:
File diff suppressed because one or more lines are too long
@@ -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,
|
|
||||||
};
|
|
||||||
@@ -1,75 +1,113 @@
|
|||||||
const database = require('./database.js');
|
const pokemon = require('./pokemon.json');
|
||||||
|
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//the handler routines
|
//the handler routines
|
||||||
const handler = {
|
const handler = {
|
||||||
Book: (parent, scalars) => {
|
Pokemon: (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 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: 'title', match: filter }
|
//takes an array of scalar types as objects: { typeName: 'String', name: 'String', match: filter }
|
||||||
//must return an array of objects containing the results
|
//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 this is a sub-query of Pokemon (a form), use the parent to narrow the search
|
||||||
if (parent && parent.typeName == 'Author') {
|
if (parent && parent.typeName == 'Pokemon') {
|
||||||
//filter based on parent object
|
//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
|
//if this query has a matched scalar, filter by that match
|
||||||
books = books.filter(b => {
|
filteredPokemon = filteredPokemon.filter(poke => {
|
||||||
return scalars.every(s => {
|
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);
|
const fields = scalars.map(s => s.name);
|
||||||
return books.map(b => {
|
return filteredPokemon.map(p => {
|
||||||
const ret = {};
|
const ret = {};
|
||||||
|
|
||||||
if (fields.includes('title')) {
|
if (fields.includes('name')) {
|
||||||
ret.title = b.title;
|
ret.name = p.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields.includes('published')) {
|
if (fields.includes('height')) {
|
||||||
ret.published = b.published;
|
ret.height = p.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fields.includes('weight')) {
|
||||||
|
ret.weight = p.weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
Author: (parent, scalars) => {
|
Stats: (parent, scalars) => {
|
||||||
let authors = database.authors;
|
if (!parent || parent.typeName != 'Pokemon') {
|
||||||
|
throw 'Stats must be inside a Pokemon query';
|
||||||
//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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//if this query has a matched scalar, filter by that match
|
let filteredPokemon = pokemon.filter(poke => poke.base_stats !== null); //skip unknown pokemon stats
|
||||||
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
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
//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);
|
const fields = scalars.map(s => s.name);
|
||||||
return authors.map(a => {
|
return filteredPokemon.map(p => {
|
||||||
const ret = {};
|
const ret = {};
|
||||||
|
|
||||||
if (fields.includes('name')) {
|
if (fields.includes('hp')) {
|
||||||
ret.name = a.name;
|
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;
|
return ret;
|
||||||
|
|||||||
1
server/pokemon.json
Normal file
1
server/pokemon.json
Normal file
File diff suppressed because one or more lines are too long
@@ -1,14 +1,18 @@
|
|||||||
module.exports = `
|
module.exports = `
|
||||||
scalar Date
|
type Pokemon {
|
||||||
|
String name
|
||||||
type Book {
|
Integer height
|
||||||
String title
|
Integer weight
|
||||||
Author author
|
Stats stats
|
||||||
Date published
|
Pokemon forms
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author {
|
type Stats {
|
||||||
String name
|
Integer hp
|
||||||
Book books
|
Integer attack
|
||||||
|
Integer defense
|
||||||
|
Integer specialAttack
|
||||||
|
Integer specialDefense
|
||||||
|
Integer speed
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -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)
|
{ 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);
|
pos = eatBlock(tokens, pos + 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user