mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
book resolvers, working server, example orm config
This commit is contained in:
parent
43f7cf8767
commit
4c0667d870
6
backend/ormconfig.json
Normal file
6
backend/ormconfig.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"type": "sqlite",
|
||||||
|
"database": "./db.sqlite3",
|
||||||
|
"entities": ["./src/graphql/models/*.ts"],
|
||||||
|
"synchronize": true
|
||||||
|
}
|
||||||
10
backend/src/graphql/inputs/CreateBookInput.ts
Normal file
10
backend/src/graphql/inputs/CreateBookInput.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { InputType, Field } from 'type-graphql'
|
||||||
|
|
||||||
|
@InputType()
|
||||||
|
export class CreateBookInput {
|
||||||
|
@Field()
|
||||||
|
title: string;
|
||||||
|
|
||||||
|
@Field()
|
||||||
|
author: string;
|
||||||
|
}
|
||||||
13
backend/src/graphql/inputs/UpdateBookInput.ts
Normal file
13
backend/src/graphql/inputs/UpdateBookInput.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
22
backend/src/graphql/models/Book.ts
Normal file
22
backend/src/graphql/models/Book.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
@ -1 +0,0 @@
|
|||||||
export default []
|
|
||||||
41
backend/src/graphql/resolvers/BookResolver.ts
Normal file
41
backend/src/graphql/resolvers/BookResolver.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,15 +3,12 @@ import express from 'express'
|
|||||||
import { graphqlHTTP } from 'express-graphql'
|
import { graphqlHTTP } from 'express-graphql'
|
||||||
import { createConnection } from 'typeorm'
|
import { createConnection } from 'typeorm'
|
||||||
import { buildSchema } from 'type-graphql'
|
import { buildSchema } from 'type-graphql'
|
||||||
// import resolvers from './graphql/resolvers'
|
import { BookResolver } from "./graphql/resolvers/BookResolver"
|
||||||
const resolvers: [string] = ['']
|
|
||||||
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
|
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
//const connection = await createConnection()
|
const connection = await createConnection()
|
||||||
const schema = await buildSchema({
|
const schema = await buildSchema({ resolvers: [BookResolver]})
|
||||||
resolvers
|
|
||||||
})
|
|
||||||
const server = express();
|
const server = express();
|
||||||
|
|
||||||
server.use('/api',
|
server.use('/api',
|
||||||
@ -57,7 +54,7 @@ async function main() {
|
|||||||
// app.get("/playground", expressPlayground({ endpoint: "/graphql" }));
|
// app.get("/playground", expressPlayground({ endpoint: "/graphql" }));
|
||||||
server.listen(4000, () => {
|
server.listen(4000, () => {
|
||||||
console.log(
|
console.log(
|
||||||
`Server is running, GraphQL Playground available at http://localhost:4000/playground`,
|
`Server is running, GraphIQL available at http://localhost:4000/api`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,8 +30,7 @@
|
|||||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||||
// TODO: "strictPropertyInitialization": false
|
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
|
||||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
|
||||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user