Skip to content

Commit

Permalink
fix: Join cube filters
Browse files Browse the repository at this point in the history
...to properly translate joinBy dimension to SPARQL filters
  • Loading branch information
bprusinowski committed Dec 1, 2023
1 parent 5cd8d9f commit 9a3cd9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
41 changes: 41 additions & 0 deletions app/graphql/hook-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ascending } from "d3";
import uniqBy from "lodash/uniqBy";
import { OperationResult } from "urql";

import { Filters } from "@/configurator";
import { FIELD_VALUE_NONE } from "@/configurator/constants";
import { Dimension, Observation, ObservationValue } from "@/domain/data";
import {
DataCubeComponentsQuery,
Expand All @@ -11,6 +13,45 @@ import {
Exact,
} from "@/graphql/query-hooks";

/** Use in both components and observations query, to omit literal `joinBy` filter,
* and use the value of the `joinBy` property of the cube filter instead.
*
* This is required to be able to properly filter the LINDAS cubes, as we use
* `joinBy` dimension on the client side.
*/
export const getJoinedCubeFilters = <
T extends { filters?: Filters | null; joinBy?: string | null }
>(
cubeFilters: T[]
) => {
const joinedCubeFilters: T[] = [];

for (const cubeFilter of cubeFilters) {
if (cubeFilter.filters && cubeFilter.joinBy) {
const { joinBy: joinByFilter, ...filters } = cubeFilter.filters;

if (
joinByFilter &&
joinByFilter.type === "single" &&
joinByFilter.value !== FIELD_VALUE_NONE
) {
joinedCubeFilters.push({
...cubeFilter,
filters: {
[cubeFilter.joinBy]: { type: "single", value: joinByFilter.value },
...filters,
},
});

continue;
}
}
joinedCubeFilters.push(cubeFilter);
}

return joinedCubeFilters;
};

/** Use to exclude joinBy dimensions when fetching dimensions, and create
* a new joinBy dimension with values from all joinBy dimensions.
*/
Expand Down
14 changes: 11 additions & 3 deletions app/graphql/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
} from "@/domain/data";

import { client } from "./client";
import { joinDimensions, mergeObservations } from "./hook-utils";
import {
getJoinedCubeFilters,
joinDimensions,
mergeObservations,
} from "./hook-utils";
import {
DataCubeComponentFilter,
DataCubeComponentsDocument,
Expand Down Expand Up @@ -152,8 +156,10 @@ export const executeDataCubesComponentsQuery = async (
);
}

const joinedCubeFilters = getJoinedCubeFilters(cubeFilters);

const queries = await Promise.all(
cubeFilters.map((cubeFilter) => {
joinedCubeFilters.map((cubeFilter) => {
const cubeVariables = { locale, sourceType, sourceUrl, cubeFilter };
const cached = client.readQuery<
DataCubeComponentsQuery,
Expand Down Expand Up @@ -240,8 +246,10 @@ export const executeDataCubesObservationsQuery = async (
);
}

const joinedCubeFilters = getJoinedCubeFilters(cubeFilters);

const queries = await Promise.all(
cubeFilters.map((cubeFilter) => {
joinedCubeFilters.map((cubeFilter) => {
const cubeVariables = { locale, sourceType, sourceUrl, cubeFilter };
const cached = client.readQuery<
DataCubeObservationsQuery,
Expand Down

0 comments on commit 9a3cd9b

Please sign in to comment.