Skip to content

Commit

Permalink
Merge pull request #6010 from neo4j/fix-tests-aggregation-6
Browse files Browse the repository at this point in the history
Fix tests aggregation 6
  • Loading branch information
angrykoala authored Feb 18, 2025
2 parents d84f56c + 7011898 commit 27f6492
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe("RelationshipAdapter", () => {

test("should parse selectable", () => {
const relationshipAdapter = userAdapter.relationships.get("accounts");
expect(relationshipAdapter?.isAggregable()).toBeTrue();
expect(relationshipAdapter?.isAggregable()).toBeFalse();
expect(relationshipAdapter?.isReadable()).toBeFalse();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*/

import { GraphQLInt, GraphQLNonNull } from "graphql";
import type { ObjectTypeComposer, SchemaComposer } from "graphql-compose";
import type { Subgraph } from "../../classes/Subgraph";
import { idResolver } from "../resolvers/field/id";
Expand All @@ -26,16 +27,56 @@ export class AggregationTypesMapper {
private readonly aggregationSelectionTypes: Record<string, ObjectTypeComposer<unknown, unknown>>;

private readonly subgraph: Subgraph | undefined;
private readonly composer: SchemaComposer;

constructor(composer: SchemaComposer, subgraph?: Subgraph) {
this.subgraph = subgraph;
this.aggregationSelectionTypes = this.getOrCreateAggregationSelectionTypes(composer);
this.composer = composer;
}

public getAggregationType(typeName: string): ObjectTypeComposer<unknown, unknown> | undefined {
return this.aggregationSelectionTypes[typeName];
}

/** Top level count */
public getCountType(): ObjectTypeComposer {
const countFieldName = "Count";
const directives: string[] = this.subgraph ? [this.subgraph.getFullyQualifiedDirectiveName("shareable")] : [];
return this.composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
});
for (const directiveName of directives) {
countField.setDirectiveByName(directiveName);
}
});
}

/** Nested count */
public getCountConnectionType(): ObjectTypeComposer {
const countFieldName = "CountConnection";
const directives: string[] = this.subgraph ? [this.subgraph.getFullyQualifiedDirectiveName("shareable")] : [];
return this.composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
edges: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
});
for (const directiveName of directives) {
countField.setDirectiveByName(directiveName);
}
});
}

private getOrCreateAggregationSelectionTypes(
composer: SchemaComposer
): Record<string, ObjectTypeComposer<unknown, unknown>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { RelationshipAdapter } from "../../schema-model/relationship/model-adapt
import type { RelationshipDeclarationAdapter } from "../../schema-model/relationship/model-adapters/RelationshipDeclarationAdapter";
import type { Neo4jFeaturesSettings } from "../../types";
import { DEPRECATE_ID_AGGREGATION } from "../constants";
import { getCountConnectionType } from "../generation/aggregate-types";
import { shouldAddDeprecatedFields } from "../generation/utils";
import { numericalResolver } from "../resolvers/field/numerical";
import { AggregationTypesMapper } from "./aggregation-types-mapper";
Expand Down Expand Up @@ -85,7 +84,7 @@ export class FieldAggregationComposer {
this.composer.createObjectTC({
name: relationshipAdapter.operations.getAggregateFieldTypename(),
fields: {
count: getCountConnectionType(this.composer).NonNull,
count: this.aggregationTypesMapper.getCountConnectionType().NonNull,
...(aggregateSelectionNode ? { node: aggregateSelectionNode } : {}),
...(aggregateSelectionEdge ? { edge: aggregateSelectionEdge } : {}),
},
Expand Down
32 changes: 1 addition & 31 deletions packages/graphql/src/schema/generation/aggregate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,6 @@ export function withAggregateSelectionType({
return aggregateSelection;
}

/** Top level count */
export function getCountType(composer: SchemaComposer): ObjectTypeComposer {
const countFieldName = "Count";
return composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
});
});
}

/** Nested count */
export function getCountConnectionType(composer: SchemaComposer): ObjectTypeComposer {
const countFieldName = "CountConnection";
return composer.getOrCreateOTC(countFieldName, (countField) => {
countField.addFields({
nodes: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
edges: {
type: new GraphQLNonNull(GraphQLInt),
resolve: numericalResolver,
},
});
});
}

/** Create aggregate field inside connections */
function createConnectionAggregate({
entityAdapter,
Expand Down Expand Up @@ -133,7 +103,7 @@ function createConnectionAggregate({
const connectionAggregate = composer.createObjectTC({
name: entityAdapter.operations.aggregateTypeNames.connection,
fields: {
count: getCountType(composer).NonNull,
count: aggregationTypesMapper.getCountType().NonNull,
},
directives: graphqlDirectivesToCompose(propagatedDirectives),
});
Expand Down

0 comments on commit 27f6492

Please sign in to comment.