Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/muthukumar89uk/go-with-graphql
https://github.com/muthukumar89uk/go-with-graphql
go go-graphql golang gorm gqlgen graphql mutations query
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/muthukumar89uk/go-with-graphql
- Owner: muthukumar89uk
- Created: 2024-07-26T11:19:50.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-08-01T08:03:44.000Z (6 months ago)
- Last Synced: 2024-08-01T09:38:04.043Z (6 months ago)
- Topics: go, go-graphql, golang, gorm, gqlgen, graphql, mutations, query
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go GraphQL CRUD Example
This repository demonstrates how to set up a Go project to perform CRUD (Create, Read, Update, Delete) operations using GraphQL.
## Features
- GraphQL server implementation in Go
- CRUD operations for a simple entity (e.g., User)
- Basic schema definition and resolvers## Requirements
- Go 1.15 or higher
- Docker (optional but recommended for setting up a database)## Getting Started
### Installation
1. **Clone the repository:**
```
git clone https://github.com/muthukumar89uk/go-with-graphql.git
```
Click here to directly [download it](https://github.com/muthukumar89uk/go-with-graphql/zipball/master).2. **Install Go dependencies:**
```sh
go mod tidy
```### Setup Database
1. **Locally:**Install PostgreSQL from the [official PostgreSQL website](https://www.postgresql.org/download/).
Create a database named `graphql` and a user with appropriate privileges.
### Generate GraphQL Folder Strucure
- For the generate graphql folder using the command:
```
go run github.com/99designs/gqlgen init
```### GraphQL Server
1. **Create the GraphQL schema:**
Create a `schema.graphql` file in your project directory with the following content:
```graphql
type User {
id: Int!
username: String!
email: String!
phone_number: String!
password: String!
}type DeleteStatus{
iserror: Boolean!
message: String
}input NewUser {
name: String!
email: String!
phoneNumber: String!
password: String!
}type Query {
GetAllUsers: [User!]!
GetOneUserById(id: Int!): User!
}type Mutation {
createUser(input: NewUser!): User!
updateUser(id: Int!, input: NewUser!): User!
deleteUser(id: Int!): DeleteStatus
}
```
2. **Create the resolver implementation:**Create a `graph` directory with `resolver.go` and the generated code:
`resolver.go`:
Run the following command to generate the necessary GraphQL code:
```sh
go run github.com/99designs/gqlgen generate
```Implement the CRUD operations in the generated resolver file (`graph/schema.resolvers.go`):
### Run the Application
1. **Run the GraphQL server:**
```sh
go run .
```2. **Access the GraphQL Playground:**
Open your browser and navigate to `http://localhost:8081/` to access the GraphQL Playground.