How to Consume GraphQL API Endpoints in Go
Graph Query Language (GraphQL) is a language and specification for interacting with GraphQL APIs, an HTTP-based client-server architecture for communication across the web.
Facebook released GraphQL as an alternative to the REST architectural standard. GraphQL addresses most of the issues with REST in a stateless and cacheable manner. It provides a simple and intuitive syntax that describes the expected output(s) or input(s), and the API relays the data matching the request.

Since GraphQL is a specification, you’re able to build and consume GraphQL APIs in any server-side programming language, including Go.
Getting Started With GraphQL APIs in Go
GraphQL is based on HTTP architecture, and Go provides HTTP functionality in its built-inhttppackage.
you could use thehttppackage toconsume RESTful APIs in Go, among other features. For GraphQL, you can make queries and mutations to GraphQL APIs servers with thehttppackage and other built-in packages.

GraphQL client packages likeMachinebox’sorshurCooL’smake the process of interacting with GraphQL APIs even easier.
You can use thehttppackage without any dependencies to interact with a GraphQL API. Import these packages in your Go file to begin:

You’ll use thebytespackage to create a new buffer for the request and thejsonpackage to marshal a map to the JSON request body. You can useioutilto read the response body, and thetimepackage to set a time limit for the request.
Querying GraphQL APIs With Go
There are many free public GraphQL APIs you can query and integrate into your applications. For this article, you’ll query Apollo GraphQL’s Countries API to query data on countries worldwide.
All GraphQL operations are typically POST requests since they must have a payload (request body). Most GraphQL APIs accept JSON request body as the content type, andGo provides functionality for using maps and structs to work with JSON.

You’ll need to study the structure of the GraphQL schema to query the API. The query will be the same as a regular GraphQL query except that the operation (query or mutation) is the key, and the data is the map’s value.
Here’s how you may declare a JSON map instance that you’ll marshal into JSON for the request.

ThejsonMapInstancevariable is the map instance for the request’s body. The value is a string of the query data you’re expecting from the API. In this case, the query data you’ll expect from the API’scountriesschema are thename,phone,currency,code, andemojifields.
You can use theMarshalmethod of thejsonpackage to encode the map instance to JSON. TheMarshalmethod returns the encoded JSON and an error for cases with an encoding problem.
Once you’ve encoded the map to JSON, you can send the POST request to the API. You can create a new request instance with theNewRequestmethod, which takes in the request type, URL, and JSON buffer.
TheNewRequestmethod returns a request instance. You’ll have to set the content type depending on the API’s specifications. You can set the content type for HTTP requests with theSetmethod of theHeadermethod of your request instance.
You can create a simple HTTP client for your request with theClientmethod of the HTTP package. TheClientmethod also allows you to set a time limit for your request with thetimepackage.
After you’ve declared the HTTP client, execute your API request with theDomethod. TheDomethod accepts the request instance and returns the response and an error.
You can read the response of the API request with theioutilpackage’sReadAllmethod. It takes in an output stream and returns a byte slice of the data with an error that you can handle.
With the built-in string function, you can convert the byte slice response to the string type.
Here’s a response showing the result of the API request:
Consuming RESTful APIs Is Like Consuming GraphQL APIs
Since both REST and GraphQL APIs use the HTTP protocol, consuming each is a very similar process, and you can use thehttppackage for both cases.
You’ll need to create clients, request instances, and read the data with the same packages.
Golang’s http package makes it easy to use REST APIs for data transfer and client apps.
Revolutionize your driving experience with these game-changing CarPlay additions.
The best features aren’t the ones being advertised.
Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.
I found my TV was always listening—so I shut it down.
Your phone’s camera app doesn’t show this, so it’s easy to miss.