diff --git a/packages/graphql/src/schema-model/relationship/model-adapters/RelationshipAdapter.test.ts b/packages/graphql/src/schema-model/relationship/model-adapters/RelationshipAdapter.test.ts index ba352de775..ae619af991 100644 --- a/packages/graphql/src/schema-model/relationship/model-adapters/RelationshipAdapter.test.ts +++ b/packages/graphql/src/schema-model/relationship/model-adapters/RelationshipAdapter.test.ts @@ -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(); }); }); diff --git a/packages/graphql/src/schema/aggregations/aggregations-types-mapper.test.ts b/packages/graphql/src/schema/aggregations/aggregation-types-mapper.test.ts similarity index 100% rename from packages/graphql/src/schema/aggregations/aggregations-types-mapper.test.ts rename to packages/graphql/src/schema/aggregations/aggregation-types-mapper.test.ts diff --git a/packages/graphql/src/schema/aggregations/aggregation-types-mapper.ts b/packages/graphql/src/schema/aggregations/aggregation-types-mapper.ts index 555a373453..c8f0b12d58 100644 --- a/packages/graphql/src/schema/aggregations/aggregation-types-mapper.ts +++ b/packages/graphql/src/schema/aggregations/aggregation-types-mapper.ts @@ -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"; @@ -26,16 +27,56 @@ export class AggregationTypesMapper { private readonly aggregationSelectionTypes: Record>; 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 | 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> { diff --git a/packages/graphql/src/schema/aggregations/field-aggregation-composer.ts b/packages/graphql/src/schema/aggregations/field-aggregation-composer.ts index cfb0d2047c..b5d5b79fd5 100644 --- a/packages/graphql/src/schema/aggregations/field-aggregation-composer.ts +++ b/packages/graphql/src/schema/aggregations/field-aggregation-composer.ts @@ -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"; @@ -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 } : {}), }, diff --git a/packages/graphql/src/schema/generation/aggregate-types.ts b/packages/graphql/src/schema/generation/aggregate-types.ts index e971fcde4f..ef8c34d99a 100644 --- a/packages/graphql/src/schema/generation/aggregate-types.ts +++ b/packages/graphql/src/schema/generation/aggregate-types.ts @@ -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, @@ -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), });