Skip to content

Commit

Permalink
chore: fix gql generator still calling prisma service onModuleInit
Browse files Browse the repository at this point in the history
the issue is that even though I try to override it, the OnModuleInit runs once the module itself is init'ed, which means it doesn't wait for the dependency graph to be complete

as the initial connect is not required, I deferred the connect to application bootstrap, and otherwise it'll simply connect lazily on the first db query

the CIEnvironement as also been renamed to Local to allow greater flexibility in using a trimmed down version of the envs in local dev as well
  • Loading branch information
V-ed committed Aug 13, 2023
1 parent 2759a53 commit 4fb4b8b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion api/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function setupEnv() {
}

function generateGraphQLSchema() {
return exec('pnpm exec nest start --entryFile="@common/graphql/schema/generate-schema"');
return exec('LOCAL=true pnpm exec nest start --entryFile="@common/graphql/schema/generate-schema"');
}

function generatePrismaHelpers() {
Expand Down
12 changes: 7 additions & 5 deletions api/src/@common/configs/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { DynamicModule } from '@nestjs/common';
import { TypedConfigModule, dotenvLoader, selectConfig, type DotenvLoaderOptions } from 'nest-typed-config';
import { EnvironmentConfig } from '../../env.validation';
import { CIEnvironmentConfig } from './ci-env.validation';
import { LocalEnvironmentConfig } from './local-env.validation';

const isCI = process.env.CI == 'true';

export function getSchema(): typeof CIEnvironmentConfig | typeof EnvironmentConfig {
// If GitHub Actions, use CI Env Config
return isCI ? CIEnvironmentConfig : EnvironmentConfig;
const isLocal = isCI || process.env.LOCAL == 'true';

export function getSchema(): typeof LocalEnvironmentConfig | typeof EnvironmentConfig {
// If Local (for example, GitHub Actions), use Local Env Config
return isLocal ? LocalEnvironmentConfig : EnvironmentConfig;
}

export function createConfigModule(options?: DotenvLoaderOptions): DynamicModule {
Expand All @@ -24,7 +26,7 @@ export function createConfigModule(options?: DotenvLoaderOptions): DynamicModule

const configProviders = [...(module.providers ?? [])];

if (isCI) {
if (isLocal) {
configProviders.push({ provide: EnvironmentConfig, useExisting: schema });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { IsString } from 'class-validator';
import { EnvironmentConfig } from '../../env.validation';

export class CIEnvironmentConfig extends EnvironmentConfig {
export class LocalEnvironmentConfig extends EnvironmentConfig {
@IsString()
override readonly EMAIL_FROM: string = '';

Expand Down
2 changes: 1 addition & 1 deletion api/src/@common/graphql/schema/helpers/generator.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GQLGeneratorPrismaService } from './gql-generator-prisma.service';
providers: [
{
provide: PrismaService,
useValue: GQLGeneratorPrismaService,
useClass: GQLGeneratorPrismaService,
},
],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export class GQLGeneratorPrismaService extends PrismaService {
override async onModuleInit(): Promise<void> {
// Do nothing on init (GQL Generation does not need to be connected)
}

override async onModuleDestroy(): Promise<void> {
// Do nothing on destroy (GQL Generation is not connected to db so no need to disconnect)
override async onApplicationBootstrap(): Promise<void> {
// Do nothing on bootstrap (GQL Generation does not need to be connected)
}
}
6 changes: 3 additions & 3 deletions api/src/@common/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaClient } from '$prisma-client';
import { Inject, Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Inject, Injectable, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
import { PrismaSelect } from '@paljs/plugins';
import { PubSub } from 'graphql-subscriptions';
import { withCancel } from '../utils/withCancel';
Expand All @@ -12,14 +12,14 @@ export type PrismaSelector = {
};

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
export class PrismaService extends PrismaClient implements OnApplicationBootstrap, OnModuleDestroy {
private eventSubsSelectors: Record<string, unknown[] | undefined> = {};

constructor(@Inject('PUB_SUB') private pubSub: PubSub) {
super();
}

async onModuleInit(): Promise<void> {
async onApplicationBootstrap(): Promise<void> {
await this.$connect();
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/@utils/tests/TestManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CIEnvironmentConfig } from '$configs/ci-env.validation';
import { ConfigModule } from '$configs/config.module';
import { LocalEnvironmentConfig } from '$configs/local-env.validation';
import { type ModuleMetadata } from '@nestjs/common';
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
import { EnvironmentConfig } from '~/env.validation';
Expand All @@ -25,7 +25,7 @@ export class TestManager<Options extends TestOptions = TestOptions> {
const sharedProviders: NonNullable<typeof metadata>['providers'] = [
{
provide: EnvironmentConfig,
useClass: process.env.CI == 'true' ? CIEnvironmentConfig : EnvironmentConfig,
useClass: process.env.CI == 'true' ? LocalEnvironmentConfig : EnvironmentConfig,
},
];

Expand Down

0 comments on commit 4fb4b8b

Please sign in to comment.