Skip to content

Commit

Permalink
fix(base-resolver): fix update bug (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 20, 2019
1 parent d04d805 commit 9df34ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/1-simple-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "npm-run-all --parallel start:ts playground:open",
"start": "DEBUG=warthog* run-p start:ts playground:open",
"start:ts": "ts-node-dev --type-check src/index.ts",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
Expand Down
11 changes: 8 additions & 3 deletions src/tgql/BaseResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ export class BaseResolver<E extends BaseModel> {

// TODO: fix - W extends Partial<E>
async findOne<W extends { [key: string]: any }>(where: W): Promise<E> {
where.deletedAt_eq = null; // Filter out soft-deleted items
const items = await this.find<W>({ id: where.id });
if (!items.length) {
throw new Error(`Unable to find ${this.entityClass.name} where ${JSON.stringify(where)}`);
} else if (items.length > 1) {
throw new Error(`Found ${items.length} ${this.entityClass.name}s where ${JSON.stringify(where)}`);
}

return this.repository.findOneOrFail(where);
return items[0];
}

async create(data: DeepPartial<E>, userId: string): Promise<E> {
Expand Down Expand Up @@ -134,7 +139,7 @@ export class BaseResolver<E extends BaseModel> {
(data as any).updatedById = userId; // TODO: fix any

// const whereOptions = this.pullParamsFromClass(where);
const found = await this.repository.findOneOrFail(where);
const found = await this.findOne(where);
const idData = ({ id: found.id } as any) as DeepPartial<E>;
const merged = this.repository.merge(new this.entityClass(), data, idData);

Expand Down

0 comments on commit 9df34ad

Please sign in to comment.