-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get rid of util file dump, seperate into better scoped files
- Loading branch information
Ciaran Schutte
committed
Dec 15, 2024
1 parent
af4a557
commit 876dc54
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}; |