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
- Download, install, configure golang
- Clone the repo
# Clone Repo
git clone [email protected]:graphprotocol/query-examples.git
# CD into remix example
cd ./examples/golang_api
- Install the deps using go modules
go mod tidy
- Create the
.env
.
cp ./.env.example ./.env
# obtain an API Key from Subgraph Studio and fill the value in the `.env`
- Run the API using
justfile
. Install just, see instructions here
# run the main file directly
just dev
# build and start the executable
just start
- Run the API by compiling the
main.go
file directly
go run .
- 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",
},
},
}