Book review GraphQL API built using Node.js, TypeScript, Prisma, and GraphQL Yoga. This API allows you to manage users, reviews, and books.
To set up the API locally, follow these steps:
- Install dependencies:
pnpm install
- Host postgresql database. For local development you can use docker and included
docker-compose.yml
file:
docker-compose up -d
- Fill
.env
file, example:
DATABASE_URL="postgresql://admin:password@localhost:5432/posts"
JWT_SECRET="secret"
PORT=4000
TOKEN_EXPIRE_TIME="1d"
- Apply migrations to create the database schema:
npx prisma migrate dev
- Start the server:
npm run start:dev
The GraphQL schema defines the types, queries, and mutations available in the API. Here's an overview of the main types:
User
: Represents a user with their name, email, and reviews.Book
: Represents a book with its title, author, ISBN, and reviews.Review
: Represents a review with its text, rating, author, and book.
Retrieve the profile of the currently authenticated user.
query {
profile {
id
name
email
reviews {
id
text
rating
book {
id
title
}
}
}
}
Retrieve a list of users based on the provided query.
query {
users(query: "example") {
id
name
email
reviews {
id
text
rating
book {
id
title
}
}
}
}
Retrieve a list of books based on the provided query, skip, and take parameters.
query {
books(query: "example", skip: 0, take: 10) {
id
title
author
reviews {
id
text
rating
author {
id
name
}
}
}
}
Create a new user.
mutation {
createUser(data: {
name: "John Doe"
email: "[email protected]"
password: "secretpassword"
}) {
id
name
email
}
}
Authenticate a user and retrieve their profile along with a token.
mutation {
login(email: "[email protected]", password: "secretpassword") {
user {
id
name
email
}
token
}
}
Create a new book.
mutation {
createBook(title: "Sample Book", author: "Author Name", isbn: "123456789") {
id
title
author
isbn
}
}
Create a new review for a book.
mutation {
createReview(text: "Great book!", rating: 5, bookId: '123') {
id
text
rating
book {
id
title
author
isbn
}
}
}
The API uses token-based authentication. To authenticate, use the login
mutation to obtain a token. Include the token in the Authorization
header of your requests with the format: Bearer <token>
.