52 Deploying graphql-go to Render/Vercel/Heroku
52 Deploying graphql-go to Render/Vercel/Heroku
As a backend engineer, I often get asked: “If you’re using Go and the graphql-go framework, is it actually easy to deploy to cloud providers like Render, Vercel, or Heroku?” The short answer is: It’s easy, as long as you understand how it works! In this article, I’ll walk you through deploying a Go-based GraphQL service (graphql-go) to three popular platforms: Render, Vercel, and Heroku. I’ll share code examples, scenario simulations, and even deployment flow diagrams.
Setting Up a GraphQL Service with graphql-go
Before we get into deployment, we need to have a simple GraphQL service in place first. Let’s start with the most fundamental piece.
Project Structure
1graphql-demo/
2├── main.go
3├── go.mod
4├── schema.graphqlThe schema.graphql File
1type Query {
2 hello: String!
3}The main.go File
1package main
2
3import (
4 "context"
5 "log"
6 "net/http"
7 "github.com/graphql-go/graphql"
8 "github.com/graphql-go/handler"
9)
10
11func main() {
12 fields := graphql.Fields{
13 "hello": &graphql.Field{
14 Type: graphql.String,
15 Resolve: func(p graphql.ResolveParams) (interface{}, error) {
16 return "Hello world!", nil
17 },
18 },
19 }
20 rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
21 schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
22 schema, err := graphql.NewSchema(schemaConfig)
23 if err != nil {
24 log.Fatalf("failed to create schema, error: %v", err)
25 }
26
27 h := handler.New(&handler.Config{
28 Schema: &schema,
29 Pretty: true,
30 GraphiQL: true,
31 })
32
33 port := getPort()
34 http.Handle("/graphql", h)
35 log.Printf("Server is running on port %s...", port)
36 log.Fatal(http.ListenAndServe(":"+port, nil))
37}
38
39// Get the port from the environment variable, defaulting to 8080.
40func getPort() string {
41 port := os.Getenv("PORT")
42 if port == "" {
43 port = "8080"
44 }
45 return port
46}go mod init graphql-demo and installed the two main packages:go get github.com/graphql-go/graphqlgo get github.com/graphql-go/handler1. Deploying to Render
Render is a fairly “developer friendly” cloud platform that’s very popular in the Go community. Render automatically builds from the source code in your repository (GitHub/GitLab) and runs your commands.
How Deploying to Render Works
flowchart TD
A[Push ke repo Github] --> B[Connect repo ke Render Web Service]
B --> C[Render build Go]
C --> D[Service jalan di subdomain Render]
Deployment Walkthrough
- Push your code to a GitHub repo.
- Sign up/log in to Render .
- Select
New Web Service→ connect to your repo. - Fill in the form:
- Environment: Go
- Build Command: leave it empty (Render automatically runs
go build) - Start Command:
./graphql-demo(this name matches the binary produced by the build)
- Render’s default port: the PORT variable (which we’ve already handled in
main.go). - Click deploy.
Deployment Configuration Table
| Field | Value |
|---|---|
| Runtime | Go |
| Build Command | (leave empty) |
| Start Command | ./graphql-demo |
| Port env | PORT (default:8080) |
| Service type | Web Service |
Testing:
Visit the URL https://your-service-name.onrender.com/graphql, open GraphiQL, and try the query:
1query {
2 hello
3}2. Deploying to Heroku
Heroku
is still a favorite too, especially for prototyping. This time, we need to create a Procfile in the project root:
The Procfile
1web: ./graphql-demoHeroku Deployment Steps
- Install the Heroku CLI (if you don’t have it yet).
heroku create graphql-go-demo- Set
GOFLAGSso the build is static:1heroku config:set GOFLAGS="-tags heroku" - The Procfile instructs Heroku to run the binary produced by the build.
- Run:
1git push heroku main - Wait for the build to finish – Heroku will automatically build and run the app.
Make sure the port uses the PORT env variable!
Query the endpoint at:https://graphql-go-demo.herokuapp.com/graphql
Troubleshooting
Sometimes Heroku refuses to run a Go binary if it isn’t static. If you hit an error, use a custom build command in a Makefile:
1build:
2 CGO_ENABLED=0 GOOS=linux go build -o graphql-demo main.go
Update the Procfile if necessary.
3. Deploying to Vercel
Vercel was originally known for frontend work (Next.js), but it can now run serverless Go too!
Creating an API Function on Vercel
The folder structure must follow Vercel’s convention:
1/api/graphql/main.goFill in api/graphql/main.go using an HTTP handler, but make use of the vercel/go-api runtime
:
1package main
2
3import (
4 "context"
5 "github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
6 "github.com/graphql-go/graphql"
7 "github.com/graphql-go/handler"
8 "github.com/vercel-community/go-api/api"
9)
10
11func main() {
12 fields := graphql.Fields{
13 "hello": &graphql.Field{
14 Type: graphql.String,
15 Resolve: func(p graphql.ResolveParams) (interface{}, error) {
16 return "Hello from Vercel!", nil
17 },
18 },
19 }
20 rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
21 schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
22 schema, _ := graphql.NewSchema(schemaConfig)
23
24 h := handler.New(&handler.Config{
25 Schema: &schema,
26 Pretty: true,
27 GraphiQL: true,
28 })
29
30 httpHandler := httpadapter.NewV2(h)
31 api.HandleFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
32 httpHandler.ServeHTTP(w, r)
33 })
34}The vercel.json File
1{
2 "version": 2,
3 "builds": [
4 { "src": "api/graphql/main.go", "use": "@vercel/go" }
5 ]
6}How to Deploy
npm i -g vercel(install the CLI).vercel loginvercel(follow the wizard).
The API endpoint will be available at:https://your-project-name.vercel.app/api/graphql
Platform Comparison
| Feature | Render | Heroku | Vercel (Serverless) |
|---|---|---|---|
| Free tier | Yes | Yes* | Yes |
| Build process | Automatic | Automatic | Automatic |
| Port configuration | Easy | Easy | Automatic |
| Persistent | Yes | Yes | Stateless, cold start |
| Scalability | Easy | Easy | Automated |
| GraphiQL support | Yes | Yes | Yes |
When Should You Pick Which?
- Render: Production-ready Go projects where you want full control and easy deployment.
- Heroku: Quick prototyping with easy database integration.
- Vercel: Serverless needs—stateless API functions.
Conclusion
Deploying graphql-go to a cloud platform turns out to be very straightforward, as long as we understand the build and execution patterns of each provider. The key is to: handle the PORT environment variable, provide a startup file (Procfile/vercel.json), and take advantage of continuous deploy features.
Go ahead and experiment, then adapt the workflow to your needs—hopefully you won’t hesitate to deploy a Go backend to a modern cloud anymore!
Main References:
See you in the next article in this series, number 53!