This project is a CRUD (Create, Read, Update, Delete) API built with Go, GraphQL, and PostgreSQL. It uses the gqlgen
library for GraphQL and go-pg
for PostgreSQL interaction. The API allows you to create and retrieve movies with a title, URL, and release date.
- Create a new movie with a title, URL, and automatically generated release date.
- Retrieve all movies.
- Go
- GraphQL
- PostgreSQL
- gqlgen
- go-pg
- godotenv
- Go (version 1.16+)
- PostgreSQL
go-pg
gqlgen
godotenv
-
Clone the repository:
git clone https://github.com/your-username/go-crud-graphql.git cd go-crud-graphql
-
Install dependencies:
go mod download
-
Create a PostgreSQL database:
CREATE DATABASE your_db_name;
-
Set up environment variables: Create a
.env
file in the root directory with the following content:DB_USER=your_db_user DB_PASSWORD=your_db_password DB_NAME=your_db_name DB_HOST=localhost DB_PORT=5432
-
Create the
movies
table:CREATE TABLE movies ( id SERIAL PRIMARY KEY, title TEXT NOT NULL, url TEXT NOT NULL, release_date TEXT NOT NULL );
-
Run the server:
go run server.go
Once the server is running, you can access the GraphQL playground at http://localhost:8080/
.
To create a new movie, use the following GraphQL mutation:
mutation createMovie {
createMovie(
input: {
title: "Rise of GraphQL Warrior Pt1"
url: "https://riseofgraphqlwarriorpt1.com/"
}
){
id
releaseDate
}
}
To update a movie, use the following GraphQL mutation:
mutation {
updateMovie(input: {
id: 7
title: "GraphQL in Action (Updated)"
}) {
id
title
url
releaseDate
}
}
To delete a movie, use the following GraphQL mutation:
mutation {
deleteMovie(id: 1)
}
To get all movie, use the following GraphQL mutation:
query {
movies {
id
title
url
releaseDate
}
}
To get movie by id, use the following GraphQL mutation:
query {
movie(id: 1) {
id
title
url
releaseDate
}
}