Skip to content

Commit 464c3de

Browse files
authored
Feature: graphql server (#3)
* add promise / eager to relationships * add graphql support
1 parent a723471 commit 464c3de

File tree

15 files changed

+239
-30
lines changed

15 files changed

+239
-30
lines changed

package-lock.json

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828
"typescript": "^2.6.2"
2929
},
3030
"dependencies": {
31+
"@omega-bbs/spec": "0.0.6",
3132
"cookie-session": "^1.3.2",
3233
"debug": "^3.1.0",
3334
"es6-shim": "^0.35.3",
3435
"express": "^4.16.2",
36+
"express-graphql": "^0.6.11",
37+
"glob": "^7.1.2",
38+
"graphql": "^0.11.7",
3539
"jsonwebtoken": "^8.1.0",
3640
"lodash": "^4.17.4",
3741
"morgan": "^1.9.0",

src/entities/Board.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ export class Board {
2828
// relationships
2929
/** topics of the board */
3030
@OneToMany(type => Topic, topic => topic.board)
31-
public topics: Topic[]
31+
public topics: Promise<Topic[]>
3232
}

src/entities/Comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export class Comment {
2727

2828
// relationships
2929
@ManyToOne(type => User, user => user.comments)
30-
public author: User
30+
public author: Promise<User>
3131

3232
/** post of the comment belongs to */
3333
@ManyToOne(type => Post, post => post.comments)
34-
public post: Post
34+
public post: Promise<Post>
3535
}

src/entities/File.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ export class File {
4242

4343
// relationships
4444
@ManyToOne(type => User, user => user.files)
45-
public author: User
45+
public author: Promise<User>
4646

4747
/** get the user if this file using(used) as avatar */
4848
@ManyToOne(type => User, user => user.avatarFiles)
49-
public usedInUser: User
49+
public usedInUser: Promise<User>
5050

5151
/** get the post if this file using in a post */
5252
@ManyToOne(type => Post, post => post.files)
5353
@Index() // post.files
54-
public usedInPost: Post
54+
public usedInPost: Promise<Post>
5555

5656
// calculated fields
5757
/** get the public accessed url of the file (use the storageDriver and path) */

src/entities/Permission.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export class Permission {
2020

2121
// relationships
2222
@ManyToMany(type => Role, role => role.permissions)
23-
public roles: Role
23+
public roles: Promise<Role>
2424
}

src/entities/Post.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,25 @@ export class Post {
4242
// relationships
4343
@ManyToOne(type => User, user => user.posts)
4444
@Index() // user.posts
45-
public author: User
45+
public author: Promise<User>
4646

4747
@ManyToOne(type => Topic, topic => topic.posts)
4848
@Index() // topic.posts
49-
public topic: Topic
49+
public topic: Promise<Topic>
5050

5151
@OneToMany(type => Comment, comment => comment.post)
52-
public comments: Comment[]
52+
public comments: Promise<Comment[]>
5353

5454
/** files used in this post */
5555
@OneToMany(type => File, file => file.author)
56-
public files: File[]
56+
public files: Promise<File[]>
5757

5858
/** posts which referenced to this post */
5959
@ManyToMany(type => Post, post => post.postsReferencedBy)
6060
@JoinTable()
61-
public postsReferenced: Post[]
61+
public postsReferenced: Promise<Post[]>
6262

6363
/** posts which referenced by this post */
6464
@ManyToMany(type => Post, post => post.postsReferenced)
65-
public postsReferencedBy: Post[]
65+
public postsReferencedBy: Promise<Post[]>
6666
}

src/entities/Role.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export class Role {
1818

1919
// relationships
2020
@ManyToOne(type => User, user => user.role)
21-
public users: User[]
21+
public users: Promise<User[]>
2222

23-
@ManyToMany(type => Permission, permission => permission.roles)
23+
@ManyToMany(type => Permission, permission => permission.roles, {
24+
eager: true
25+
})
2426
@JoinTable()
2527
public permissions: Permission[]
2628
}

src/entities/Topic.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {
22
Column,
33
Entity,
44
Index,
5+
JoinColumn,
56
ManyToOne,
67
OneToMany,
8+
OneToOne,
79
PrimaryGeneratedColumn
810
} from 'typeorm'
911
import { Board } from './Board'
@@ -23,10 +25,14 @@ export class Topic {
2325
@Column() public deletedAt: Date
2426

2527
// relationships
28+
@OneToOne(type => Post, { eager: true })
29+
@JoinColumn()
30+
public rootPost: Post
31+
2632
@ManyToOne(type => Board, board => board.topics)
2733
@Index() // board.topics
28-
public board: Board
34+
public board: Promise<Board>
2935

3036
@OneToMany(type => Post, post => post.topic)
31-
public posts: Post[]
37+
public posts: Promise<Post[]>
3238
}

src/entities/User.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ export class User {
4040

4141
// relationships
4242
@OneToMany(type => File, file => file.usedInUser)
43-
public avatarFiles: File[]
43+
public avatarFiles: Promise<File[]>
4444

4545
@OneToMany(type => File, file => file.author)
46-
public files: File[]
46+
public files: Promise<File[]>
4747

4848
@OneToMany(type => Post, post => post.author)
49-
public posts: Post[]
49+
public posts: Promise<Post[]>
5050

5151
@OneToMany(type => Comment, comment => comment.author)
52-
public comments: Comment[]
52+
public comments: Promise<Comment[]>
5353

5454
@OneToOne(type => File)
5555
@JoinColumn()
56-
public currentAvatarFile: File
56+
public currentAvatarFile: Promise<File>
5757

5858
@ManyToOne(type => Role, role => role.users)
59-
public role: Role
59+
public role: Promise<Role>
6060

6161
// calculated fileds
6262
/** get user's current avatar public url */
63-
public getAvatarUrl(): string {
63+
public async getAvatarUrl(): Promise<string> {
6464
const methods = {
6565
[UserAvatarType.file]: this.getAvatarUrlFile,
6666
[UserAvatarType.gravatar]: this.getAvatarUrlGravatar,
@@ -69,15 +69,15 @@ export class User {
6969
return methods[this.avatarType].call(this)
7070
}
7171

72-
private getAvatarUrlGravatar(): string {
73-
return 'https://gravatar.com'
72+
private getAvatarUrlGravatar(): Promise<string> {
73+
return Promise.resolve('https://gravatar.com')
7474
}
7575

76-
private getAvatarUrlFile(): string {
77-
return this.currentAvatarFile.getUrl()
76+
private async getAvatarUrlFile(): Promise<string> {
77+
return (await this.currentAvatarFile).getUrl()
7878
}
7979

80-
private getAvatarUrlUnset(): string {
80+
private async getAvatarUrlUnset(): Promise<string> {
8181
return 'about:blank'
8282
}
8383
}

0 commit comments

Comments
 (0)