Skip to docs content

GraphQL with Go

There are many GraphQL client written in golang. machinebox/graphql is a Low-level GraphQL client for Go.

This client is,

  1. Simple, familiar API
  2. Respects context.Context timeouts and cancellation
  3. Build and execute any kind of GraphQL request
  4. Use strong Go types for response data
  5. Use variables and upload files
  6. Simple error handling

Installation

Make sure you have a working Go environment. To install graphql, simply run:

$ go get github.com/machinebox/graphql

Usage

Tip:

Always remember to replace API_SECRET in the Bearer Token and project-id in the URL with the correct values from the Apito console.
If you are unsure where to find your API secrets and endpoints for your project, visit this page.

import "context"

// create a client (safe to share across requests)
client := graphql.NewClient("https://api.apito.io/secured/graphql")

// make a request
req := graphql.NewRequest(`
    query ($key: String!) {
        items (id:$key) {
            field1
            field2
            field3
        }
    }
`)

// set any variables
req.Var("key", "value")

// set header fields
req.Header.Set("Authorization", "Bearer API_SECRET")

// define a Context for the request
ctx := context.Background()

// run it and capture the response
var respData ResponseStruct
if err := client.Run(ctx, req, &respData); err != nil {
    log.Fatal(err)
}