Last active
May 19, 2018 23:03
-
-
Save dupski/5d49128c012af0c62cf20601a8afd3b1 to your computer and use it in GitHub Desktop.
Create a GraphQL in 5 minutes - models.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as rev from 'rev-models'; | |
export class User { | |
@rev.AutoNumberField({ primaryKey: true }) | |
id: number; | |
@rev.EmailField() | |
email: string; | |
@rev.TextField() | |
full_name: string; | |
@rev.RelatedModelList({ model: 'Post', field: 'user' }) | |
posts: Post[]; | |
constructor(data?: Partial<User>) { | |
Object.assign(this, data); | |
} | |
} | |
export class Post { | |
@rev.AutoNumberField({ primaryKey: true }) | |
id: number; | |
@rev.DateField() | |
post_date: string; | |
@rev.TextField({ multiLine: true, maxLength: 2000 }) | |
body: string; | |
@rev.RelatedModel({ model: 'User' }) | |
user: User; | |
constructor(data?: Partial<Post>) { | |
Object.assign(this, data); | |
} | |
validate(ctx: rev.IValidationContext) { | |
if (this.body.includes('fake news')) { | |
ctx.result.addFieldError('body', 'Post must not include fake news!'); | |
} | |
} | |
} | |
export class Comment { | |
@rev.AutoNumberField({ primaryKey: true }) | |
id: number; | |
@rev.DateField() | |
comment_date: string; | |
@rev.TextField({ multiLine: true, maxLength: 500 }) | |
comment: string; | |
@rev.RelatedModel({ model: 'User' }) | |
user: User; | |
constructor(data?: Partial<Comment>) { | |
Object.assign(this, data); | |
} | |
} | |
export const modelManager = new rev.ModelManager(); | |
modelManager.registerBackend('default', new rev.InMemoryBackend()) | |
modelManager.register(User); | |
modelManager.register(Post); | |
modelManager.register(Comment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment