add User Model and Resolver

This commit is contained in:
einhornimmond 2021-06-24 10:17:09 +02:00 committed by Ulf Gebhardt
parent 34b30b216b
commit 9cd14d1304
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
6 changed files with 139 additions and 0 deletions

2
backend/.env.dist Normal file
View File

@ -0,0 +1,2 @@
LOGIN_API_URL=http://localhost/login_api/
COMMUNITY_API_URL=http://localhost/api/

View File

@ -12,6 +12,7 @@
"lint": "eslint . --ext .js,.ts"
},
"dependencies": {
"apollo-datasource-rest": "^0.14.0",
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^15.5.1",

View File

@ -0,0 +1,24 @@
// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env)
// Load Package Details for some default values
const pkg = require('../../package')
const environment = {
NODE_ENV: process.env.NODE_ENV,
DEBUG: process.env.NODE_ENV !== 'production' || false,
PRODUCTION: process.env.NODE_ENV === 'production' || false,
ALLOW_REGISTER: process.env.ALLOW_REGISTER !== 'false',
}
const server = {
LOGIN_API_URL: process.env.LOGIN_API_URL || 'http://localhost/login_api/',
COMMUNITY_API_URL: process.env.COMMUNITY_API_URL || 'http://localhost/api/',
}
const CONFIG = {
...environment,
...server,
APP_VERSION: pkg.version,
}
export default CONFIG

View File

@ -0,0 +1,8 @@
import { RESTDataSource } from 'apollo-datasource-rest'
export class LoginServerAPI extends RESTDataSource {
constructor() {
super()
this.baseURL = 'https://api.spacexdata.com/v2/'
}
}

View File

@ -0,0 +1,62 @@
import { Entity, BaseEntity, PrimaryGeneratedColumn, Column } from 'typeorm'
import { ObjectType, Field, ID, GraphQLISODateTime } from 'type-graphql'
@Entity()
@ObjectType()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number
@Field(() => String)
@Column({ length: 191 })
email: string
@Field(() => String)
@Column({ length: 150 })
firstName: string
@Field(() => String)
@Column()
lastName: string
@Field(() => String)
@Column()
username: string
@Field(() => String)
@Column('text')
description: string
@Field(() => String)
@Column({ length: 64 })
pubkey: string
@Field(() => GraphQLISODateTime)
@Column({ type: 'datetime' })
created: Date
@Field(() => Boolean)
@Column({ default: false })
emailChecked: boolean
@Field(() => Boolean)
@Column({ default: false })
passphraseShown: boolean
@Field(() => String)
@Column({ default: 'de' })
language: string
@Field(() => Boolean)
@Column({ default: false })
disabled: boolean
@Field(() => ID)
@Column()
groupId: number
@Field(() => ID)
@Column({ default: 0 })
publisherId: number
}

View File

@ -0,0 +1,42 @@
import { Resolver, Query, Mutation, Arg } from 'type-graphql'
import { User } from '../models/User'
// import { CreateBookInput } from '../inputs/CreateBookInput'
// import { UpdateBookInput } from '../inputs/UpdateBookInput'
@Resolver()
export class UserResolver {
@Query(() => [User])
users(): Promise<User[]> {
return User.find()
}
@Query(() => User)
user(@Arg('id') id: string): Promise<User | undefined> {
return User.findOne({ where: { id } })
}
/*
@Mutation(() => User)
async createBook(@Arg('data') data: CreateBookInput) {
const book = User.create(data)
await book.save()
return book
}
@Mutation(() => Book)
async updateBook(@Arg('id') id: string, @Arg('data') data: UpdateBookInput) {
const book = await Book.findOne({ where: { id } })
if (!book) throw new Error('Book not found!')
Object.assign(book, data)
await book.save()
return book
}
*/
@Mutation(() => Boolean)
async deleteUser(@Arg('id') id: string): Promise<boolean> {
const user = await User.findOne({ where: { id } })
if (!user) throw new Error('User not found!')
await user.remove()
return true
}
}