clean up, use inputs

This commit is contained in:
Moriz Wahl 2021-07-13 19:18:17 +02:00
parent a2f932f040
commit ae4edf4605
10 changed files with 62 additions and 188 deletions

View File

@ -17,6 +17,7 @@
"dependencies": {
"apollo-server-express": "^2.25.2",
"axios": "^0.21.1",
"class-validator": "^0.13.1",
"express": "^4.17.1",
"graphql": "^15.5.1",
"jsonwebtoken": "^8.5.1",

View File

@ -0,0 +1,20 @@
import axios from 'axios'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const apiPost = async (url: string, payload: unknown): Promise<any> => {
try {
const result = await axios.post(url, payload)
if (result.status !== 200) {
throw new Error('HTTP Status Error ' + result.status)
}
if (result.data.state === 'warning') {
return { success: true, result: result.data.errors }
}
if (result.data.state !== 'success') {
throw new Error(result.data.msg)
}
return { success: true, result }
} catch (error) {
return { success: false, result: error }
}
}

View File

@ -1,10 +0,0 @@
import { InputType, Field } from 'type-graphql'
@InputType()
export class CreateBookInput {
@Field()
title: string
@Field()
author: string
}

View File

@ -1,5 +1,6 @@
import { InputType, Field } from 'type-graphql'
import { ArgsType, Field } from 'type-graphql'
/*
@InputType()
export class LoginUserInput {
@Field({ nullable: true })
@ -14,3 +15,13 @@ export class LoginUserInput {
@Field()
password: string
}
*/
@ArgsType()
export class UnsecureLoginArgs {
@Field(() => String)
email: string
@Field(() => String)
password: string
}

View File

@ -1,13 +0,0 @@
import { InputType, Field } from 'type-graphql'
@InputType()
export class UpdateBookInput {
@Field({ nullable: true })
title?: string
@Field({ nullable: true })
author?: string
@Field({ nullable: true })
isPublished?: boolean
}

View File

@ -1,22 +0,0 @@
import { Entity, BaseEntity, PrimaryGeneratedColumn, Column } from 'typeorm'
import { ObjectType, Field, ID } from 'type-graphql'
@Entity()
@ObjectType()
export class Book extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: string
@Field(() => String)
@Column()
title: string
@Field(() => String)
@Column()
author: string
@Field(() => Boolean)
@Column({ default: false })
isPublished: boolean
}

View File

@ -1,62 +0,0 @@
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

@ -1,43 +0,0 @@
/*
import { Resolver, Query, Mutation, Arg } from 'type-graphql'
import { Book } from '../models/Book'
import { CreateBookInput } from '../inputs/CreateBookInput'
import { UpdateBookInput } from '../inputs/UpdateBookInput'
@Resolver()
export class BookResolver {
@Query(() => [Book])
books() {
return Book.find()
}
@Query(() => Book)
book(@Arg('id') id: string) {
return Book.findOne({ where: { id } })
}
@Mutation(() => Book)
async createBook(@Arg('data') data: CreateBookInput) {
const book = Book.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 deleteBook(@Arg('id') id: string) {
const book = await Book.findOne({ where: { id } })
if (!book) throw new Error('Book not found!')
await book.remove()
return true
}
}
*/

View File

@ -1,35 +1,12 @@
// import jwt from 'jsonwebtoken'
import axios from 'axios'
import { Resolver, Query, /* Mutation, */ Arg } from 'type-graphql'
import { Resolver, Query, /* Mutation, */ Args } from 'type-graphql'
import CONFIG from '../../config'
import { LoginResponse } from '../models/User'
// import { LoginUserInput } from '../inputs/LoginUserInput'
// import { loginAPI, LoginResult } from '../../apis/loginAPI'
import { UnsecureLoginArgs } from '../inputs/LoginUserInput'
import { apiPost } from '../../apis/loginAPI'
// import { CreateBookInput } from '../inputs/CreateBookInput'
// import { UpdateBookInput } from '../inputs/UpdateBookInput'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const apiPost = async (url: string, payload: unknown): Promise<any> => {
try {
// console.log(url, payload)
const result = await axios.post(url, payload)
// console.log('-----', result)
if (result.status !== 200) {
throw new Error('HTTP Status Error ' + result.status)
}
if (result.data.state === 'warning') {
return { success: true, result: result.data.errors }
}
if (result.data.state !== 'success') {
throw new Error(result.data.msg)
}
return { success: true, result }
} catch (error) {
// console.log(error)
return { success: false, result: error }
}
}
@Resolver()
export class UserResolver {
/* @Query(() => [User])
@ -44,7 +21,7 @@ export class UserResolver {
@Query(() => LoginResponse)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async login(@Arg('email') email: string, @Arg('password') password: string): Promise<any> {
async login(@Args() { email, password }: UnsecureLoginArgs): Promise<any> {
email = email.trim().toLowerCase()
const result = await apiPost(CONFIG.LOGIN_API_URL + 'unsecureLogin', { email, password })
@ -54,7 +31,6 @@ export class UserResolver {
}
// temporary solution until we have JWT implemented
// console.log(result.result.data)
return {
sessionId: result.result.data.session_id,
user: {
@ -88,16 +64,8 @@ export class UserResolver {
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 } })

View File

@ -389,6 +389,11 @@
"@types/mime" "^1"
"@types/node" "*"
"@types/validator@^13.1.3":
version "13.6.3"
resolved "https://registry.yarnpkg.com/@types/validator/-/validator-13.6.3.tgz#31ca2e997bf13a0fffca30a25747d5b9f7dbb7de"
integrity sha512-fWG42pMJOL4jKsDDZZREnXLjc3UE0R8LOJfARWYg6U966rxDT7TYejYzLnUF5cvSObGg34nd0+H2wHHU5Omdfw==
"@types/ws@^7.0.0":
version "7.4.5"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-7.4.5.tgz#8ff0f7efcd8fea19f51f9dd66cb8b498d172a752"
@ -982,6 +987,15 @@ ci-info@^2.0.0:
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
integrity sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==
class-validator@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.13.1.tgz#381b2001ee6b9e05afd133671fbdf760da7dec67"
integrity sha512-zWIeYFhUitvAHBwNhDdCRK09hWx+P0HUwFE8US8/CxFpMVzkUK8RJl7yOIE+BVu2lxyPNgeOaFv78tLE47jBIg==
dependencies:
"@types/validator" "^13.1.3"
libphonenumber-js "^1.9.7"
validator "^13.5.2"
cli-boxes@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/cli-boxes/-/cli-boxes-2.2.1.tgz#ddd5035d25094fce220e9cab40a45840a440318f"
@ -2283,6 +2297,11 @@ levn@^0.4.1:
prelude-ls "^1.2.1"
type-check "~0.4.0"
libphonenumber-js@^1.9.7:
version "1.9.22"
resolved "https://registry.yarnpkg.com/libphonenumber-js/-/libphonenumber-js-1.9.22.tgz#b6b460603dedbd58f2d71f15500f216d70850fad"
integrity sha512-nE0aF0wrNq09ewF36s9FVqRW73hmpw6cobVDlbexmsu1432LEfuN24BCudNuRx4t2rElSeK/N0JbedzRW/TC4A==
load-json-file@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-4.0.0.tgz#2f5f45ab91e33216234fd53adab668eb4ec0993b"
@ -3528,6 +3547,11 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"
validator@^13.5.2:
version "13.6.0"
resolved "https://registry.yarnpkg.com/validator/-/validator-13.6.0.tgz#1e71899c14cdc7b2068463cb24c1cc16f6ec7059"
integrity sha512-gVgKbdbHgtxpRyR8K0O6oFZPhhB5tT1jeEHZR0Znr9Svg03U0+r9DXWMrnRAB+HtCStDQKlaIZm42tVsVjqtjg==
vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"