Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sloweyyy/graphql-example
https://github.com/sloweyyy/graphql-example
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/sloweyyy/graphql-example
- Owner: sloweyyy
- License: mit
- Created: 2024-11-14T07:17:33.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-16T13:20:47.000Z (about 2 months ago)
- Last Synced: 2024-12-07T21:49:01.673Z (about 1 month ago)
- Language: TypeScript
- Size: 411 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL API for Quotes Management
This project is a GraphQL API for managing quotes and authors. It allows users to create, read, update, and delete quotes and authors through a flexible and efficient API.
## Table of Contents
- [Features](#features)
- [Technologies Used](#technologies-used)
- [Getting Started](#getting-started)
- [API Endpoints](#api-endpoints)
- [Testing](#testing)
- [License](#license)## Features
- Create, read, update, and delete authors.
- Create, read, update, and delete quotes.
- Query quotes with pagination and filtering options.
- Author information is linked to quotes.## Technologies Used
- Node.js
- Express.js
- GraphQL
- Knex.js
- MySQL
- Jest## Getting Started
### Prerequisites
- Node.js (v14 or higher)
- PostgreSQL (or your preferred SQL database)
- Yarn or npm### Installation
### 1. Clone the Repository:
```bash
git clone https://github.com/sloweyyy/graphql-example.git
cd graphql-example/Backend
```### 2. Install Dependencies:
Choose your preferred package manager:
```bash
# With npm:
npm install# Or with yarn:
yarn install
```### 3. Set Up Your Database:
- Create a MySQL database.
- Update the database connection settings in the `.env` file.### 4. Run Migrations and Seed the Database:
```bash
# With npm:
npm run migrate
npm run seed# Or with yarn:
yarn run migrate
yarn run seed
```### 5. Start the Server:
```bash
# With npm:
npm start# Or with yarn:
yarn start
```Alternatively, you can use Docker to build and run the project:
```bash
docker-compose up --build
```### 6. Start the Frontend:
```bash
cd Frontend
yarn install
yarn run relay
yarn start
```### Access URLs:
- **Server:** `http://localhost:3010/graphql`
- **Frontend:** `http://localhost:3000`
- **GraphQL Playground:** `http://localhost:3010/playground`## API Endpoints
#### Example Queries
Get list of authors (_it will return only first 10 authors!_):
```graphql
query {
authors {
edges {
node {
id
_id
firstName
lastName
}
}
}
}
```Filter authors based of first name, also return total number of such authors:
```graphql
query {
authors(firstName: "Robert") {
totalCount
edges {
node {
id
_id
firstName
lastName
}
}
}
}
```Order authors by first name and last name:
```graphql
query {
authors(
orderBy: [
{ field: FIRST_NAME, direction: ASC }
{ field: LAST_NAME, direction: ASC }
]
) {
edges {
cursor
node {
_id
firstName
lastName
}
}
}
}
```Get name of author with ID = 4:
```GraphQL
query {
author(id: 4) {
id
_id
firstName
lastName
}
}
```Get list of quotes:
```GraphQL
query {
quotes {
edges {
node {
id
_id
text
}
}
}
}
```Create new author:
```GraphQL
mutation {
createAuthor(input:{
firstName:"Phuc"
lastName:"Truong"
}) {
id
_id
firstName
lastName
}
}
```Update existing author:
```GraphQL
mutation {
updateAuthor(input:{
id: 1
firstName: "Tai"
lastName: "Ngu"
}) {
id
_id
firstName
lastName
}
}
```Create new quote:
```GraphQL
mutation {
createQuote(input: {
authorId: 1, // Replace with a valid author ID
text: "This is a new quote."
}) {
id
_id
text
author {
id
_id
firstName
lastName
}
}
}
```Update existing quote:
```GraphQL
mutation {
updateQuote(input: {
id: 1,
text: "Đây là một quote đã được cập nhật."
}) {
id
_id
text
author {
id
_id
firstName
lastName
}
}
}
```Delete an author:
```GraphQL
mutation {
deleteAuthor(id: 1) {
id
_id
firstName
lastName
}
}
```Delete a quote:
```GraphQL
mutation {
deleteQuote(id: 1) {
id
_id
text
author {
id
_id
firstName
lastName
}
}
}
```## Testing
To run the tests for the API, use the following command:
```bash
cd Backend
npm test
# or
yarn test
```## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.