What are the main steps to connect a Go application to Hygraph?
To connect a Go application to Hygraph, you should: 1) Initialize your Go module (e.g., go mod init using-golang), 2) Install the github.com/machinebox/graphql client package, 3) Set up a GraphQL client pointing to your Hygraph API endpoint, 4) Define Go structs matching your Hygraph schema, 5) Write functions to query Hygraph and handle responses, and 6) Run your Go program to fetch and display data. The article provides a full code example and links to the GitHub repository for reference.
Which Go package is recommended for making GraphQL requests to Hygraph?
The article recommends using the github.com/machinebox/graphql package, authored by Mat Ryer and Machinebox, for making GraphQL requests from Go to Hygraph. This package simplifies sending queries and handling responses in Go applications.
Do I need authentication to query the sample Hygraph endpoint in the tutorial?
No authentication is required to query the sample Hygraph endpoint provided in the tutorial (https://api-eu-central-1.hygraph.com/v2/ck8sn5tnf01gc01z89dbc7s0o/master). This endpoint is open for public querying, making it suitable for learning and experimentation.
How should I structure my Go types to match Hygraph's GraphQL schema?
You should define Go structs that mirror the fields in your Hygraph GraphQL schema. For example, if your Hygraph schema has a Product type with name, description, and price fields, your Go struct should have corresponding fields with appropriate types and JSON tags. This ensures that the GraphQL response can be correctly unmarshaled into your Go types.
Where can I find the complete Go code example for connecting to Hygraph?
The complete Go code example for connecting to Hygraph, as shown in the article, is available on GitHub at https://github.com/hygraph/hygraph-examples/tree/master/with-golang. This repository contains all the files and instructions needed to replicate the tutorial steps.
As more people use "Hygraph as a Database", we'll explore how to query Hygraph inside of your Go program.
Last updated by Jamie
on Jun 12, 2026
Originally written by Jamie
on Jun 25, 2021
Go is an open-source programming language that has seen significant adoption over the last few years and has become the backbone to many banks, businesses, and API platforms across the world.
As more people use "Hygraph as a Database", just like we saw with working with Express.js, we can query Hygraph just like we would any other database. Go has no opinion on where your data comes from, just as long as you structure your data access the right way.
We'll use Go modules, and since I don't plan to use this as a package I can share with others, I'll just name it with-golang.
Inside there you'll want to declare the main package, import our GraphQL client module we just installed, and define a client variable.
ℹ️ If you're using Visual Studio Code, you can install the Go extension, which will automatically format (fmt), and do its best to manage your imports as and when you use them.
package main
import"github.com/machinebox/graphql"
var client *graphql.Client
funcmain(){
// ...
}
Let's now create a function for setupClient() and assign it to the client variable.
ℹ️ The endpoint above is open to all for querying, so no authentication is required.
Before we can use the client to make a NewRequest to Hygraph, it's best we define a struct for our Product, and Response.
You don't need to do this, but Go would much prefer you to. 😉
The first struct for Product will contain our name, description, and price fields we have in our Hygraph schema. We'll assign these to string, string, and int respectively.
While we're at it, we'll specify the json format of these too.
type Product struct{
Name string`json:"name"`
Description string`json:"description"`
Price int`json:"price"`
}
Next, let's define the ProductResponse, you could call this QueryResponse but this will define the shape of the response from Hygraph. This must match our GraphQL query.
type ProductResponse struct{
Products []Product `json:"products"`
}
ℹ️ You might want to format and account for any errors here too.
Let's now create a function getAllProducts() where we will make our request to client.
Our query must match the struct of Product and ProductResponse above.
funcgetAllProducts(){
query := graphql.NewRequest(`
{
products {
name
description
price
}
}
`)
ctx := context.Background()
var responseData ProductResponse
if err := client.Run(ctx, query,&responseData); err !=nil{
log.Fatal(err)
}
}
You could define your query outside of this and pass it into a new function, and instead of using context you could use the context of a http request if you ran this code as an HTTP handler.
But for now, let's add a bit more to this function to print our response as JSON.
I hope you found this useful, and to learn more about using Go with Hygraph, I recommend you check the README on GitHub for github.com/machinebox/graphql.
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.
As more people use "Hygraph as a Database", we'll explore how to query Hygraph inside of your Go program.
Last updated by Jamie
on Jun 12, 2026
Originally written by Jamie
on Jun 25, 2021
Go is an open-source programming language that has seen significant adoption over the last few years and has become the backbone to many banks, businesses, and API platforms across the world.
As more people use "Hygraph as a Database", just like we saw with working with Express.js, we can query Hygraph just like we would any other database. Go has no opinion on where your data comes from, just as long as you structure your data access the right way.
We'll use Go modules, and since I don't plan to use this as a package I can share with others, I'll just name it with-golang.
Inside there you'll want to declare the main package, import our GraphQL client module we just installed, and define a client variable.
ℹ️ If you're using Visual Studio Code, you can install the Go extension, which will automatically format (fmt), and do its best to manage your imports as and when you use them.
package main
import"github.com/machinebox/graphql"
var client *graphql.Client
funcmain(){
// ...
}
Let's now create a function for setupClient() and assign it to the client variable.
ℹ️ The endpoint above is open to all for querying, so no authentication is required.
Before we can use the client to make a NewRequest to Hygraph, it's best we define a struct for our Product, and Response.
You don't need to do this, but Go would much prefer you to. 😉
The first struct for Product will contain our name, description, and price fields we have in our Hygraph schema. We'll assign these to string, string, and int respectively.
While we're at it, we'll specify the json format of these too.
type Product struct{
Name string`json:"name"`
Description string`json:"description"`
Price int`json:"price"`
}
Next, let's define the ProductResponse, you could call this QueryResponse but this will define the shape of the response from Hygraph. This must match our GraphQL query.
type ProductResponse struct{
Products []Product `json:"products"`
}
ℹ️ You might want to format and account for any errors here too.
Let's now create a function getAllProducts() where we will make our request to client.
Our query must match the struct of Product and ProductResponse above.
funcgetAllProducts(){
query := graphql.NewRequest(`
{
products {
name
description
price
}
}
`)
ctx := context.Background()
var responseData ProductResponse
if err := client.Run(ctx, query,&responseData); err !=nil{
log.Fatal(err)
}
}
You could define your query outside of this and pass it into a new function, and instead of using context you could use the context of a http request if you ran this code as an HTTP handler.
But for now, let's add a bit more to this function to print our response as JSON.
I hope you found this useful, and to learn more about using Go with Hygraph, I recommend you check the README on GitHub for github.com/machinebox/graphql.
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.