Skip to content

Commit

Permalink
fix(generated): paths should be relative (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 26, 2019
1 parent 871eda0 commit 684057e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Maybe } from './types';

export interface AppOptions<T> {
authChecker?: AuthChecker<T>;
container?: Container;
container: Container;
context?: (request: Request) => object;
host?: string;
generatedFolder?: string;
Expand All @@ -43,6 +43,7 @@ export class App<C extends BaseContext> {
appPort: number;
authChecker: AuthChecker<C>;
connection!: Connection;
container: Container;
context: (request: Request) => object;
generatedFolder: string;
graphQLServer!: ApolloServer;
Expand All @@ -59,10 +60,10 @@ export class App<C extends BaseContext> {
}

// 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<string> = this.appOptions.host || process.env.APP_HOST;
if (!host) {
Expand All @@ -76,6 +77,9 @@ export class App<C extends BaseContext> {

// 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 = () => {
Expand Down
4 changes: 4 additions & 0 deletions src/decorators/EnumField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'
Expand Down
12 changes: 10 additions & 2 deletions src/decorators/EnumField.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -27,7 +35,7 @@ export function EnumField(name: string, enumeration: object, options: EnumFieldO
propertyKey,
name,
enumeration,
enumFileName
relativeFilePath
);
};

Expand Down
9 changes: 8 additions & 1 deletion src/decorators/Model.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/tgql/BaseResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class BaseResolver<E extends BaseModel> {
});

// 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:
Expand Down

0 comments on commit 684057e

Please sign in to comment.