Skip to content

Commit

Permalink
refactor: Stop fetching geoCoordinates on each filter change
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Feb 1, 2022
1 parent 9ba7ad8 commit c7c2038
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 24 deletions.
23 changes: 19 additions & 4 deletions app/charts/map/chart-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
import {
DimensionMetaDataFragment,
useDataCubeObservationsQuery,
useGeoCoordinatesByDimensionIriQuery,
} from "../../graphql/query-hooks";
import { useLocale } from "../../locales/use-locale";
import { GeoCoordinates } from "../../rdf/query-geo-coordinates";
import { QueryFilters } from "../shared/chart-helpers";
import { ChartContainer } from "../shared/containers";
import { MapComponent } from "./map";
Expand Down Expand Up @@ -69,6 +69,21 @@ export const ChartMapVisualization = ({
| Observation[]
| undefined;

const [{ data: fetchedGeoCoordinates }] =
useGeoCoordinatesByDimensionIriQuery({
variables: {
dataCubeIri: dataSetIri,
locale,
dimensionIri: symbolDimensionIri,
},
});

const geoCoordinates =
fetchedGeoCoordinates?.dataCubeByIri?.dimensionByIri?.__typename ===
"GeoCoordinatesDimension"
? fetchedGeoCoordinates.dataCubeByIri.dimensionByIri.geoCoordinates
: null;

const areaLayer: AreaLayer | undefined = useMemo(() => {
const dimension = dimensions?.find((d) => d.iri === areaDimensionIri);

Expand Down Expand Up @@ -99,8 +114,8 @@ export const ChartMapVisualization = ({
const symbolLayer: SymbolLayer | undefined = useMemo(() => {
const dimension = dimensions?.find((d) => d.iri === symbolDimensionIri);

if (isGeoCoordinatesDimension(dimension)) {
const points = (dimension.geoCoordinates as GeoCoordinates[]).map(
if (isGeoCoordinatesDimension(dimension) && geoCoordinates) {
const points = geoCoordinates.map(
(d) =>
({
coordinates: [d.longitude, d.latitude],
Expand All @@ -125,7 +140,7 @@ export const ChartMapVisualization = ({
return { points };
}
}
}, [areaLayer, dimensions, observations, symbolDimensionIri]);
}, [areaLayer, dimensions, observations, symbolDimensionIri, geoCoordinates]);

useEffect(() => {
const loadLakes = async () => {
Expand Down
23 changes: 20 additions & 3 deletions app/graphql/queries/data-cubes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ fragment dimensionMetaData on Dimension {
isKeyDimension
values(filters: $filters)
unit
... on GeoCoordinatesDimension {
geoCoordinates
}
... on GeoShapesDimension {
geoShapes
}
Expand Down Expand Up @@ -141,6 +138,26 @@ query DimensionValues(
}
}

query GeoCoordinatesByDimensionIri(
$dataCubeIri: String!
$dimensionIri: String!
$latest: Boolean
$locale: String!
) {
dataCubeByIri(iri: $dataCubeIri, locale: $locale, latest: $latest) {
dimensionByIri(iri: $dimensionIri) {
... on GeoCoordinatesDimension {
geoCoordinates {
iri
label
latitude
longitude
}
}
}
}
}

query TemporalDimensionValues(
$dataCubeIri: String!
$dimensionIri: String!
Expand Down
45 changes: 39 additions & 6 deletions app/graphql/query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type Scalars = {
Float: number;
DimensionValue: any;
Filters: any;
GeoCoordinates: any;
GeoShapes: any;
Observation: any;
RawObservation: any;
Expand Down Expand Up @@ -111,6 +110,13 @@ export type DimensionValuesArgs = {



export type GeoCoordinates = {
__typename: 'GeoCoordinates';
iri: Scalars['String'];
label: Scalars['String'];
latitude: Scalars['Float'];
longitude: Scalars['Float'];
};

export type GeoCoordinatesDimension = Dimension & {
__typename: 'GeoCoordinatesDimension';
Expand All @@ -120,7 +126,7 @@ export type GeoCoordinatesDimension = Dimension & {
scaleType?: Maybe<Scalars['String']>;
isKeyDimension: Scalars['Boolean'];
values: Array<Scalars['DimensionValue']>;
geoCoordinates: Scalars['GeoCoordinates'];
geoCoordinates?: Maybe<Array<GeoCoordinates>>;
};


Expand Down Expand Up @@ -292,7 +298,7 @@ export type DataCubesQueryVariables = Exact<{

export type DataCubesQuery = { __typename: 'Query', dataCubes: Array<{ __typename: 'DataCubeResult', highlightedTitle?: Maybe<string>, highlightedDescription?: Maybe<string>, dataCube: { __typename: 'DataCube', iri: string, title: string, description?: Maybe<string>, publicationStatus: DataCubePublicationStatus, datePublished?: Maybe<string>, creator?: Maybe<{ __typename: 'DataCubeOrganization', iri: string, label?: Maybe<string> }>, themes: Array<{ __typename: 'DataCubeTheme', iri: string, label?: Maybe<string> }> } }> };

type DimensionMetaData_GeoCoordinatesDimension_Fragment = { __typename: 'GeoCoordinatesDimension', geoCoordinates: any, iri: string, label: string, isKeyDimension: boolean, values: Array<any>, unit?: Maybe<string> };
type DimensionMetaData_GeoCoordinatesDimension_Fragment = { __typename: 'GeoCoordinatesDimension', iri: string, label: string, isKeyDimension: boolean, values: Array<any>, unit?: Maybe<string> };

type DimensionMetaData_GeoShapesDimension_Fragment = { __typename: 'GeoShapesDimension', geoShapes: any, iri: string, label: string, isKeyDimension: boolean, values: Array<any>, unit?: Maybe<string> };

Expand Down Expand Up @@ -416,6 +422,16 @@ export type DimensionValuesQuery = { __typename: 'Query', dataCubeByIri?: Maybe<
& DimensionMetaData_TemporalDimension_Fragment
)> }> };

export type GeoCoordinatesByDimensionIriQueryVariables = Exact<{
dataCubeIri: Scalars['String'];
dimensionIri: Scalars['String'];
latest?: Maybe<Scalars['Boolean']>;
locale: Scalars['String'];
}>;


export type GeoCoordinatesByDimensionIriQuery = { __typename: 'Query', dataCubeByIri?: Maybe<{ __typename: 'DataCube', dimensionByIri?: Maybe<{ __typename: 'GeoCoordinatesDimension', geoCoordinates?: Maybe<Array<{ __typename: 'GeoCoordinates', iri: string, label: string, latitude: number, longitude: number }>> } | { __typename: 'GeoShapesDimension' } | { __typename: 'Measure' } | { __typename: 'NominalDimension' } | { __typename: 'OrdinalDimension' } | { __typename: 'TemporalDimension' }> }> };

export type TemporalDimensionValuesQueryVariables = Exact<{
dataCubeIri: Scalars['String'];
dimensionIri: Scalars['String'];
Expand Down Expand Up @@ -500,9 +516,6 @@ export const DimensionMetaDataFragmentDoc = gql`
isKeyDimension
values(filters: $filters)
unit
... on GeoCoordinatesDimension {
geoCoordinates
}
... on GeoShapesDimension {
geoShapes
}
Expand Down Expand Up @@ -640,6 +653,26 @@ export const DimensionValuesDocument = gql`
export function useDimensionValuesQuery(options: Omit<Urql.UseQueryArgs<DimensionValuesQueryVariables>, 'query'> = {}) {
return Urql.useQuery<DimensionValuesQuery>({ query: DimensionValuesDocument, ...options });
};
export const GeoCoordinatesByDimensionIriDocument = gql`
query GeoCoordinatesByDimensionIri($dataCubeIri: String!, $dimensionIri: String!, $latest: Boolean, $locale: String!) {
dataCubeByIri(iri: $dataCubeIri, locale: $locale, latest: $latest) {
dimensionByIri(iri: $dimensionIri) {
... on GeoCoordinatesDimension {
geoCoordinates {
iri
label
latitude
longitude
}
}
}
}
}
`;

export function useGeoCoordinatesByDimensionIriQuery(options: Omit<Urql.UseQueryArgs<GeoCoordinatesByDimensionIriQueryVariables>, 'query'> = {}) {
return Urql.useQuery<GeoCoordinatesByDimensionIriQuery>({ query: GeoCoordinatesByDimensionIriDocument, ...options });
};
export const TemporalDimensionValuesDocument = gql`
query TemporalDimensionValues($dataCubeIri: String!, $dimensionIri: String!, $locale: String!, $latest: Boolean, $filters: Filters) {
dataCubeByIri(iri: $dataCubeIri, locale: $locale, latest: $latest) {
Expand Down
28 changes: 19 additions & 9 deletions app/graphql/resolver-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type Scalars = {
Float: number;
DimensionValue: DimensionValue;
Filters: Filters;
GeoCoordinates: any;
GeoShapes: any;
Observation: Observation;
RawObservation: RawObservation;
Expand Down Expand Up @@ -116,6 +115,13 @@ export type DimensionValuesArgs = {



export type GeoCoordinates = {
__typename?: 'GeoCoordinates';
iri: Scalars['String'];
label: Scalars['String'];
latitude: Scalars['Float'];
longitude: Scalars['Float'];
};

export type GeoCoordinatesDimension = Dimension & {
__typename?: 'GeoCoordinatesDimension';
Expand All @@ -125,7 +131,7 @@ export type GeoCoordinatesDimension = Dimension & {
scaleType?: Maybe<Scalars['String']>;
isKeyDimension: Scalars['Boolean'];
values: Array<Scalars['DimensionValue']>;
geoCoordinates: Scalars['GeoCoordinates'];
geoCoordinates?: Maybe<Array<GeoCoordinates>>;
};


Expand Down Expand Up @@ -367,7 +373,7 @@ export type ResolversTypes = ResolversObject<{
Boolean: ResolverTypeWrapper<Scalars['Boolean']>;
DimensionValue: ResolverTypeWrapper<Scalars['DimensionValue']>;
Filters: ResolverTypeWrapper<Scalars['Filters']>;
GeoCoordinates: ResolverTypeWrapper<Scalars['GeoCoordinates']>;
GeoCoordinates: ResolverTypeWrapper<GeoCoordinates>;
GeoCoordinatesDimension: ResolverTypeWrapper<ResolvedDimension>;
GeoShapes: ResolverTypeWrapper<Scalars['GeoShapes']>;
GeoShapesDimension: ResolverTypeWrapper<ResolvedDimension>;
Expand Down Expand Up @@ -397,7 +403,7 @@ export type ResolversParentTypes = ResolversObject<{
Boolean: Scalars['Boolean'];
DimensionValue: Scalars['DimensionValue'];
Filters: Scalars['Filters'];
GeoCoordinates: Scalars['GeoCoordinates'];
GeoCoordinates: GeoCoordinates;
GeoCoordinatesDimension: ResolvedDimension;
GeoShapes: Scalars['GeoShapes'];
GeoShapesDimension: ResolvedDimension;
Expand Down Expand Up @@ -476,9 +482,13 @@ export interface FiltersScalarConfig extends GraphQLScalarTypeConfig<ResolversTy
name: 'Filters';
}

export interface GeoCoordinatesScalarConfig extends GraphQLScalarTypeConfig<ResolversTypes['GeoCoordinates'], any> {
name: 'GeoCoordinates';
}
export type GeoCoordinatesResolvers<ContextType = any, ParentType extends ResolversParentTypes['GeoCoordinates'] = ResolversParentTypes['GeoCoordinates']> = ResolversObject<{
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
label?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
latitude?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
longitude?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type GeoCoordinatesDimensionResolvers<ContextType = any, ParentType extends ResolversParentTypes['GeoCoordinatesDimension'] = ResolversParentTypes['GeoCoordinatesDimension']> = ResolversObject<{
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand All @@ -487,7 +497,7 @@ export type GeoCoordinatesDimensionResolvers<ContextType = any, ParentType exten
scaleType?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
isKeyDimension?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
values?: Resolver<Array<ResolversTypes['DimensionValue']>, ParentType, ContextType, RequireFields<GeoCoordinatesDimensionValuesArgs, never>>;
geoCoordinates?: Resolver<ResolversTypes['GeoCoordinates'], ParentType, ContextType>;
geoCoordinates?: Resolver<Maybe<Array<ResolversTypes['GeoCoordinates']>>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

Expand Down Expand Up @@ -582,7 +592,7 @@ export type Resolvers<ContextType = any> = ResolversObject<{
Dimension?: DimensionResolvers<ContextType>;
DimensionValue?: GraphQLScalarType;
Filters?: GraphQLScalarType;
GeoCoordinates?: GraphQLScalarType;
GeoCoordinates?: GeoCoordinatesResolvers<ContextType>;
GeoCoordinatesDimension?: GeoCoordinatesDimensionResolvers<ContextType>;
GeoShapes?: GraphQLScalarType;
GeoShapesDimension?: GeoShapesDimensionResolvers<ContextType>;
Expand Down
10 changes: 8 additions & 2 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ scalar Observation
scalar DimensionValue
scalar RawObservation
scalar Filters
scalar GeoCoordinates
scalar GeoShapes

type ObservationsQuery {
Expand Down Expand Up @@ -54,14 +53,21 @@ interface Dimension {
values(filters: Filters): [DimensionValue!]!
}

type GeoCoordinates {
iri: String!
label: String!
latitude: Float!
longitude: Float!
}

type GeoCoordinatesDimension implements Dimension {
iri: String!
label: String!
unit: String
scaleType: String
isKeyDimension: Boolean!
values(filters: Filters): [DimensionValue!]!
geoCoordinates: GeoCoordinates!
geoCoordinates: [GeoCoordinates!]
}

type GeoShapesDimension implements Dimension {
Expand Down

0 comments on commit c7c2038

Please sign in to comment.