diff --git a/src/core/app.ts b/src/core/app.ts index 28e9f0c0..53722e43 100644 --- a/src/core/app.ts +++ b/src/core/app.ts @@ -28,7 +28,7 @@ import { Maybe } from './types'; export interface AppOptions { authChecker?: AuthChecker; - container?: Container; + container: Container; context?: (request: Request) => object; host?: string; generatedFolder?: string; @@ -43,6 +43,7 @@ export class App { appPort: number; authChecker: AuthChecker; connection!: Connection; + container: Container; context: (request: Request) => object; generatedFolder: string; graphQLServer!: ApolloServer; @@ -59,10 +60,10 @@ export class App { } // Ensure that Warthog, TypeORM and TypeGraphQL are all using the same typedi container - if (this.appOptions.container) { - TypeGraphQLUseContainer(this.appOptions.container as any); // TODO: fix any - TypeORMUseContainer(this.appOptions.container as any); // TODO: fix any - } + + this.container = this.appOptions.container; + TypeGraphQLUseContainer(this.container as any); // TODO: fix any + TypeORMUseContainer(this.container as any); // TODO: fix any const host: Maybe = this.appOptions.host || process.env.APP_HOST; if (!host) { @@ -76,6 +77,9 @@ export class App { // Use https://github.com/inxilpro/node-app-root-path to find project root this.generatedFolder = this.appOptions.generatedFolder || path.join(process.cwd(), 'generated'); + // Set this so that we can pull in decorators later + Container.set('warthog:generatedFolder', this.generatedFolder); + this.logger = Container.has('LOGGER') ? Container.get('LOGGER') : logger; const returnEmpty = () => { diff --git a/src/decorators/EnumField.test.ts b/src/decorators/EnumField.test.ts index 81b53a68..39e1cb9c 100644 --- a/src/decorators/EnumField.test.ts +++ b/src/decorators/EnumField.test.ts @@ -2,6 +2,7 @@ import 'reflect-metadata'; import { IntrospectionEnumType, IntrospectionSchema } from 'graphql'; import { ObjectType, Query, Resolver } from 'type-graphql'; +import { Container } from 'typedi'; import { getSchemaInfo } from '../schema'; @@ -11,6 +12,9 @@ describe('Enums', () => { let schemaIntrospection: IntrospectionSchema; beforeAll(async () => { + // TODO: should we set this up as part of the test harness? + Container.set('warthog:generatedFolder', process.cwd()); + enum StringEnum { Foo = 'FOO', Bar = 'BAR' diff --git a/src/decorators/EnumField.ts b/src/decorators/EnumField.ts index d010220a..efa24ae7 100644 --- a/src/decorators/EnumField.ts +++ b/src/decorators/EnumField.ts @@ -1,5 +1,7 @@ const caller = require('caller'); // tslint:disable-line:no-var-requires +import * as path from 'path'; import { Field, registerEnumType } from 'type-graphql'; +import { Container } from 'typedi'; import { Column } from 'typeorm'; import { getMetadataStorage } from '../metadata'; @@ -15,7 +17,13 @@ export function EnumField(name: string, enumeration: object, options: EnumFieldO // In order to use the enums in the generated classes file, we need to // save their locations and import them in the generated file - const enumFileName = caller(); + const decoratorSourceFile = caller(); + + // Use relative paths in the source files so that they can be used on different machines + const relativeFilePath = path.relative( + Container.get('warthog:generatedFolder'), + decoratorSourceFile + ); const registerEnumWithWarthog = ( target: any, @@ -27,7 +35,7 @@ export function EnumField(name: string, enumeration: object, options: EnumFieldO propertyKey, name, enumeration, - enumFileName + relativeFilePath ); }; diff --git a/src/decorators/Model.ts b/src/decorators/Model.ts index 857b6351..d7b8580e 100644 --- a/src/decorators/Model.ts +++ b/src/decorators/Model.ts @@ -1,5 +1,7 @@ const caller = require('caller'); // tslint:disable-line:no-var-requires +import * as path from 'path'; import { ObjectType } from 'type-graphql'; +import { Container } from 'typedi'; import { Entity } from 'typeorm'; import { getMetadataStorage } from '../metadata'; @@ -10,11 +12,16 @@ interface ModelOptions { } export function Model(this: any, args: ModelOptions = {}): any { + // In order to use the enums in the generated classes file, we need to + // save their locations and import them in the generated file const modelFileName = caller(); + // Use relative paths in the source files so that they can be used on different machines + const relativeFilePath = path.relative(Container.get('warthog:generatedFolder'), modelFileName); + const registerModelWithWarthog = (target: any): any => { // Save off where the model is located so that we can import it in the generated classes - getMetadataStorage().addModel(target.name, target, modelFileName); + getMetadataStorage().addModel(target.name, target, relativeFilePath); }; const factories = [ diff --git a/src/tgql/BaseResolver.ts b/src/tgql/BaseResolver.ts index 22e35cae..d5e2e443 100644 --- a/src/tgql/BaseResolver.ts +++ b/src/tgql/BaseResolver.ts @@ -128,7 +128,7 @@ export class BaseResolver { }); // TODO: remove any when this is fixed: https://github.com/Microsoft/TypeScript/issues/21592 - return this.repository.save(results as any, { reload: true }); + return this.repository.save(results, { reload: true }); } // TODO: There must be a more succinct way to: