Skip to content

Commit

Permalink
perf: Use scalar for nested objects (GQL SearchCubes)
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Oct 25, 2023
1 parent 4ed5fdf commit 3d23a44
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 29 deletions.
11 changes: 11 additions & 0 deletions app/domain/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ export type GeoData = {
symbolLayer: SymbolLayer | undefined;
};

// Extracted for performance reasons.
export type SearchCubeCreator = {
iri: string;
label: string;
};

export type SearchCubeThemes = {
iri: string;
label: string;
}[];

const xmlSchema = "http://www.w3.org/2001/XMLSchema#";
export const parseRDFLiteral = <T = ObservationValue>(value: Literal): T => {
const v = value.value;
Expand Down
10 changes: 2 additions & 8 deletions app/graphql/queries/data-cubes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,8 @@ query SearchCubes(
description
publicationStatus
datePublished
creator {
iri
label
}
themes {
iri
label
}
creator
themes
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions app/graphql/query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { DimensionValue } from '../domain/data';
import { QueryFilters } from '../configurator';
import { Observation } from '../domain/data';
import { RawObservation } from '../domain/data';
import { SearchCubeCreator } from '../domain/data';
import { SearchCubeThemes } from '../domain/data';
import gql from 'graphql-tag';
import * as Urql from 'urql';
export type Maybe<T> = T | null;
Expand All @@ -22,6 +24,8 @@ export type Scalars = {
GeoShapes: any;
Observation: Observation;
RawObservation: RawObservation;
SearchCubeCreator: SearchCubeCreator;
SearchCubeThemes: SearchCubeThemes;
ValueIdentifier: any;
ValuePosition: any;
};
Expand Down Expand Up @@ -443,12 +447,13 @@ export type SearchCube = {
iri: Scalars['String'];
title: Scalars['String'];
description?: Maybe<Scalars['String']>;
creator?: Maybe<DataCubeOrganization>;
creator?: Maybe<Scalars['SearchCubeCreator']>;
publicationStatus: DataCubePublicationStatus;
datePublished?: Maybe<Scalars['String']>;
themes: Array<DataCubeTheme>;
themes: Scalars['SearchCubeThemes'];
};


export type SearchCubeFilter = {
type: Scalars['String'];
value: Scalars['String'];
Expand All @@ -468,6 +473,7 @@ export enum SearchCubeResultOrder {
CreatedDesc = 'CREATED_DESC'
}


export type StandardErrorDimension = Dimension & {
__typename: 'StandardErrorDimension';
iri: Scalars['String'];
Expand Down Expand Up @@ -832,7 +838,7 @@ export type SearchCubesQueryVariables = Exact<{
}>;


export type SearchCubesQuery = { __typename: 'Query', searchCubes: Array<{ __typename: 'SearchCubeResult', highlightedTitle?: Maybe<string>, highlightedDescription?: Maybe<string>, cube: { __typename: 'SearchCube', 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> }> } }> };
export type SearchCubesQuery = { __typename: 'Query', searchCubes: Array<{ __typename: 'SearchCubeResult', highlightedTitle?: Maybe<string>, highlightedDescription?: Maybe<string>, cube: { __typename: 'SearchCube', iri: string, title: string, description?: Maybe<string>, publicationStatus: DataCubePublicationStatus, datePublished?: Maybe<string>, creator?: Maybe<SearchCubeCreator>, themes: SearchCubeThemes } }> };

export type DataCubePreviewQueryVariables = Exact<{
iri: Scalars['String'];
Expand Down Expand Up @@ -1255,14 +1261,8 @@ export const SearchCubesDocument = gql`
description
publicationStatus
datePublished
creator {
iri
label
}
themes {
iri
label
}
creator
themes
}
}
}
Expand Down
28 changes: 24 additions & 4 deletions app/graphql/resolver-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { DimensionValue } from '../domain/data';
import { Filters } from '../configurator';
import { Observation } from '../domain/data';
import { RawObservation } from '../domain/data';
import { SearchCubeCreator } from '../domain/data';
import { SearchCubeThemes } from '../domain/data';
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
import { ResolvedDataCube, ResolvedObservationsQuery, ResolvedMeasure, ResolvedDimension } from './shared-types';
import { VisualizeGraphQLContext } from './context';
Expand All @@ -23,6 +25,8 @@ export type Scalars = {
GeoShapes: any;
Observation: Observation;
RawObservation: RawObservation;
SearchCubeCreator: SearchCubeCreator;
SearchCubeThemes: SearchCubeThemes;
ValueIdentifier: any;
ValuePosition: any;
};
Expand Down Expand Up @@ -444,12 +448,13 @@ export type SearchCube = {
iri: Scalars['String'];
title: Scalars['String'];
description?: Maybe<Scalars['String']>;
creator?: Maybe<DataCubeOrganization>;
creator?: Maybe<Scalars['SearchCubeCreator']>;
publicationStatus: DataCubePublicationStatus;
datePublished?: Maybe<Scalars['String']>;
themes: Array<DataCubeTheme>;
themes: Scalars['SearchCubeThemes'];
};


export type SearchCubeFilter = {
type: Scalars['String'];
value: Scalars['String'];
Expand All @@ -469,6 +474,7 @@ export enum SearchCubeResultOrder {
CreatedDesc = 'CREATED_DESC'
}


export type StandardErrorDimension = Dimension & {
__typename?: 'StandardErrorDimension';
iri: Scalars['String'];
Expand Down Expand Up @@ -669,9 +675,11 @@ export type ResolversTypes = ResolversObject<{
RelatedDimension: ResolverTypeWrapper<RelatedDimension>;
ScaleType: ScaleType;
SearchCube: ResolverTypeWrapper<SearchCube>;
SearchCubeCreator: ResolverTypeWrapper<Scalars['SearchCubeCreator']>;
SearchCubeFilter: SearchCubeFilter;
SearchCubeResult: ResolverTypeWrapper<SearchCubeResult>;
SearchCubeResultOrder: SearchCubeResultOrder;
SearchCubeThemes: ResolverTypeWrapper<Scalars['SearchCubeThemes']>;
StandardErrorDimension: ResolverTypeWrapper<ResolvedDimension>;
TemporalDimension: ResolverTypeWrapper<ResolvedDimension>;
TemporalOrdinalDimension: ResolverTypeWrapper<ResolvedDimension>;
Expand Down Expand Up @@ -710,8 +718,10 @@ export type ResolversParentTypes = ResolversObject<{
RawObservation: Scalars['RawObservation'];
RelatedDimension: RelatedDimension;
SearchCube: SearchCube;
SearchCubeCreator: Scalars['SearchCubeCreator'];
SearchCubeFilter: SearchCubeFilter;
SearchCubeResult: SearchCubeResult;
SearchCubeThemes: Scalars['SearchCubeThemes'];
StandardErrorDimension: ResolvedDimension;
TemporalDimension: ResolvedDimension;
TemporalOrdinalDimension: ResolvedDimension;
Expand Down Expand Up @@ -956,13 +966,17 @@ export type SearchCubeResolvers<ContextType = VisualizeGraphQLContext, ParentTyp
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
title?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
description?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
creator?: Resolver<Maybe<ResolversTypes['DataCubeOrganization']>, ParentType, ContextType>;
creator?: Resolver<Maybe<ResolversTypes['SearchCubeCreator']>, ParentType, ContextType>;
publicationStatus?: Resolver<ResolversTypes['DataCubePublicationStatus'], ParentType, ContextType>;
datePublished?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
themes?: Resolver<Array<ResolversTypes['DataCubeTheme']>, ParentType, ContextType>;
themes?: Resolver<ResolversTypes['SearchCubeThemes'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export interface SearchCubeCreatorScalarConfig extends GraphQLScalarTypeConfig<ResolversTypes['SearchCubeCreator'], any> {
name: 'SearchCubeCreator';
}

export type SearchCubeResultResolvers<ContextType = VisualizeGraphQLContext, ParentType extends ResolversParentTypes['SearchCubeResult'] = ResolversParentTypes['SearchCubeResult']> = ResolversObject<{
score?: Resolver<Maybe<ResolversTypes['Float']>, ParentType, ContextType>;
cube?: Resolver<ResolversTypes['SearchCube'], ParentType, ContextType>;
Expand All @@ -971,6 +985,10 @@ export type SearchCubeResultResolvers<ContextType = VisualizeGraphQLContext, Par
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export interface SearchCubeThemesScalarConfig extends GraphQLScalarTypeConfig<ResolversTypes['SearchCubeThemes'], any> {
name: 'SearchCubeThemes';
}

export type StandardErrorDimensionResolvers<ContextType = VisualizeGraphQLContext, ParentType extends ResolversParentTypes['StandardErrorDimension'] = ResolversParentTypes['StandardErrorDimension']> = ResolversObject<{
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
label?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -1054,7 +1072,9 @@ export type Resolvers<ContextType = VisualizeGraphQLContext> = ResolversObject<{
RawObservation?: GraphQLScalarType;
RelatedDimension?: RelatedDimensionResolvers<ContextType>;
SearchCube?: SearchCubeResolvers<ContextType>;
SearchCubeCreator?: GraphQLScalarType;
SearchCubeResult?: SearchCubeResultResolvers<ContextType>;
SearchCubeThemes?: GraphQLScalarType;
StandardErrorDimension?: StandardErrorDimensionResolvers<ContextType>;
TemporalDimension?: TemporalDimensionResolvers<ContextType>;
TemporalOrdinalDimension?: TemporalOrdinalDimensionResolvers<ContextType>;
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const DataCube: DataCubeResolvers = {
description: ({ data: { description } }) => description ?? null,
dateModified: ({ data: { dateModified } }) => dateModified ?? null,
datePublished: ({ data: { datePublished } }) => datePublished ?? null,
themes: ({ data: { themes } }) => themes || [],
themes: ({ data: { themes } }) => themes ?? [],
creator: ({ data: { creator } }) => creator ?? null,
dimensions: async (parent, args, context, info) => {
const source = getSource(args.sourceType);
Expand Down
7 changes: 5 additions & 2 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,17 @@ type OrdinalMeasure implements Dimension {
hierarchy(sourceType: String!, sourceUrl: String!): [HierarchyValue!]
}

scalar SearchCubeCreator
scalar SearchCubeThemes

type SearchCube {
iri: String!
title: String!
description: String
creator: DataCubeOrganization
creator: SearchCubeCreator
publicationStatus: DataCubePublicationStatus!
datePublished: String
themes: [DataCubeTheme!]!
themes: SearchCubeThemes!
}

type SearchCubeResult {
Expand Down
9 changes: 6 additions & 3 deletions app/rdf/query-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Literal, NamedNode } from "rdf-js";
import StreamClient from "sparql-http-client";
import ParsingClient from "sparql-http-client/ParsingClient";

import { SearchCubeCreator, SearchCubeThemes } from "@/domain/data";
import { truthy } from "@/domain/types";
import { RequestQueryMeta } from "@/graphql/query-meta";
import {
Expand Down Expand Up @@ -307,10 +308,12 @@ export const searchCubes = async ({
}

if (!parsedCube.creator && cube.creator) {
parsedCube.creator = {
const creator: SearchCubeCreator = {
iri: cube.creator,
label: cube.creatorLabel,
};

parsedCube.creator = creator;
}

if (!parsedCube.datePublished) {
Expand All @@ -326,9 +329,9 @@ export const searchCubes = async ({
}

if (cube.theme || cube.themeName) {
parsedCube.themes.push({
(parsedCube.themes as SearchCubeThemes).push({
iri: cube.theme,
name: cube.themeName,
label: cube.themeName,
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ generates:
RawObservation: "../domain/data#RawObservation"
Filters: "../configurator#QueryFilters"
GeoShape: "../domain/data#GeoShape"
SearchCubeCreator: "../domain/data#SearchCubeCreator"
SearchCubeThemes: "../domain/data#SearchCubeThemes"
app/graphql/resolver-types.ts:
plugins:
- "typescript"
Expand All @@ -35,6 +37,8 @@ generates:
RawObservation: "../domain/data#RawObservation"
Filters: "../configurator#Filters"
GeoShape: "../domain/data#GeoShape"
SearchCubeCreator: "../domain/data#SearchCubeCreator"
SearchCubeThemes: "../domain/data#SearchCubeThemes"
mappers:
DataCube: "./shared-types#ResolvedDataCube"
ObservationsQuery: "./shared-types#ResolvedObservationsQuery"
Expand Down

0 comments on commit 3d23a44

Please sign in to comment.