From 876dc54a1721ef7b8acc02f21fa11c10c24a6065 Mon Sep 17 00:00:00 2001 From: Ciaran Schutte Date: Sun, 15 Dec 2024 04:32:07 -0500 Subject: [PATCH] get rid of util file dump, seperate into better scoped files --- modules/server/src/network/utils/gql.ts | 42 +++++++++++++++++++++ modules/server/src/network/utils/promise.ts | 11 ++++++ 2 files changed, 53 insertions(+) create mode 100644 modules/server/src/network/utils/gql.ts create mode 100644 modules/server/src/network/utils/promise.ts diff --git a/modules/server/src/network/utils/gql.ts b/modules/server/src/network/utils/gql.ts new file mode 100644 index 000000000..9413d06ed --- /dev/null +++ b/modules/server/src/network/utils/gql.ts @@ -0,0 +1,42 @@ +import { DocumentNode, GraphQLResolveInfo, print } from 'graphql'; +import graphqlFields from 'graphql-fields'; + +/* + * GraphQL AST => String + */ +export const ASTtoString = (ast: DocumentNode) => { + return print(ast); +}; + +/** + * Turns GraphQLResolveInfo into a map of the requested fields + * + * @param info GQL request info object + * @example + * ``` + * { + * analysis__analysis_state: { + * bucket_count: {}, + * buckets: { + * key: {}, + * doc_count: {} + * }, + * __typename: {} + * } + * ``` + */ +export type RequestedFieldsMap = Record; +export const resolveInfoToMap = (info: GraphQLResolveInfo, key: string): RequestedFieldsMap => { + const requestedFields = graphqlFields(info); + const aggregations = requestedFields[key]; + + // ensure __typename will be queried to network nodes + const aggs = Object.keys(aggregations).reduce((aggs, key) => { + const element = aggregations[key]; + if (!element.hasOwnProperty('__typename')) { + element['__typename'] = {}; + } + return { ...aggs, [key]: element }; + }, {}); + return aggs; +}; diff --git a/modules/server/src/network/utils/promise.ts b/modules/server/src/network/utils/promise.ts new file mode 100644 index 000000000..1d442ecb0 --- /dev/null +++ b/modules/server/src/network/utils/promise.ts @@ -0,0 +1,11 @@ +/** + * Type guard to filter fulfilled Promises + */ +export const fulfilledPromiseFilter = (result: unknown): result is Result => { + return ( + typeof result === 'object' && + result !== null && + 'status' in result && + result.status === 'fulfilled' + ); +};