-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and fixed issues with double resolving modules
- Loading branch information
Showing
14 changed files
with
199 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,46 @@ | ||
import type { UnTypedClass } from "@proto-kit/protocol"; | ||
import { ConfigurableModule } from "@proto-kit/common"; | ||
import { | ||
ConfigurableModule, | ||
TypedClass, | ||
} from "@proto-kit/common"; | ||
import { GraphQLSchema } from "graphql/type"; | ||
import { injectable, Lifecycle, scoped } from "tsyringe"; | ||
import { Resolver } from "type-graphql"; | ||
|
||
const graphqlModuleMetadataKey = "graphqlModule"; | ||
|
||
export abstract class GraphqlModule<Config> extends ConfigurableModule<Config> { | ||
public abstract resolverType: UnTypedClass; | ||
public constructor() { | ||
super(); | ||
|
||
const isDecoratedProperly = | ||
Reflect.getMetadata(graphqlModuleMetadataKey, this.constructor) === true; | ||
if (!isDecoratedProperly) { | ||
throw new Error( | ||
`Module ${this.constructor.name} not decorated property. Make sure to use @graphqlModule() on all GraphqlModules` | ||
); | ||
} | ||
} | ||
} | ||
|
||
export abstract class SchemaGeneratingGraphqlModule< | ||
Config | ||
> extends GraphqlModule<Config> { | ||
public abstract generateSchema(): GraphQLSchema; | ||
} | ||
|
||
export function graphqlModule() { | ||
return ( | ||
/** | ||
* Check if the target class extends RuntimeModule, while | ||
* also providing static config presets | ||
*/ | ||
target: TypedClass<GraphqlModule<unknown>> | ||
) => { | ||
injectable()(target); | ||
scoped(Lifecycle.ContainerScoped)(target); | ||
// eslint-disable-next-line new-cap | ||
Resolver()(target); | ||
|
||
Reflect.defineMetadata(graphqlModuleMetadataKey, true, target); | ||
}; | ||
} |
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
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
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,55 @@ | ||
import { Field, ObjectType, Query } from "type-graphql"; | ||
import { ChildContainerProvider } from "@proto-kit/common"; | ||
|
||
import { graphqlModule, GraphqlModule } from "../GraphqlModule"; | ||
import { NodeStatus, NodeStatusService } from "../services/NodeStatusService"; | ||
|
||
@ObjectType() | ||
export class NodeStatusObject { | ||
public static fromServiceLayerModel(status: NodeStatus) { | ||
return new NodeStatusObject( | ||
status.uptime, | ||
status.uptimeHumanReadable, | ||
status.height | ||
); | ||
} | ||
|
||
@Field() | ||
public uptime: number; | ||
|
||
@Field() | ||
public height: number; | ||
|
||
@Field() | ||
public uptimeHumanReadable: string; | ||
|
||
public constructor( | ||
uptime: number, | ||
uptimeHumanReadable: string, | ||
height: number | ||
) { | ||
this.uptime = uptime; | ||
this.uptimeHumanReadable = uptimeHumanReadable; | ||
this.height = height; | ||
} | ||
} | ||
|
||
@graphqlModule() | ||
export class NodeStatusResolver extends GraphqlModule<object> { | ||
public constructor(private readonly nodeStatusService: NodeStatusService) { | ||
super(); | ||
} | ||
|
||
public create(childContainerProvider: ChildContainerProvider) { | ||
super.create(childContainerProvider); | ||
|
||
// Workaround to initialize uptime | ||
void this.nodeStatusService.getNodeStatus(); | ||
} | ||
|
||
@Query(() => NodeStatusObject) | ||
public async nodeStatus(): Promise<NodeStatusObject> { | ||
const status = await this.nodeStatusService.getNodeStatus(); | ||
return NodeStatusObject.fromServiceLayerModel(status); | ||
} | ||
} |
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,31 @@ | ||
import { inject, injectable } from "tsyringe"; | ||
import { BlockStorage } from "@proto-kit/sequencer"; | ||
import humanizeDuration from "humanize-duration"; | ||
|
||
export interface NodeStatus { | ||
uptime: number; | ||
uptimeHumanReadable: string; | ||
height: number; | ||
} | ||
|
||
@injectable() | ||
export class NodeStatusService { | ||
private readonly startupTime = Date.now(); | ||
|
||
public constructor( | ||
@inject("BlockStorage") private readonly blockStorage: BlockStorage | ||
) { | ||
} | ||
|
||
public async getNodeStatus(): Promise<NodeStatus> { | ||
const uptime = Date.now() - this.startupTime; | ||
const uptimeHumanReadable = humanizeDuration(uptime); | ||
const height = await this.blockStorage.getCurrentBlockHeight(); | ||
|
||
return { | ||
uptime, | ||
uptimeHumanReadable, | ||
height, | ||
}; | ||
} | ||
} |
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
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
Oops, something went wrong.