Skip to content

Commit

Permalink
fix(various): various fixes (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 12, 2019
1 parent a158d3d commit ad28c60
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 71 deletions.
1 change: 0 additions & 1 deletion examples/1-simple-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Running `yarn bootstrap` will do the following:

- Install packages
- Create the example DB
- Seed the database with test data

## Running the App

Expand Down
15 changes: 4 additions & 11 deletions examples/1-simple-model/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ dotenv.config();
import { App } from '../../../src/';

async function bootstrap() {
const app = new App(
// Path written in generated classes
{
container: Container,
warthogImportPath: '../../../src'
},
{
cache: true,
synchronize: true
}
);
const app = new App({
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
});

await app.start();
}
Expand Down
10 changes: 2 additions & 8 deletions examples/2-complex-example/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import { App, AppOptions } from '../../../src/';

export function getApp(appOptions: Partial<AppOptions> = {}, dbOptions: any = {}) {
return new App(
// Path written in generated classes
{
container: Container,
warthogImportPath: '../../../src',
warthogImportPath: '../../../src', // Path written in generated classes
...appOptions
},
{
cache: true,
synchronize: true,
// dropSchema: true,
...dbOptions
}
dbOptions
);
}
1 change: 0 additions & 1 deletion examples/3-relationships/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Running `yarn bootstrap` will do the following:

- Install packages
- Create the example DB
- Seed the database

## Running the App

Expand Down
14 changes: 4 additions & 10 deletions examples/3-relationships/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ dotenv.config();
import { App } from '../../../src/';

async function bootstrap() {
const app = new App(
{
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
},
{
cache: true,
synchronize: true
}
);
const app = new App({
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
});

await app.start();
}
Expand Down
22 changes: 11 additions & 11 deletions src/core/BaseModel.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import * as shortid from 'shortid';
import { Field, Int, InterfaceType, ObjectType } from 'type-graphql';
import { Field, ID, Int, InterfaceType, ObjectType } from 'type-graphql';
import { BeforeInsert, Column, CreateDateColumn, PrimaryColumn, UpdateDateColumn, VersionColumn } from 'typeorm';

import { ID } from '.';
import { IDType } from './types';

// tslint:disable:max-classes-per-file

// This interface adds all of the base type-graphql fields to our BaseClass
@InterfaceType()
export abstract class BaseGraphQLObject {
@Field(type => String)
id!: ID;
@Field(type => ID)
id!: IDType;

@Field() createdAt!: Date;
@Field() createdById?: ID;
@Field() createdById?: IDType;

@Field({ nullable: true })
updatedAt?: Date;
@Field({ nullable: true })
updatedById?: ID;
updatedById?: IDType;

@Field({ nullable: true })
deletedAt?: Date;
@Field({ nullable: true })
deletedById?: ID;
deletedById?: IDType;

@Field(type => Int)
version!: number;
Expand All @@ -33,20 +33,20 @@ export abstract class BaseGraphQLObject {
@ObjectType({ implements: BaseGraphQLObject })
export abstract class BaseModel implements BaseGraphQLObject {
@PrimaryColumn({ type: String })
id!: ID;
id!: IDType;

@CreateDateColumn() createdAt!: Date;
@Column() createdById!: ID;
@Column() createdById!: IDType;

@UpdateDateColumn({ nullable: true })
updatedAt?: Date;
@Column({ nullable: true })
updatedById?: ID;
updatedById?: IDType;

@Column({ nullable: true })
deletedAt?: Date;
@Column({ nullable: true })
deletedById?: ID;
deletedById?: IDType;

@VersionColumn() version!: number;

Expand Down
15 changes: 7 additions & 8 deletions src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class App {
schema?: GraphQLSchema;

constructor(private appOptions: AppOptions, private dbOptions: Partial<ConnectionOptions> = {}) {
if (!process.env.NODE_ENV) {
throw new Error("NODE_ENV must be set - use 'development' locally");
}

if (this.appOptions.container) {
// register 3rd party IOC container
TypeGraphQLUseContainer(this.appOptions.container);
Expand All @@ -52,6 +56,9 @@ export class App {

async establishDBConnection(): Promise<Connection> {
if (!this.connection) {
if (typeof this.dbOptions.synchronize === 'undefined') {
this.dbOptions = { ...this.dbOptions, synchronize: process.env.NODE_ENV === 'development' };
}
this.connection = await createDBConnection(this.dbOptions);
}

Expand Down Expand Up @@ -150,14 +157,6 @@ export class App {
console.log(`🚀 Server ready at http://${this.appHost}:${this.appPort}${this.graphQLServer.graphqlPath}`)
);

// // Configure server options
// const serverOptions: Options = {
// endpoint: '/graphql',
// formatError: formatArgumentValidationError,
// playground: '/playground',
// port: this.appPort
// };

return this;
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type StringMap = { [key: string]: string };

export type DateTime = string;
export type ID = string;
export type IDType = string;

export interface BaseEntity {
id: ID;
id: IDType;
[key: string]: any;
}

export type WhereInput = {
id_eq?: ID;
id_in?: ID[];
id_eq?: IDType;
id_in?: IDType[];
};

export type DeleteReponse = {
id: ID;
id: IDType;
};

export interface ClassType<T = any> {
Expand Down
2 changes: 1 addition & 1 deletion src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SchemaGenerator {
// This file has been auto-generated by Warthog. Do not update directly as it
// will be re-written. If you need to change this file, update models or add
// new TypeGraphQL objects
import { ArgsType, Field, InputType } from 'type-graphql';
import { ArgsType, Field, ID, InputType } from 'type-graphql';
import { registerEnumType } from 'type-graphql';
import { BaseWhereInput, PaginationArgs } from '${warthogImportPath}';
${entityListToImports(entities).join('')}
Expand Down
20 changes: 15 additions & 5 deletions src/schema/TypeORMConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { GraphQLInt, GraphQLScalarType, GraphQLString, GraphQLBoolean, GraphQLFloat, GraphQLEnumType } from 'graphql';
import {
GraphQLInt,
GraphQLScalarType,
GraphQLString,
GraphQLBoolean,
GraphQLFloat,
GraphQLEnumType,
GraphQLID
} from 'graphql';
import { EntityMetadata } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import { UniqueMetadata } from 'typeorm/metadata/UniqueMetadata';
Expand Down Expand Up @@ -187,13 +195,13 @@ export function entityToWhereInput(entity: EntityMetadata): string {

// TODO: for foreign key fields, only allow the same filters as ID below
// Example: photo.userId: String
if (column.isPrimary) {
if (column.isPrimary || graphqlType === GraphQLID) {
fieldTemplates += `
@Field({ nullable: true })
${column.propertyName}_eq?: ${tsType};
@Field(type => ${graphqlType},{ nullable: true })
${column.propertyName}_eq?: string;
@Field(type => [${graphqlType}], { nullable: true })
${column.propertyName}_in?: ${tsType}[];
${column.propertyName}_in?: string[];
`;
} else if (graphqlType === GraphQLString) {
// TODO: do we need NOT?
Expand Down Expand Up @@ -330,6 +338,8 @@ export function columnToGraphQLType(column: ColumnMetadata): GraphQLScalarType |
const enumObject = getMetadataStorage().getEnum(column.entityMetadata.name, column.propertyName);
if (enumObject) {
return enumObject.name;
} else if (column.propertyName.match(/Id$/)) {
return GraphQLID;
}

// Some types have a name attribute
Expand Down
4 changes: 0 additions & 4 deletions src/tgql/BaseResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,11 @@ export class BaseResolver<E extends BaseModel> {
return { id: where.id };
}

// TODO: boolean logic - AND, OR, NOT
// extends WhereInput
processWhereOptions<W extends any>(where: W) {
const whereOptions: { [key: string]: FindOperator<any> } = {};
Object.keys(where).forEach(k => {
const key = k as keyof W;
if (key === 'AND' || key === 'OR' || key === 'not') {
throw new Error('Boolean logic not yet supported');
}
const [attr, operator] = getFindOperator(String(key), where[key]);
whereOptions[attr] = operator;
});
Expand Down
12 changes: 6 additions & 6 deletions src/tgql/DeleteResponse.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Field, InterfaceType, ObjectType } from 'type-graphql';
import { Field, ID, InterfaceType, ObjectType } from 'type-graphql';

import { ID } from '../core';
import { IDType } from '../core';

// tslint:disable:max-classes-per-file

@InterfaceType()
export abstract class DeleteResponse {
@Field(type => String)
id!: ID;
@Field(type => ID)
id!: IDType;
}

@ObjectType()
export class StandardDeleteResponse {
@Field(type => String)
id!: ID;
@Field(type => ID)
id!: IDType;
}

// tslint:enable:max-classes-per-file

0 comments on commit ad28c60

Please sign in to comment.