Repository navigation
Reorg connection #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Reorg connection #891
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f743da
change resolver code based on new gql type
a088522
comments and cleanup
e7bc1c9
break up main entry point into two modules, one for querying and one …
b4c7230
rename network config type, remove supported aggs from type
9412fed
type cleanup
f764bd0
only send network queries with original query fields
7d1a46b
move gql health check into module
5046131
cleanup, renaming types, removing redundant code
c11dd43
add type
68ab587
fix merged conflict
f0ddc0c
adds full stop. fixes comment typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,36 @@ | ||
| import axios from 'axios'; | ||
| import urljoin from 'url-join'; | ||
| import { CONNECTION_STATUS } from './resolvers/networkNode'; | ||
| import { ConnectionStatus } from './types/types'; | ||
|
|
||
| /** | ||
| * Check the status of remote connections | ||
| * | ||
| * @param url - Remote connection url | ||
| * @returns A connection status | ||
| */ | ||
| export const gqlHealthCheck = async (url: string): Promise<ConnectionStatus> => { | ||
| /* | ||
| * recommended way to health check gql server is to run the `__typename` query that every server has. | ||
| * very small query with no additional params, so using GET is not a concern for the GQL server. | ||
| * adds recommended pre flight header to make sure Apollo doesn't block request by CSRF protection. | ||
| */ | ||
| const healthCheckQuery = '?query=%7B__typename%7D'; | ||
| const fullUrl = urljoin(url, healthCheckQuery); | ||
|
|
||
| try { | ||
| const pong = await axios.get(fullUrl, { | ||
| headers: { 'apollo-require-preflight': 'true', 'Content-Type': 'application/json' }, | ||
| }); | ||
|
|
||
| // only need to check the server returns successfully (just a health check) | ||
| if (pong.data.data?.__typename) { | ||
| return CONNECTION_STATUS.OK; | ||
| } else { | ||
| throw Error('no data object returned from GQL server __typname query'); | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| return CONNECTION_STATUS.ERROR; | ||
| } | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,10 +4,9 @@ import { DocumentNode } from 'graphql'; | |
| import { resolveAggregations } from '../aggregations'; | ||
| import { fetchGql } from '../gql'; | ||
| import { failure, isSuccess, success } from '../httpResponses'; | ||
| import { supportedAggregationQueries } from '../queries'; | ||
| import { NetworkAggregation, NetworkAggregationConfig } from '../types'; | ||
| import { NetworkAggregation } from '../types/types'; | ||
| import { ASTtoString } from '../util'; | ||
| import { CONNECTION_STATUS, RemoteConnection } from './remoteConnections'; | ||
| import { CONNECTION_STATUS, NetworkNode } from './networkNode'; | ||
|
|
||
| /** | ||
| * Query remote connections and handle network responses | ||
|
|
@@ -66,12 +65,12 @@ export const aggregationPipeline = async ( | |
| return { ...accumulator, [field]: emptyAggregation }; | ||
| }, {}); | ||
|
|
||
| const nodeInfo: any = []; | ||
| const nodeInfo: NetworkNode[] = []; | ||
|
|
||
| const aggregationResultPromises = queries.map< | ||
| Promise<{ | ||
| aggregations: any; | ||
| remoteConnection: RemoteConnection; | ||
| remoteConnection: NetworkNode; | ||
| }> | ||
| >(async (query) => { | ||
| const name = query.url; // TODO: use readable name not url | ||
|
|
@@ -111,54 +110,58 @@ type NetworkQuery = { | |
| }; | ||
|
|
||
| /** | ||
| * Find requested field in remote connection supported fields | ||
| * all nodes may not have all fields | ||
| * Converts info field object into string | ||
| * | ||
| * @param config | ||
| * @param fieldName | ||
| * @returns | ||
| */ | ||
| const findMatchedAggregationField = (config: NetworkAggregationConfig, fieldName: string) => { | ||
| return config.supportedAggregations.find((agg) => agg.name === fieldName); | ||
| }; | ||
|
|
||
| /** | ||
| * construct gql string { [fieldName] { [Aggregation query fields] } } | ||
| * @param requestedFields | ||
| * | ||
| * @param requestedAggregations | ||
| * @example | ||
| * ### Input | ||
| * ``` | ||
| * { donors: { | ||
| * buckets: { | ||
| * bucket_count: {}, | ||
| * } | ||
| * }} | ||
| * ``` | ||
| * | ||
| * ### Output | ||
| * ``` | ||
| * ` | ||
| * { donors { | ||
| * buckets { | ||
| * bucket_count | ||
| * } | ||
| * }} | ||
| * ` | ||
| * ``` | ||
| */ | ||
| const createGqlFieldsString = (config: NetworkAggregationConfig, requestedAggregations: any[]) => { | ||
| return requestedAggregations.reduce((gqlString, fieldName) => { | ||
| const matchedAggregationField = findMatchedAggregationField(config, fieldName); | ||
| if (matchedAggregationField) { | ||
| const { name, type } = matchedAggregationField; | ||
| // get gql query string for supported aggregation | ||
| // TODO: only query requested fields + bucket_count if nodes is requested | ||
| const aggregationFieldQueryString = supportedAggregationQueries.get(type); | ||
| return gqlString + `${name}${aggregationFieldQueryString}`; | ||
| } | ||
| return gqlString; | ||
| }, ''); | ||
| const createGqlFieldsString = (requestedFields: {}, documentName: string) => { | ||
| const gqlFieldsString = JSON.stringify(requestedFields) | ||
| .replaceAll('"', '') | ||
| .replaceAll(':', '') | ||
| .replaceAll('{}', '') | ||
| .replaceAll(',', ' '); | ||
|
|
||
| // add top level field for query and format with correct brackets | ||
| const gqlString = `{${documentName} ${gqlFieldsString}}`; | ||
|
|
||
| return gqlString; | ||
|
Comment on lines
+138
to
+148
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parse out request object to gql string |
||
| }; | ||
|
|
||
| /** | ||
| * Create queries for remote nodes based on requested fields | ||
| * | ||
| * @param configs | ||
| * @param requestedAggregations | ||
| * @param requestedFields | ||
| * @returns | ||
| */ | ||
| export const createNetworkQueries = ( | ||
| configs: NetworkAggregationConfig[], | ||
| requestedAggregations: string[], | ||
| configs: NetworkConfig[], | ||
| requestedFields: {}, | ||
| ): NetworkQuery[] => { | ||
| // TODO: what if there are no matched findMatchedAggregationField | ||
| const queries = configs | ||
| .map((config) => { | ||
| const gqlStringFields = createGqlFieldsString(config, requestedAggregations); | ||
|
|
||
| // add top level field for query and format with correct brackets | ||
| const gqlString = `{${config.documentName} { ${gqlStringFields} }}`; | ||
| const gqlString = createGqlFieldsString(requestedFields, config.documentName); | ||
|
|
||
| /* | ||
| * convert string to AST object to use as query | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forwarding relevant part of query to network nodes, instead of splicing in this static query for each field
resulting queries are smaller and only return requested fields (half the point of GQL)