This content originally appeared on Level Up Coding - Medium and was authored by Rahul Reddy
Introduction
This is a simple Todo application developed in Golang using GraphQL. This tutorial helps you to find the right way for building your own GraphQL Server implementation in Go.
Click here to access the full source code
Click here to access the Postman collections.
In this tutorial, we will focus mainly on 3 things
- It’s beginner friendly.
- Focused on industry best practices.
- Deploy to the cloud.
The scope of this tutorial is to focus mostly on building graphQL servers rather than Go basics. If you’re completely new to Golang, I would highly encourage you to have a strong knowledge of Go basics before going into this article.
What is GraphQL?
GraphQL is created by Facebook, implemented in their mobile app in 2012 and open-sourced in 2015.
GraphQL is a query language and server-side runtime for APIs. GraphQL provides a flexible and intuitive syntax that enables clients the power to ask for exactly what they need and nothing more, making it easier to evolve APIs over time.
As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call. Therefore reducing the network calls and bandwidth saves the battery life and CPU cycles consumed by the backend applications (Official source).
Additionally, GraphQL gives API maintainers the flexibility to add or deprecate fields without impacting existing queries.
GraphQL is rapidly becoming the standard for API-based data access.
What are Schemas?
API developers create GraphQL schema that describes all the possible data that clients can query through that service. GraphQL schema is made up of object types, which defines the kind of object you can request and the fields it has.
The most common operations on any GraphQL APIs are Queries and Mutations. Queries is used for reading data from APIs. Mutations are used for Create, Update and Delete operations.
Each operation in GraphQL schema is attached to a function called resolvers. A resolver is used to perform the actual execution of Query or Mutation.
Live Demo
Use this endpoint to perform live test
Develop a GraphQL Server in Go
- Open your favorite code editor and create a go module using this go mod init github.com/rahul-yr/learn-go-graphql command.
- Install the below modules.
- Create a folder named todo in the root directory and 3 files named todo/models.go, todo/schema.go and todo/udf.go
- Create a Todo model in the todo/models.go file. This model represents the todo item.
Implement CRUD operations.
- Let’s create the CRUD operations for the TODO model(todo/udf.go).
- The above snippet has 5 functions defined 2 for Queries and 3 for Mutations. As well as a variable to store all the todo items(you could use a database instance here).
- GetTodos() method is used for fetching all the todo items.
- GetTodo(id int) method is used for fetching a todo item based on item id.
- AddTodo(title string) method is used for creating a new todo item.
- UpdateTodo(id int, title string, completed bool) method is used for updating the existing todo item based on item id.
- DeleteTodo(id int) method is used for deleting the todo item based on item id.
Implement GraphQL schema
- Now it’s finally time to create the GraphQL schema (todo/schema.go).
- GraphQL schema is implemented using this graphql.NewSchema(graphql.SchemaConfig{...}) method. This method actually resolves the GraphQL requests.
- You could define Queries,Mutations and other object types using this graphql.NewObject(graphql.Objectconfig{...}) method.
- This graphql.Fields{...} method is useful for defining fields.
- The below method is used for declaring input arguments.
graphql.FieldConfigArgument{
"some_id": &graphql.ArgumentConfig{
Type: graphql.SomeDataType,
},
}
- Resolve block is where actual execution happens.
Implement an endpoint for GraphQL
- Create a route handler(graph/router.go) file.
- Here I have used the gin package. You could use any http package of your choice for creating endpoints.
- The most important part here is to add this graphql.Do(...) method. This actually resolves the graphql request.
Run the server
- Create a main.go file for running the graphql server.
- Run the server using go run . command
- You can find the live postman API collection here. Refer to this for API specifications.
- Below are a few snapshots of API requests and responses in action using Postman.
Create a Todo item
Update a Todo item
Read a Todo item based on item id
Read all Todo items
Delete a Todo item
Deploy to Cloud
Know how to deploy this repo to Heroku? Click here to find out the right way
Summary
Awesome 🔥, you have successfully completed this tutorial. I would 💝 to hear your feedback and comments on the great things you’re gonna build with this. If you are stuck somewhere feel free to comment. I am always available. Please find the complete code at github
Level Up Coding
Thanks for being a part of our community! More content in the Level Up Coding publication.
Follow: Twitter, LinkedIn, Newsletter
Level Up is transforming tech recruiting ➡️ Join our talent collective
Develop a Todo GraphQL Server in Golang 🔥 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Rahul Reddy
Rahul Reddy | Sciencx (2022-07-05T15:19:25+00:00) Develop a Todo GraphQL Server in Golang. Retrieved from https://www.scien.cx/2022/07/05/develop-a-todo-graphql-server-in-golang/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.