Skip to content

Commit

Permalink
refactor(config): make path of both trpc and graphql endpoint can be …
Browse files Browse the repository at this point in the history
…set with configuration
  • Loading branch information
async3619 committed Apr 5, 2023
1 parent d256aae commit f8320c6
Show file tree
Hide file tree
Showing 29 changed files with 267 additions and 179 deletions.
34 changes: 34 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@
},
"nullable": false,
"x-typia-jsDocTags": []
},
"servers": {
"x-typia-required": false,
"type": "object",
"properties": {
"graphql": {
"x-typia-required": false,
"type": "object",
"properties": {
"path": {
"type": "string",
"nullable": false,
"x-typia-required": false
}
},
"nullable": false,
"x-typia-jsDocTags": []
},
"trpc": {
"x-typia-required": false,
"type": "object",
"properties": {
"path": {
"type": "string",
"nullable": false,
"x-typia-required": false
}
},
"nullable": false,
"x-typia-jsDocTags": []
}
},
"nullable": false,
"x-typia-jsDocTags": []
}
},
"nullable": false,
Expand Down
2 changes: 1 addition & 1 deletion src/album/album.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AlbumService } from "@album/album.service";
import { AlbumResolver } from "@album/album.resolver";

@Module({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, ConfigModule.forFeature()],
providers: [AlbumResolver, AlbumService],
exports: [AlbumService],
})
Expand Down
4 changes: 2 additions & 2 deletions src/album/album.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Test, TestingModule } from "@nestjs/testing";

import { MetadataModule } from "@metadata/metadata.module";
import { ConfigModule } from "@config/config.module";

import { AlbumResolver } from "@album/album.resolver";
import { AlbumService } from "@album/album.service";

import { installMockedConfig } from "@test/utils/installMockedConfig";
import { installMetadataMock, MockResolver } from "@test/utils/installMetadataMock";

describe("AlbumResolver", () => {
Expand All @@ -14,7 +14,7 @@ describe("AlbumResolver", () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, installMockedConfig()],
providers: [AlbumResolver, AlbumService],
}).compile();

Expand Down
4 changes: 2 additions & 2 deletions src/album/album.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Test, TestingModule } from "@nestjs/testing";

import { MetadataModule } from "@metadata/metadata.module";
import { ConfigModule } from "@config/config.module";

import { AlbumService } from "@album/album.service";

import { installMetadataMock, MockResolver } from "@test/utils/installMetadataMock";
import { installMockedConfig } from "@test/utils/installMockedConfig";

describe("AlbumService", () => {
let service: AlbumService;
let resolver: MockResolver;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, installMockedConfig()],
providers: [AlbumService],
}).compile();

Expand Down
8 changes: 5 additions & 3 deletions src/album/album.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { Album } from "@common/album.dto";

import { MetadataService } from "@metadata/metadata.service";

import { ConfigData } from "@config/config.module";
import { InjectConfig } from "@config/config.decorator";

import { ObjectService } from "@common/object.service";
import { ConfigService } from "@config/config.service";

@Injectable()
export class AlbumService extends ObjectService<Album> {
public constructor(
@Inject(MetadataService) private readonly metadataService: MetadataService,
@Inject(ConfigService) private readonly configService: ConfigService,
@InjectConfig() private readonly configData: ConfigData,
) {
super(metadataService, configService, ({ resolver, ...input }) => resolver.searchAlbum(input));
super(metadataService, configData, ({ resolver, ...input }) => resolver.searchAlbum(input));
}
}
18 changes: 12 additions & 6 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Module } from "@nestjs/common";
import { GraphQLModule } from "@nestjs/graphql";
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";

import { ConfigModule } from "@config/config.module";
import { ConfigData, ConfigModule } from "@config/config.module";
import { CONFIG_DATA } from "@config/config.decorator";

import { MetadataModule } from "@metadata/metadata.module";
import { AlbumModule } from "@album/album.module";
import { TrackModule } from "@track/track.module";
Expand All @@ -13,12 +15,16 @@ import { TRPCServerModule } from "@trpc-server/trpc-server.module";

@Module({
imports: [
ConfigModule,
GraphQLModule.forRoot<ApolloDriverConfig>({
ConfigModule.forRoot(),
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
playground: process.env.NODE_ENV === "development",
autoSchemaFile: path.join(process.cwd(), "schema.graphqls"),
path: "/",
imports: [ConfigModule.forFeature()],
inject: [CONFIG_DATA],
useFactory: async (configData: ConfigData) => ({
playground: process.env.NODE_ENV === "development",
autoSchemaFile: path.join(process.cwd(), "schema.graphqls"),
path: configData?.servers?.graphql?.path || "/",
}),
}),
TRPCServerModule,
MetadataModule,
Expand Down
2 changes: 1 addition & 1 deletion src/artist/artist.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ArtistService } from "@artist/artist.service";
import { ArtistResolver } from "@artist/artist.resolver";

@Module({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, ConfigModule.forFeature()],
providers: [ArtistResolver, ArtistService],
exports: [ArtistService],
})
Expand Down
4 changes: 2 additions & 2 deletions src/artist/artist.resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Test, TestingModule } from "@nestjs/testing";

import { MetadataModule } from "@metadata/metadata.module";
import { ConfigModule } from "@config/config.module";

import { ArtistResolver } from "@artist/artist.resolver";
import { ArtistService } from "@artist/artist.service";

import { installMetadataMock, MockResolver } from "@test/utils/installMetadataMock";
import { installMockedConfig } from "@test/utils/installMockedConfig";

describe("ArtistResolver", () => {
let resolver: ArtistResolver;
let mockedResolver: MockResolver;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, installMockedConfig()],
providers: [ArtistResolver, ArtistService],
}).compile();

Expand Down
4 changes: 2 additions & 2 deletions src/artist/artist.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Test, TestingModule } from "@nestjs/testing";

import { MetadataModule } from "@metadata/metadata.module";
import { ConfigModule } from "@config/config.module";

import { ArtistService } from "@artist/artist.service";

import { installMetadataMock, MockResolver } from "@test/utils/installMetadataMock";
import { installMockedConfig } from "@test/utils/installMockedConfig";

describe("ArtistService", () => {
let service: ArtistService;
let mockedResolver: MockResolver;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [MetadataModule, ConfigModule],
imports: [MetadataModule, installMockedConfig()],
providers: [ArtistService],
}).compile();

Expand Down
8 changes: 5 additions & 3 deletions src/artist/artist.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Inject, Injectable } from "@nestjs/common";

import { MetadataService } from "@metadata/metadata.service";
import { ConfigService } from "@config/config.service";

import { ConfigData } from "@config/config.module";
import { InjectConfig } from "@config/config.decorator";

import { ObjectService } from "@common/object.service";
import { Artist } from "@common/artist.dto";
Expand All @@ -10,8 +12,8 @@ import { Artist } from "@common/artist.dto";
export class ArtistService extends ObjectService<Artist> {
public constructor(
@Inject(MetadataService) private readonly metadataService: MetadataService,
@Inject(ConfigService) private readonly configService: ConfigService,
@InjectConfig() private readonly configData: ConfigData,
) {
super(metadataService, configService, ({ resolver, ...input }) => resolver.searchArtist(input));
super(metadataService, configData, ({ resolver, ...input }) => resolver.searchArtist(input));
}
}
16 changes: 4 additions & 12 deletions src/common/object.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ObjectService } from "@common/object.service";
import { ConfigService } from "@config/config.service";
import { ConfigData, DEFAULT_CONFIG } from "@config/config.module";

import { MetadataServiceMock } from "@test/utils/metadata.service.mock";

class MockService extends ObjectService<any> {
public constructor() {
super(new MetadataServiceMock(), new ConfigService(), async () => [{ test: "value" }]);
public constructor(config: ConfigData = DEFAULT_CONFIG) {
super(new MetadataServiceMock(), config, async () => [{ test: "value" }]);
}
}

Expand All @@ -22,22 +22,14 @@ describe("ObjectService", () => {
});

it("should set cache TTL on module initialization", async () => {
const service = new MockService();
Object.defineProperty(service["config"], "getConfig", {
value: () => ({ cacheTTL: 1000 }),
});

const service = new MockService({ cacheTTL: 1000, resolvers: {} });
await service.onModuleInit();

expect(service["searchCache"]["timeToLive"]).toBe(1000);
});

it("should not set cache TTL on module initialization if it is not defined", async () => {
const service = new MockService();
Object.defineProperty(service["config"], "getConfig", {
value: () => ({}),
});

await service.onModuleInit();

expect(service["searchCache"]["timeToLive"]).toBe(3600);
Expand Down
10 changes: 6 additions & 4 deletions src/common/object.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { OnModuleInit } from "@nestjs/common";

import { MetadataService } from "@metadata/metadata.service";
import BaseResolver from "@metadata/resolvers/base";

import { ConfigData } from "@config/config.module";

import { SearchInput } from "@common/search-input.dto";

import { CacheStorage } from "@utils/cache";
import { AsyncFn } from "@utils/types";
import { ConfigService } from "@config/config.service";
import { OnModuleInit } from "@nestjs/common";

interface SearchInputData extends SearchInput {
resolver: BaseResolver<string, any>;
Expand All @@ -19,12 +21,12 @@ export class ObjectService<TItem> implements OnModuleInit {

constructor(
private readonly metadata: MetadataService,
private readonly config: ConfigService,
private readonly config: ConfigData,
private readonly searchFn: AsyncFn<SearchInputData, TItem[]>,
) {}

public onModuleInit() {
const config = this.config.getConfig();
const config = this.config;
if (typeof config.cacheTTL !== "number") {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/config.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Inject } from "@nestjs/common";

export const CONFIG_DATA = "CONFIG_DATA";

export function InjectConfig() {
return Inject(CONFIG_DATA);
}
Loading

0 comments on commit f8320c6

Please sign in to comment.