Skip to content

Commit

Permalink
fix: user meta is not a function error (#462)
Browse files Browse the repository at this point in the history
* fix: update packages to resolve type errors

Update prime-core package.json to resolving typing errors between
@accounts/typeorm and typeorm which causes compilation to fail.

* fix: resolve user.meta is not a function

Resolve the user.meta is not a function error by making user.meta a
static function which takes an id as an argument due to entities not
inheriting their types methods.

* Update yarn.lock

* Update yarn.lock

Co-authored-by: Birkir Gudjonsson <birkir.gudjonsson@gmail.com>
  • Loading branch information
its-lucas and birkir authored Jul 17, 2020
1 parent 4ade023 commit 2e94286
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"link": "lerna exec -- yarn link",
"unlink": "lerna exec -- yarn unlink",
"clean": "lerna run clean && rimraf {.cache,node_modules,reports,packages/**/{node_modules,lib,build,coverage},yarn.lock}",
"compile": "lerna run compile",
"compile": "lerna run compile --stream",
"lint": "tslint ./packages/*/{src,__tests__}/*.{ts,tsx}",
"publish": "lerna publish prerelease",
"publish:canary": "lerna publish --canary",
Expand Down
14 changes: 8 additions & 6 deletions packages/prime-core/src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { UserMeta } from './UserMeta';
@Entity()
@ObjectType()
export class User extends AccountsUser {
@Field(type => ID)
public id = super.id;

public async meta(): Promise<UserMeta> {
public static async meta(id: string): Promise<UserMeta> {
const metaRepo = getRepository(UserMeta);
let meta = await metaRepo.findOne(this.id);
let meta = await metaRepo.findOne(id);

if (!meta) {
meta = new UserMeta();
meta.id = this.id;
meta.id = id;
await metaRepo.save(meta);
}

return meta;
}

@Field(type => ID)
public id = super.id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export class UserResolver {
@Authorized()
@Query(returns => User)
public async getUser(@Ctx() context: Context) {
const user = await this.userRepository.findOneOrFail(context.user.id);
const meta = await user.meta();
const meta = await User.meta(context.user.id);

return {
...context.user,
meta,
Expand All @@ -61,7 +61,7 @@ export class UserResolver {
});
(result as any).resolveNode = async user => {
user.emails = await this.userEmailRepository.find({ user });
const meta = await user.meta();
const meta = await User.meta(user.id);
return {
...user,
meta,
Expand Down Expand Up @@ -132,7 +132,7 @@ export class UserResolver {
@Ctx() context: Context
) {
const user = await this.userRepository.findOneOrFail(id);
const meta = await user.meta();
const meta = await User.meta(id);
meta.profile = input.profile;
ForbiddenError.from(context.ability).throwUnlessCan('update', user);
await this.userRepository.save(user);
Expand Down Expand Up @@ -172,7 +172,7 @@ export class UserResolver {
}

@FieldResolver(returns => UserMeta)
public async meta(@Root() user: User): Promise<UserMeta> {
return user.meta();
public meta(@Root() user: User): Promise<UserMeta> {
return User.meta(user.id);
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
lodash "^4.17.15"
reflect-metadata "^0.1.13"
tslib "1.11.2"
typeorm "^0.2.22"

"@accounts/types@^0.19.0":
version "0.19.0"
Expand Down Expand Up @@ -18836,6 +18835,7 @@ typeorm-typedi-extensions@^0.2.0:
resolved "https://registry.yarnpkg.com/typeorm-typedi-extensions/-/typeorm-typedi-extensions-0.2.3.tgz#94fca2656206d771bf6d2242f5aab570511188e8"
integrity sha512-T9i1NvRZNjPn9Jb8oT772ihfn6PwdqDVpzPCtKSqjkZGOgXrCkdyD3dDrzfMaoWJ1afU58bVx2CMb95FzT42Ow==


typeorm@0.2.25, typeorm@^0.2.21, typeorm@^0.2.22:
version "0.2.25"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.25.tgz#1a33513b375b78cc7740d2405202208b918d7dde"
Expand Down

0 comments on commit 2e94286

Please sign in to comment.