diff --git a/modules/server/src/network/typeDefs/aggregations.ts b/modules/server/src/network/typeDefs/aggregations.ts index 901f45d7d..bdcde465a 100644 --- a/modules/server/src/network/typeDefs/aggregations.ts +++ b/modules/server/src/network/typeDefs/aggregations.ts @@ -7,48 +7,45 @@ import { GraphQLString, } from 'graphql'; import GraphQLJSON from 'graphql-type-json'; -import { SupportedNetworkFieldType } from '../types/types'; -import { singleToNetworkAggregationMap } from './networkAggregations'; +import { SupportedNetworkFieldType } from '../setup/fields'; /** - * Converts field/types to GQLObjectType definition shape + * Creates nested GQL structured object from field type + * { name: 'donor_gender', type: 'Aggregation'} + * => + * { name: 'donor_gender', type: { name: 'Aggregation' }} * - * @example - * { name: "donor_age", type: "NumericAggregations" } => { donor_age: { type: "NetworkNumericAggregations" } } + * @param fieldTypes - An array of fields with types + * @returns Structured GQL fields object */ -const convertToGQLObjectType = (networkFieldTypes: SupportedNetworkFieldType[]) => { - return networkFieldTypes.reduce((allFields, currentField) => { +const convertToGQLObjectType = (fieldTypes: SupportedNetworkFieldType[]) => { + return fieldTypes.reduce((allFields, currentField) => { const field = { - [currentField.name]: { type: singleToNetworkAggregationMap.get(currentField.type) }, + [currentField.name]: { type: currentField.type }, }; return { ...allFields, ...field }; }, {}); }; /** - * Returns available aggregations by filtering duplicates, and mapping from - * singular remote aggregation type to network aggregation types + * Typedefs for network search. + * Typenames need to be unique globally or gql will merge / throw errors. * - * eg. NumericAggregations to NetworkNumericAggregations - * - * There is no distinction on which types come from which remote connections - * This is the resolvers responsibility - * - * @param configs - * @returns + * TODO: Use a single convention of creating typedefs consistently + * Some redundancy here by creating typedefs in object centric way. + * There are typesdefs already declared as gql tagged template strings elsewhere. + * Objects are easier to compose and are typesafe. */ -export const createNetworkAggregationTypeDefs = ( - networkFieldTypes: SupportedNetworkFieldType[], -) => { - const allFields = convertToGQLObjectType(networkFieldTypes); +export const createNetworkAggregationTypeDefs = (fieldTypes: SupportedNetworkFieldType[]) => { + const fields = convertToGQLObjectType(fieldTypes); - const aggregationsType = new GraphQLObjectType({ - name: 'NodeAggregations', - fields: allFields, + const aggregations = new GraphQLObjectType({ + name: 'NetworkAggregations', + fields, }); - const remoteConnectionType = new GraphQLObjectType({ - name: 'NetworkNode', + const node = new GraphQLObjectType({ + name: 'Node', fields: { name: { type: GraphQLString }, hits: { type: GraphQLInt }, @@ -57,19 +54,16 @@ export const createNetworkAggregationTypeDefs = ( }, }); - const connectionNodeType = new GraphQLList(remoteConnectionType); - const networkType = new GraphQLObjectType({ name: 'Network', fields: { - nodes: { type: connectionNodeType }, + nodes: { type: new GraphQLList(node) }, aggregations: { - type: aggregationsType, + type: aggregations, }, }, }); - // correct object structure to merge with other types const rootType = new GraphQLObjectType({ name: 'Root', fields: { diff --git a/modules/server/src/network/typeDefs/index.ts b/modules/server/src/network/typeDefs/index.ts index 1527d41c1..afe487439 100644 --- a/modules/server/src/network/typeDefs/index.ts +++ b/modules/server/src/network/typeDefs/index.ts @@ -1,4 +1,4 @@ -import { SupportedNetworkFieldType } from '../types/types'; +import { SupportedNetworkFieldType } from '../setup/fields'; import { createNetworkAggregationTypeDefs } from './aggregations'; export const createTypeDefs = (networkFieldTypes: SupportedNetworkFieldType[]) => { diff --git a/modules/server/src/network/typeDefs/networkAggregations.ts b/modules/server/src/network/typeDefs/networkAggregations.ts deleted file mode 100644 index 81ec37d6b..000000000 --- a/modules/server/src/network/typeDefs/networkAggregations.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { GraphQLInt, GraphQLList, GraphQLObjectType, GraphQLString } from 'graphql'; -import { SupportedAggregation, SUPPORTED_AGGREGATION } from '../setup/constants'; - -const bucket = new GraphQLObjectType({ - name: 'bucket', - fields: { - doc_count: { - type: GraphQLInt, - }, - key: { - type: GraphQLString, - }, - relation: { - type: GraphQLString, - }, - }, -}); - -// TODO: Placeholder to expand type -const networkAggregations = new GraphQLObjectType({ - name: 'NetworkAggregations', - fields: { - bucket_count: { - type: GraphQLInt, - }, - buckets: { - type: new GraphQLList(bucket), - }, - }, -}); - -// TODO: Placeholder to expand type -const numericNetworkAggregations = new GraphQLObjectType({ - name: 'NumericNetworkAggregations', - fields: { - test: { - type: GraphQLString, - }, - }, -}); - -/** - * return network aggregation gql type - */ -export const singleToNetworkAggregationMap = new Map(); -singleToNetworkAggregationMap.set(SUPPORTED_AGGREGATION.Aggregations, networkAggregations); -singleToNetworkAggregationMap.set( - SUPPORTED_AGGREGATION.NumericAggregations, - numericNetworkAggregations, -);