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 all 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 @@ -41,10 +41,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
Expand Up @@ -5,7 +5,7 @@ import * as jwt from 'jsonwebtoken';
import jwksClient from 'jwks-rsa';

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

import { VerifyAuthUserStrategy } from './verify-auth-user.strategy';

Expand Down
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 VerifyAuthUserStrategy {
/*
Expand Down
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';

import { VerifyAuthUserStrategy } from './verify-auth-user.strategy';

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 { VerifyAuthUserStrategy } from '../auth-strategies/verify-auth-user.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.
101 changes: 101 additions & 0 deletions libs/shared/model/src/lib/gql.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = {
[K in keyof T]: T[K];
};
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]?: Maybe<T[SubKey]>;
};
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]: Maybe<T[SubKey]>;
};
export type MakeEmpty<
T extends { [key: string]: unknown },
K extends keyof T,
> = { [_ in K]?: never };
export type Incremental<T> =
| T
| {
[P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never;
};
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: { input: string; output: string };
String: { input: string; output: string };
Boolean: { input: boolean; output: boolean };
Int: { input: number; output: number };
Float: { input: number; output: number };
};

export type AppData = {
__typename?: 'AppData';
message: Scalars['String']['output'];
};

export type BBox = {
__typename?: 'BBox';
bottomRight: Coordinate;
topLeft: Coordinate;
};

export type BBoxInput = {
bottomRight: CoordinateInput;
topLeft: CoordinateInput;
};

export type Coordinate = {
__typename?: 'Coordinate';
lat: Scalars['Float']['output'];
lon: Scalars['Float']['output'];
};

export type CoordinateInput = {
lat: Scalars['Float']['input'];
lon: Scalars['Float']['input'];
};

export type Mutation = {
__typename?: 'Mutation';
createOrganization: Organization;
updateOrganizationGeoSettings: Organization;
};

export type MutationCreateOrganizationArgs = {
geoSettings: OrganizationGeoSettingsInput;
name: Scalars['String']['input'];
};

export type MutationUpdateOrganizationGeoSettingsArgs = {
geoSettings: OrganizationGeoSettingsInput;
id: Scalars['String']['input'];
};

export type Organization = {
__typename?: 'Organization';
createdAt: Scalars['String']['output'];
geoSettings: OrganizationGeoSettings;
id: Scalars['String']['output'];
name: Scalars['String']['output'];
updatedAt: Scalars['String']['output'];
};

export type OrganizationGeoSettings = {
__typename?: 'OrganizationGeoSettings';
bbox: BBox;
centroid: Coordinate;
};

export type OrganizationGeoSettingsInput = {
bbox: BBoxInput;
centroid: CoordinateInput;
};

export type Query = {
__typename?: 'Query';
data: AppData;
organization: Organization;
};

export type QueryOrganizationArgs = {
id: Scalars['String']['input'];
};
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