Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions modules/server/src/network/health.ts
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;
}
};
148 changes: 10 additions & 138 deletions modules/server/src/network/index.ts
Original file line number Diff line number Diff line change
@@ -1,160 +1,32 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { SUPPORTED_AGGREGATIONS_LIST } from './common';
import { NetworkAggregationError } from './errors';
import { fetchGql } from './gql';
import { gqlAggregationTypeQuery, GQLFieldType, GQLTypeQueryResponse } from './queries';
import { createResolvers } from './resolvers';
import { getAllFieldTypes } from './setup/fields';
import { fetchRemoteSchemas } from './setup/query';
import { createTypeDefs } from './typeDefs';
import {
NetworkAggregationConfig,
NetworkAggregationConfigInput,
NetworkFieldType,
SupportedNetworkFieldType,
} from './types';
import { fulfilledPromiseFilter, getAllTypes } from './util';

/**
* GQL query remote connection with __type query to retrieve list of types
*
* @param config - network config from env
* @returns a promise containing network config and the gql query result
*
* @throws Fetch failed error
*
* @throws JSON parse error
*
* @throws Unexpected data error
*/
const fetchRemoteSchema = async (
config: NetworkAggregationConfigInput,
): Promise<GQLTypeQueryResponse | undefined> => {
const { graphqlUrl, documentType, timeout } = config;
try {
const response = await fetchGql({
url: graphqlUrl,
gqlQuery: gqlAggregationTypeQuery,
variables: { documentName: documentType },
options: {
timeout,
},
});

// axios response "data" field, graphql response "data" field
const responseData = response.data?.data;
if (response.status === 200 && response.statusText === 'OK') {
return responseData;
}

console.error('unexpected response data in fetchRemoteSchema');
throw new NetworkAggregationError(
`Unexpected data in response object. Please verify the endpoint at ${graphqlUrl} is returning a valid GQL Schema.`,
);
} catch (error) {
console.error(`Failed to retrieve schema from url: ${config.graphqlUrl}`);
return;
}
};

const isSupportedType = (
fieldObject: NetworkFieldType<string>,
supportedList: string[],
): fieldObject is SupportedNetworkFieldType => {
return supportedList.includes(fieldObject.type);
};

/**
* Reduce response into simplified object, returning both supported and unsupported types
* @param fields
* @returns { supportedAggregations: [], unsupportedAggregations: [] }
*/
export const getFieldTypes = (fields: GQLFieldType[], supportedAggregationsList: string[]) => {
const fieldTypes = fields.reduce(
(
aggregations: Pick<
NetworkAggregationConfig,
'supportedAggregations' | 'unsupportedAggregations'
>,
field,
) => {
const fieldType = field.type.name;
const fieldObject = { name: field.name, type: fieldType };
if (isSupportedType(fieldObject, supportedAggregationsList)) {
return {
...aggregations,
supportedAggregations: aggregations.supportedAggregations.concat(fieldObject),
};
} else {
return {
...aggregations,
unsupportedAggregations: aggregations.unsupportedAggregations.concat(fieldObject),
};
}
},
{
supportedAggregations: [],
unsupportedAggregations: [],
},
);

return fieldTypes;
};

type NetworkQueryResult = PromiseFulfilledResult<{
config: NetworkAggregationConfigInput;
gqlResponse: GQLTypeQueryResponse;
}>;

/**
* Fetch fields and types from remote connections
* @param { networkConfigs }
* @returns GQL object type to be used in functions
*/
const fetchRemoteSchemas = async ({
networkConfigs,
}: {
networkConfigs: NetworkAggregationConfigInput[];
}): Promise<NetworkAggregationConfig[]> => {
// query remote connection types
const networkQueryPromises = networkConfigs.map(async (config) => {
const gqlResponse = await fetchRemoteSchema(config);
return { config, gqlResponse };
});

const networkQueries = await Promise.allSettled(networkQueryPromises);
const configs = networkQueries
.filter(fulfilledPromiseFilter<NetworkQueryResult>)
.map((networkResult) => {
const { config, gqlResponse } = networkResult.value;
const fields = gqlResponse.__type.fields;

const { supportedAggregations, unsupportedAggregations } = getFieldTypes(
fields,
SUPPORTED_AGGREGATIONS_LIST,
);
return { ...config, supportedAggregations, unsupportedAggregations };
});

return configs;
};
import { NetworkConfig } from './types/setup';

/**
* GQL Federated Search schema setup
* Connects to remote network connections, looks up field types, add field/type pairs to configs
*
* @param { networkConfigs }
* @returns graphql schema for the network - types and resolvers combined
*/
export const createSchemaFromNetworkConfig = async ({
networkConfigs,
}: {
networkConfigs: NetworkAggregationConfigInput[];
networkConfigs: NetworkConfig[];
}) => {
const configs = await fetchRemoteSchemas({
const networkFields = await fetchRemoteSchemas({
networkConfigs,
});

const networkFieldTypes = getAllTypes(configs);
const networkFieldTypes = getAllFieldTypes(networkFields, SUPPORTED_AGGREGATIONS_LIST);

const typeDefs = createTypeDefs(networkFieldTypes);
const resolvers = createResolvers(configs);

const resolvers = createResolvers(networkConfigs);

const networkSchema = makeExecutableSchema({ typeDefs, resolvers });

Expand Down
22 changes: 1 addition & 21 deletions modules/server/src/network/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* These are the GQL queries for the supported GQL types
*/

import { SupportedAggregation, SUPPORTED_AGGREGATIONS } from '../common';
import { Aggregations, NumericAggregations } from '../types';
import { Aggregations } from '../types/aggregations';

export type GQLFieldType = {
name: string;
Expand Down Expand Up @@ -46,22 +45,3 @@ export const gqlAggregationTypeQuery = `#graphql
}
}
`;

export const aggregationsQuery = /* GraphQL */ `
#graphql
{
__typename
bucket_count
buckets {
key
doc_count
}
}
`;

/**
* Returns gql query for gql type
*/
export const supportedAggregationQueries = new Map<SupportedAggregation, string>();
supportedAggregationQueries.set(SUPPORTED_AGGREGATIONS.Aggregations, aggregationsQuery);
supportedAggregationQueries.set(SUPPORTED_AGGREGATIONS.NumericAggregations, '');
Comment on lines -49 to -67

Copy link
Copy Markdown
Contributor Author

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)

79 changes: 41 additions & 38 deletions modules/server/src/network/resolvers/aggregations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading