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.
you might use thehttppackage toconsume RESTful APIs in Go, among other features.
For GraphQL, you could make queries and mutations to GraphQL APIs servers with thehttppackage and other built-in packages.
GraphQL client packages likeMachineboxsorshurCooLsmake the process of interacting with GraphQL APIs even easier.
you’re free to use thehttppackage without any dependencies to interact with a GraphQL API.
you could useioutilto read the response body, and thetimepackage to set a time limit for the request.
For this article, youll query Apollo GraphQLs Countries API to query data on countries worldwide.
All GraphQL operations are typically POST requests since they must have a payload (request body).
Youll need to study the structure of the GraphQL schema to query the API.
ThejsonMapInstancevariable is the map instance for the requests body.
The value is a string of the query data youre expecting from the API.
you could 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.
TheNewRequestmethod returns a request instance.
You’ll have to set the content pop in depending on the API’s specifications.
you’re free to 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 youve declared the HTTP client, execute your API request with theDomethod.
TheDomethod accepts the request instance and returns the response and an error.
you might read the response of the API request with theioutilpackagesReadAllmethod.
With the built-in string function, you might convert the byte slice response to the string bang out.
You’ll need to create clients, request instances, and read the data with the same packages.