Skip to content

Commit

Permalink
cleanup network typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciaran Schutte committed Dec 16, 2024
1 parent 074b775 commit 139dd88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 82 deletions.
56 changes: 25 additions & 31 deletions modules/server/src/network/typeDefs/aggregations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion modules/server/src/network/typeDefs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedNetworkFieldType } from '../types/types';
import { SupportedNetworkFieldType } from '../setup/fields';
import { createNetworkAggregationTypeDefs } from './aggregations';

export const createTypeDefs = (networkFieldTypes: SupportedNetworkFieldType[]) => {
Expand Down
50 changes: 0 additions & 50 deletions modules/server/src/network/typeDefs/networkAggregations.ts

This file was deleted.

0 comments on commit 139dd88

Please sign in to comment.