Skip to content

Latest commit

 

History

History

golang_api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

The Graph Query Examples | Golang API

Demos how to query the Graph Network Arbitrum Subgraph published to The Graph Network using an API Key obtained on The Graph Studio in a Golang api. This is a handy way to integrate a subgraph into a golang api. It is pretty simple in nature as it just provides a passthrough to the underlying Subgraph GraphQL Schema. But, it provides your Subgraph in a way that:

  • hides your API Key from the client
  • allows you to utilize CORS to help prevent non-configured apps/users from querying this API, and therefore your Subgraph
  • allows you to add authentication/authorization on this API and only allow authenticated/authorized users of your app to query your Subgraph through this API.
  • allows you to provide custom endpoints/resolvers to add additional processing ontop of your Subgraph

Running

  1. Download, install, configure golang
  2. Clone the repo
# Clone Repo
git clone [email protected]:graphprotocol/query-examples.git

# CD into remix example
cd ./examples/golang_api
  1. Install the deps using go modules
go mod tidy
  1. Create the .env.
cp ./.env.example ./.env
# obtain an API Key from Subgraph Studio and fill the value in the `.env`
  1. Run the API using justfile. Install just, see instructions here
# run the main file directly
just dev
# build and start the executable
just start
  1. Run the API by compiling the main.go file directly
go run .

Querying the API -> Subgraph

  • just a query, no variables. queries the _meta entity
# start the api
just dev

# query the _meta. no variables
curl http://127.0.0.1:4000/api/graphql -H 'Content-Type: application/json' -d '{"query": "{_meta { block { number }}}"}' -X POST
{
  data: {
    _meta: {
      block: {
        number: 202710879,
      },
    },
  },
}
  • query with variables and an operationName
curl http://127.0.0.1:4000/api/graphql -H 'Content-Type: application/json' -d '{"query": "query Subgraph($id: ID!) { subgraph(id: $id) { id } }", "variables": {"id": "8SxuHUYYBLHs1UkgFFYNaS7MgrEiAMbDyt5YzwZsSa6R"}, "operationName": "Subgraph"}' -X POST
{
  data: {
    subgraph: {
      id: "8SxuHUYYBLHs1UkgFFYNaS7MgrEiAMbDyt5YzwZsSa6R",
    },
  },
}