Skip to content

Commit

Permalink
Query to find connection graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Oct 18, 2024
1 parent 834fd27 commit 487e3f1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
15 changes: 15 additions & 0 deletions server/src/entities/connection/connection-graph.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Args, Query, Resolver } from "@nestjs/graphql";
import { ConnectionGraphService } from "./connection-graph.service";
import { FindConnectionGraph } from "./dto/find-connection-graph.dto";

@Resolver()
export class ConnectionGraphResolver {
constructor(private connectionGraphService: ConnectionGraphService) {}

@Query(() => FindConnectionGraph)
async findConnectionGraph(
@Args("root") root: string,
): Promise<FindConnectionGraph> {
return this.connectionGraphService.findBySystem(root);
}
}
53 changes: 53 additions & 0 deletions server/src/entities/connection/connection-graph.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { Connection } from "./connection.model";
import { FindConnectionGraph } from "./dto/find-connection-graph.dto";

@Injectable()
export class ConnectionGraphService {
constructor(
@InjectModel(Connection.name) private connectionModel: Model<Connection>,
) {}

async findBySystem(root: string): Promise<FindConnectionGraph> {
const chains = await this.connectionModel.aggregate([
{ $match: { from: root } },
{
$graphLookup: {
from: "connections",
startWith: "$to",
connectFromField: "to",
connectToField: "from",
as: "children",
depthField: "depth",
maxDepth: 100,
},
},
]);

return {
root,
// TODO: All children are repeated in each chain. Filter or return only one list of children?
chains: chains.map(this.addIdsToChain),
};
}

/**
* Mongo's default ID's (`_id`) need to be mapped manually to GraphQL `id`
* fields because Mongoose does not apply the model to aggregate pipeline
* results.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private addIdsToChain(chain: any) {
return {
...chain,
id: chain._id,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
children: chain.children.map((child: any) => ({
...child,
id: child._id,
})),
};
}
}
11 changes: 9 additions & 2 deletions server/src/entities/connection/connection.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { ConnectionGraphResolver } from "./connection-graph.resolver";
import { ConnectionGraphService } from "./connection-graph.service";
import { Connection, ConnectionSchema } from "./connection.model";
import { ConnectionResolver } from "./connection.resolver";
import { ConnectionService } from "./connection.service";
Expand All @@ -10,7 +12,12 @@ import { ConnectionService } from "./connection.service";
{ name: Connection.name, schema: ConnectionSchema },
]),
],
providers: [ConnectionService, ConnectionResolver],
exports: [MongooseModule, ConnectionService],
providers: [
ConnectionService,
ConnectionResolver,
ConnectionGraphService,
ConnectionGraphResolver,
],
exports: [MongooseModule, ConnectionService, ConnectionGraphService],
})
export class ConnectionModule {}
12 changes: 12 additions & 0 deletions server/src/entities/connection/dto/chain-root.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Field, ObjectType, OmitType } from "@nestjs/graphql";
import { Connection } from "../connection.model";
import { GraphConnection } from "./graph-connection.dto";

@ObjectType()
export class ChainRoot extends OmitType(Connection, ["reverse"]) {
@Field()
reverse: string;

@Field(() => [GraphConnection])
children: GraphConnection[];
}
11 changes: 11 additions & 0 deletions server/src/entities/connection/dto/find-connection-graph.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Field, ObjectType } from "@nestjs/graphql";
import { ChainRoot } from "./chain-root.dto";

@ObjectType()
export class FindConnectionGraph {
@Field()
root: string;

@Field(() => [ChainRoot])
chains: ChainRoot[];
}
11 changes: 11 additions & 0 deletions server/src/entities/connection/dto/graph-connection.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Field, ObjectType, OmitType } from "@nestjs/graphql";
import { Connection } from "../connection.model";

@ObjectType()
export class GraphConnection extends OmitType(Connection, ["reverse"]) {
@Field()
reverse: string;

@Field()
depth: number;
}

0 comments on commit 487e3f1

Please sign in to comment.