Skip to content

Commit

Permalink
get rid of util file dump, seperate into better scoped files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciaran Schutte committed Dec 15, 2024
1 parent af4a557 commit 876dc54
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
42 changes: 42 additions & 0 deletions modules/server/src/network/utils/gql.ts
Original file line number Diff line number Diff line change
@@ -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<string, {}>;
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;
};
11 changes: 11 additions & 0 deletions modules/server/src/network/utils/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Type guard to filter fulfilled Promises
*/
export const fulfilledPromiseFilter = <Result>(result: unknown): result is Result => {
return (
typeof result === 'object' &&
result !== null &&
'status' in result &&
result.status === 'fulfilled'
);
};

0 comments on commit 876dc54

Please sign in to comment.