Skip to content

Commit

Permalink
tighten resolver types
Browse files Browse the repository at this point in the history
tighten config resolver
  • Loading branch information
Ciaran Schutte committed Dec 15, 2024
1 parent 876dc54 commit 7828313
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
15 changes: 13 additions & 2 deletions modules/server/src/gqlServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ export type Context = {
esClient: Client;
};

export type Resolver<Root, QueryArgs, ReturnValue> = (
export type ResolverOutput<T> = T | Promise<T>;

/**
* GQL resolver
*
* @param root The parent object of a query.
* @param args The query arguments.
* @param context The context passed to apollo-server for queries.
* @param info The GraphQL info object.
* @return Returns resolved value;
*/
export type Resolver<Root = {}, QueryArgs = Object, ReturnValue = undefined> = (
root: Root,
args: QueryArgs,
context: Context,
info: GraphQLResolveInfo,
) => ReturnValue;
) => ResolverOutput<ReturnValue> | ResolverOutput<ReturnValue>;
4 changes: 2 additions & 2 deletions modules/server/src/mapping/masking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export type Relation = keyof typeof Relation;
* It is calculated by adding +1 for values under threshold or adding bucket.doc_count amount
* for values greater than or equal to
*
* @param aggregation an aggregation with the most buckets which has data masking applied
* @returns hits total value
* @param aggregation An aggregation with the most buckets which has data masking applied
* @returns Hits total value
*/
const calculateHitsFromAggregation = ({
aggregation,
Expand Down
4 changes: 2 additions & 2 deletions modules/server/src/mapping/resolveAggregations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import esSearch from './utils/esSearch';
/*
* GQL query types
*/
type GQLAggregationQueryFilters = {
export type GQLAggregationQueryFilters = {
filters: any;
aggregations_filter_themselves: boolean;
include_missing: boolean;
Expand All @@ -31,7 +31,7 @@ export type Aggregation = {
buckets: Bucket[];
};

type Aggregations = Record<string, Aggregation>;
export type Aggregations = Record<string, Aggregation>;

const resolveAggregations = ({
type,
Expand Down
70 changes: 42 additions & 28 deletions modules/server/src/mapping/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { ConfigProperties, ExtendedConfigsInterface } from '@/config/types';
import { Context } from '@/gqlServer';
import { GraphQLResolveInfo } from 'graphql';
import { get } from 'lodash';

import { ConfigProperties, ExtendedConfigsInterface } from '@/config/types';
import { Context, Resolver } from '@/gqlServer';
import { CreateConnectionResolversArgs } from './createConnectionResolvers';
import { applyAggregationMasking } from './masking';
import resolveAggregations, { Aggregation, aggregationsToGraphql } from './resolveAggregations';
import resolveAggregations, {
Aggregation,
Aggregations,
aggregationsToGraphql,
GQLAggregationQueryFilters,
} from './resolveAggregations';
import resolveHits from './resolveHits';
import { Hits, Root } from './types';
import { HitsQuery } from './types';

type Root = Record<string, any>;

/**
* Resolve hits from aggregations
Expand All @@ -15,20 +23,24 @@ import { Hits, Root } from './types';
* @param aggregationsQuery - resolver ES query code for aggregations
* @returns Returns a total count that is less than or equal to the actual total hits in the query.
*/
const resolveHitsFromAggs =
(
aggregationsQuery: (
obj: Root,
args: {
filters?: object;
include_missing?: boolean;
aggregations_filter_themselves?: boolean;
},
context: Context,
info: GraphQLResolveInfo,
) => Record<string, Aggregation>,
) =>
async (obj: Root, args: Hits, context: Context, info: GraphQLResolveInfo) => {
const resolveHitsFromAggs = (
aggregationsQuery: (
obj: Root,
args: {
filters?: object;
include_missing?: boolean;
aggregations_filter_themselves?: boolean;
},
context: Context,
info: GraphQLResolveInfo,
) => Record<string, Aggregation>,
) => {
const resolver: Resolver<Root, HitsQuery, Promise<{ total: number }>> = async (
obj,
args,
context,
info,
) => {
/*
* Get "aggregations" field from full query if found
* Popular gql parsing libs parse the "info" property which may not include full query based on schema
Expand Down Expand Up @@ -57,6 +69,8 @@ const resolveHitsFromAggs =
return { total: 0 };
}
};
return resolver;
};

export const createResolvers = ({
createStateResolvers,
Expand All @@ -66,7 +80,11 @@ export const createResolvers = ({
enableDocumentHits,
}: Omit<CreateConnectionResolversArgs, 'enableAdmin'>) => {
// configs
const configs = async (parentObj: Root, { fieldNames }: { fieldNames: string[] }) => {
// TODO: tighten return value type
const configs: Resolver<Root, { fieldNames: string[] }, any> = async (
parentObj,
{ fieldNames },
) => {
return {
downloads: type.config?.[ConfigProperties.DOWNLOADS],
extended: fieldNames
Expand All @@ -86,15 +104,11 @@ export const createResolvers = ({
// @ts-expect-error - tighten types higher up, "type"
const aggregationsQuery = resolveAggregations({ type, getServerSideFilter });

const aggregations = async (
obj: Root,
args: {
filters: object;
include_missing: boolean;
aggregations_filter_themselves: boolean;
},
context: Context,
info: GraphQLResolveInfo,
const aggregations: Resolver<Root, GQLAggregationQueryFilters, Promise<Aggregations>> = async (
obj,
args,
context,
info,
) => {
const aggregations = await aggregationsQuery(obj, args, context, info);
if (enableDocumentHits) {
Expand Down
18 changes: 1 addition & 17 deletions modules/server/src/mapping/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
import { Client } from '@elastic/elasticsearch';
import { Relation } from './masking';

export type Bucket = {
doc_count: number;
key: string;
relation: Relation;
};

export type Aggregation = {
bucket_count: number;
buckets: Bucket[];
};

export type Root = Record<string, unknown>;

enum Missing {
first,
last,
Expand All @@ -38,7 +22,7 @@ export type Sort = {
missing: Missing;
};

export type Hits = {
export type HitsQuery = {
score: string;
offset: number;
sort: [Sort];
Expand Down

0 comments on commit 7828313

Please sign in to comment.