-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
server/src/entities/connection/connection-graph.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
server/src/entities/connection/connection-graph.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
server/src/entities/connection/dto/find-connection-graph.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
server/src/entities/connection/dto/graph-connection.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |