Here's a quick summary of everything we released in Q1 2024.

Headless CMS for Go

Hygraph is the ideal Headless CMS for Go websites and applications. Read further to learn how our API-first CMS allows you to add components to your Go apps in minutes and enable your website's content to be managed from a powerful CMS.

Step #1 - Construct your query and fetch the data from Hygraph

To fetch data from a GraphQL API in Go, we use a GraphQL client library to establish a connection to the API endpoint. We construct a query string with the required fields, wrap it in a request, and include an authorization header for secure access.

Then we define a Go struct to map the expected JSON response. By executing the client's Run method with our query, the response populates our struct, and we can handle any errors that occur. This process efficiently retrieves the desired data ready for use in our application.

package main
import (
"context"
"fmt"
"log"
"github.com/machinebox/graphql"
)
func main() {
// Create a client (safe to share across requests)
client := graphql.NewClient("https://api-<region>.hygraph.com/v2/<some hash>/master")
// Make a request
req := graphql.NewRequest(`
{
products {
name
description
slug
availability
imageUrl
}
}
`)
// Set any variables
// req.Var("key", "value")
// Set header fields
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Authorization", "Bearer YOUR_HYGRAPH_TOKEN")
// Create a context
ctx := context.Background()
// Define a struct to receive the response
var responseData struct {
Products []struct {
Name string `json:"name"`
Description string `json:"description"`
Slug string `json:"slug"`
Availability bool `json:"availability"`
ImageUrl string `json:"imageUrl"`
} `json:"products"`
}
// Run the query with a context and pointer to the response data
if err := client.Run(ctx, req, &responseData); err != nil {
log.Fatal(err)
}
// Use the response data
for _, product := range responseData.Products {
fmt.Printf("Product: %s, Available: %t\n", product.Name, product.Availability)
}
}

Step #2 - Working with mutations - store data to headless CMS

The Go code for a GraphQL mutation sets up a client, constructs a request with an authorization header, and sends a createProduct mutation with product details. The server's response, which includes the new product's information, is then captured in a structured format, ready for use within the Go application.

This approach simplifies creating new entries in the database through GraphQL, avoiding manual HTTP and JSON handling.

package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/machinebox/graphql"
)
type CreateProductResponse struct {
CreateProduct Product `json:"createProduct"`
}
type Product struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
ImageURL string `json:"imageUrl"`
Slug string `json:"slug"`
Availability bool `json:"availability"`
}
func main() {
client := graphql.NewClient("https://management.hygraph.com/graphql")
// Define your query.
mutation := `
mutation ($product: ProductInput!) {
createProduct(input: $product) {
id
name
description
imageUrl
slug
availability
}
}
`
// Set up authorization.
req := graphql.NewRequest(mutation)
req.Header.Set("Authorization", "Bearer "+YOUR_HYGRAPH_TOKEN)
// Construct variables.
product := Product{
Name: "New Product",
Description: "This is a new product.",
ImageURL: "http://example.com/image.png",
Slug: "new-product",
Availability: true,
}
req.Var("product", product)
// Define a struct to receive the response.
var respData CreateProductResponse
// Run the query.
if err := client.Run(context.Background(), req, &respData); err != nil {
panic(err)
}
// Here you can use respData.
fmt.Println("Created Product ID:", respData.CreateProduct.ID)
}

Start building with Go

We made it really easy to set up your project in Hygraph and use our GraphQL API within your Go project.

Quickstart

Check out our docs to see how you can quickly set up your Hygraph project and enable the content API for your Go website or app.

Learn GraphQL

Hygraph is GraphQL-native Headless CMS offers precise data retrieval, minimizing over-fetching and optimizing efficiency.

Examples

Look at some of the example projects to see Hygraph in action.

Why Hygraph

Choosing Hygraph for your Go project

Integrating a GraphQL-native headless CMS with a Go application streamlines the content management and development process. For developers, it provides a schema that defines how to query or mutate data, making API calls straightforward and efficient. The strong typing in Go complements GraphQL's structure, making the integration robust and the codebase easier to maintain.

For content editors, a headless CMS with a GraphQL API offers a user-friendly interface to manage content independently from the site’s infrastructure. They can update, create, and organize content without delving into the code, allowing for real-time content updates.

go cms

Developer Experience

We try to be the most un-opinionated CMS on the market with a wide collection of open source example projects to get you started.

Headless CMS

As a headless CMS (i.e. API based content management), you can be as modular and flexible as you need. We even support multiplatform content management.

Management API

Hygraph boasts a flexible and powerful management API to manage your content and schema, as well as a blazing fast content API.

Get started for free, or request a demo to discuss larger projects