Merge pull request 'Filter utils to create filter input for query' (#17) from filter-query into master
Reviewed-on: billnerds/rn-playground#17
This commit is contained in:
commit
92258f4925
31
graphql/filter.ts
Normal file
31
graphql/filter.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
type BaseFilter = {
|
||||||
|
createVars: () => { [key: string]: any };
|
||||||
|
};
|
||||||
|
|
||||||
|
function createBaseFilter(key: string, content: any): BaseFilter {
|
||||||
|
return {
|
||||||
|
createVars: () => ({ [key]: content }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createAndFilter(filters: Array<BaseFilter>): BaseFilter {
|
||||||
|
const filterVars = filters.map((filter) => filter.createVars());
|
||||||
|
return createBaseFilter("andFilters", { filters: filterVars });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createCategoryFilter(
|
||||||
|
feature: string,
|
||||||
|
value: string,
|
||||||
|
): BaseFilter {
|
||||||
|
const content = { [feature]: { value } };
|
||||||
|
return createBaseFilter(feature, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createRangeFilter(
|
||||||
|
feature: string,
|
||||||
|
greaterThanEqualTo: number,
|
||||||
|
lessThan: number,
|
||||||
|
): BaseFilter {
|
||||||
|
const content = { greaterThanEqualTo, lessThan };
|
||||||
|
return createBaseFilter(feature, content);
|
||||||
|
}
|
@ -2,6 +2,7 @@ import { gql } from "@apollo/client";
|
|||||||
|
|
||||||
export const GET_SHOTS = gql`
|
export const GET_SHOTS = gql`
|
||||||
query GetShots(
|
query GetShots(
|
||||||
|
$filterInput: FilterInput
|
||||||
$includeCueObjectDistance: Boolean! = false
|
$includeCueObjectDistance: Boolean! = false
|
||||||
$includeTargetPocketDistance: Boolean! = false
|
$includeTargetPocketDistance: Boolean! = false
|
||||||
$includeCueObjectAngle: Boolean! = false
|
$includeCueObjectAngle: Boolean! = false
|
||||||
@ -10,7 +11,7 @@ export const GET_SHOTS = gql`
|
|||||||
$includeShotDirection: Boolean! = false
|
$includeShotDirection: Boolean! = false
|
||||||
$includeDidMake: Boolean! = false
|
$includeDidMake: Boolean! = false
|
||||||
) {
|
) {
|
||||||
getShots {
|
getShots(filterInput: $filterInput) {
|
||||||
id
|
id
|
||||||
videoId
|
videoId
|
||||||
startFrame
|
startFrame
|
||||||
|
28
test/graphql/filter.test.ts
Normal file
28
test/graphql/filter.test.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { createAndFilter, createRangeFilter } from "../../graphql/filter";
|
||||||
|
|
||||||
|
describe("create filterInput", () => {
|
||||||
|
it("should create a filter input with the correct structure", () => {
|
||||||
|
const feature = "cueObjectAngle";
|
||||||
|
const greaterThanEqualTo = 10;
|
||||||
|
const lessThan = 20;
|
||||||
|
|
||||||
|
const rangeFilter = createRangeFilter(
|
||||||
|
feature,
|
||||||
|
greaterThanEqualTo,
|
||||||
|
lessThan,
|
||||||
|
);
|
||||||
|
const andFilterInput = createAndFilter([rangeFilter]);
|
||||||
|
expect(andFilterInput.createVars()).toEqual({
|
||||||
|
andFilters: {
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
[feature]: {
|
||||||
|
greaterThanEqualTo,
|
||||||
|
lessThan,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user