Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shared-model): generate models from graphql schema #670

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/dev-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ application of our AAD.
| testuser | c0cc4404-7907-4480-86d3-ba4bfc513c6d | Test | User | [email protected] | testorganization (dff7584efe2c174eee8bae45) | `eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJvaWQiOiIxMjM0IiwiZW1haWxzIjpbInRlc3R1c2VyQHRlc3QuY29tIl0sImdpdmVuX25hbWUiOiJUZXN0IiwiZmFtaWx5X25hbWUiOiJVc2VyIDEifQ.` |

The claims will be mapped to the
[AuthUser](../../libs/shared/auth/src/lib/auth-user.model.ts) Model in the
[AuthUser](../../libs/shared/model/src/lib/auth-user.model.ts) Model in the
[AuthInterceptor](../../libs/api/auth/src/lib/interceptors/auth.interceptor.ts).
19 changes: 19 additions & 0 deletions apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
}
}
},
"build-generate-schema-script": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"options": {
"target": "node",
"compiler": "tsc",
"outputPath": "dist/apps/api-generate-schema",
"main": "apps/api/src/generate-schema.ts",
"tsConfig": "apps/api/tsconfig.app.json",
"webpackConfig": "apps/api/webpack.config.js"
}
},
"generate-schema": {
"executor": "@nx/js:node",
"options": {
"buildTarget": "api:build-generate-schema-script",
"watch": false
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
Expand Down
5 changes: 1 addition & 4 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ const UTILITY_MODULES = [
imports: [ConfigModule],
driver: ApolloDriver,
useFactory: (config: ConfigService) => ({
autoSchemaFile:
config.get('NODE_ENV') !== 'production'
? path.join(process.cwd(), 'apps/api/src/schema.gql')
: true,
autoSchemaFile: true,
subscriptions: {
'graphql-ws': true,
},
Expand Down
21 changes: 21 additions & 0 deletions apps/api/src/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NestFactory } from '@nestjs/core';
import { GraphQLSchemaHost } from '@nestjs/graphql';
import { writeFileSync } from 'fs';
import { printSchema } from 'graphql/utilities';
import { join } from 'path';

import { AppModule } from './app/app.module';

const generateSchema = async (): Promise<void> => {
const app = await NestFactory.create(AppModule, { logger: false });
await app.init();

const { schema } = app.get(GraphQLSchemaHost);
const outputPath = join(process.cwd(), 'dist/schema.gql');
writeFileSync(outputPath, printSchema(schema));
await app.close();
// eslint-disable-next-line no-console
console.log(`Scheme generated successfully at ${outputPath}`);
};
// eslint-disable-next-line no-console
generateSchema().catch(console.log);
6 changes: 6 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
overwrite: true
schema: 'dist/schema.gql'
generates:
libs/shared/model/src/lib/gql.model.ts:
plugins:
- typescript
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from 'express';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

export abstract class AuthUserExtractorStrategy {
abstract getUserFromRequest(req: Request): AuthUser | null;
Expand Down
2 changes: 1 addition & 1 deletion libs/api/auth/src/lib/decorators/user.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
createGqlContextForRequest,
createParamDecoratorFactory,
} from '@kordis/api/test-helpers';
import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { User } from './user.decorator';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createGqlContextForRequest,
createHttpContextForRequest,
} from '@kordis/api/test-helpers';
import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { AuthUserExtractorStrategy } from '../auth-user-extractor-strategies/auth-user-extractor.strategy';
import { AuthInterceptor } from './auth.interceptor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Observable, firstValueFrom, of } from 'rxjs';

import { KordisRequest } from '@kordis/api/shared';
import { createGqlContextForRequest } from '@kordis/api/test-helpers';
import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { SentryOTelUserContextInterceptor } from './sentry-otel-user-context.interceptor';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as Sentry from '@sentry/node';
import { Observable } from 'rxjs';

import { KordisGqlContext, KordisRequest } from '@kordis/api/shared';
import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

@Injectable()
export class SentryOTelUserContextInterceptor implements NestInterceptor {
Expand Down
2 changes: 1 addition & 1 deletion libs/api/shared/src/lib/models/request.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request } from 'express';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

export interface KordisGqlContext {
req: KordisRequest;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shared-auth",
"name": "shared-model",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/shared/auth/src",
"sourceRoot": "libs/shared/model/src",
"projectType": "library",
"targets": {
"lint": {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts", "../../../reset.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
}
2 changes: 1 addition & 1 deletion libs/spa/auth/src/lib/components/dev-login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, Component, Inject } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { AUTH_SERVICE } from '../services/auth-service';
import { DevAuthService } from '../services/dev-auth.service';
Expand Down
4 changes: 3 additions & 1 deletion libs/spa/auth/src/lib/services/auth-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

export interface AuthService {
readonly user$: Observable<AuthUser | null>;
readonly isAuthenticated$: Observable<boolean>;

login(): void;

logout(): void;
}

Expand Down
2 changes: 1 addition & 1 deletion libs/spa/auth/src/lib/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
shareReplay,
} from 'rxjs';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { AuthService } from './auth-service';

Expand Down
2 changes: 1 addition & 1 deletion libs/spa/auth/src/lib/services/dev-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject, Observable, map } from 'rxjs';

import { AuthUser } from '@kordis/shared/auth';
import { AuthUser } from '@kordis/shared/model';

import { AuthService } from './auth-service';

Expand Down
Loading