Skip to content

Commit

Permalink
feat(api): support filtering items by item class (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak authored Dec 3, 2023
1 parent 4d246e0 commit d456e2a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/spelunker-api/src/lib/core/Query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Query {
return this;
}

filter(filters) {
if (filters) {
this.entity.filter(this, filters);
}
return this;
}

slice() {
notImplemented(this, 'slice');
}
Expand Down
6 changes: 6 additions & 0 deletions packages/spelunker-api/src/lib/entities/Item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class Item extends DatabaseEntity {
query.where('name', 'LIKE', `%${searchQuery}%`);
}

static filter(query, filters) {
if (filters?.itemClassIds?.length > 0) {
query.andWhere('class', 'IN', filters.itemClassIds);
}
}

get id() {
return this.data.entry;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/spelunker-api/src/lib/graph/root.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {
factions: ({ searchQuery }) => Faction.query.search(searchQuery),
faction: ({ id }) => Faction.find(id),

items: ({ searchQuery }) => Item.query.search(searchQuery),
items: ({ searchQuery, ...filters }) => Item.query.search(searchQuery).filter(filters),
item: ({ id }) => Item.find(id),

itemSets: ({ searchQuery }) => ItemSet.query.search(searchQuery),
Expand Down
8 changes: 6 additions & 2 deletions packages/spelunker-api/src/lib/graph/schema/QueryType.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
Expand Down Expand Up @@ -29,10 +30,11 @@ const finderFor = (type, idType = GraphQLInt) => ({
},
});

const searchableCollectionFor = (type) => (
const searchableCollectionFor = (type, filters = {}) => (
CollectionType.definitionFor(type, {
args: {
searchQuery: { type: GraphQLString },
...filters,
},
})
);
Expand All @@ -55,7 +57,9 @@ export default new GraphQLObjectType({
factions: searchableCollectionFor(FactionType),
faction: finderFor(FactionType),

items: searchableCollectionFor(ItemType),
items: searchableCollectionFor(ItemType, {
itemClassIds: { type: new GraphQLList(GraphQLInt) },
}),
item: finderFor(ItemType),

itemSets: searchableCollectionFor(ItemSetType),
Expand Down

0 comments on commit d456e2a

Please sign in to comment.