Getting Started with Go

This simple program demonstrates how easy it is to query our GraphQL API over gRPC. It:

  1. Uses our go client to retrieve a JWT
  2. Creates a gRPC connection with credentials
  3. Instantiates a GraphQL client
  4. Executes a simple GraphQL query
  5. Prints the response

Before You Begin

  1. Visit golang gRPC prerequisites
  2. Then make sure that protoc is accessible from your $PATH

import (
    "context"
    "fmt"
    "log"
    pbgraphql "main/graphql"
    "os"

    "github.com/dfuse-io/eosws-go"
    "golang.org/x/oauth2"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"
    "google.golang.org/grpc/credentials/oauth"
    "io"
)

func main() {
    token, _, err := eosws.Auth(os.Args[1])
    if err != nil {
        log.Fatalf("cannot get auth token: %s", err)
    }

    credential := oauth.NewOauthAccess(&oauth2.Token{AccessToken: token, TokenType: "Bearer"})
    connection, err := grpc.Dial(
        "mainnet.eos.dfuse.io:443",
        grpc.WithPerRPCCredentials(credential),
        grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
    )
    graphqlClient := pbgraphql.NewGraphQLClient(connection)

    q := `subscription  {
       searchTransactionsForward(query: "action:transfer", limit:10) {
         trace { matchingActions { receiver account name json }}}}`

    executionClient, err := graphqlClient.Execute(
        context.Background(),
        &pbgraphql.Request{Query: q},
    )
    if err != nil {
        log.Fatalf("execution error: %s", err)
    }

    for {
        response, err := executionClient.Recv()
        if err != nil {
            if err != io.EOF {
                fmt.Println("No more result available")
                break
            }

            log.Fatalf("reception error: %s", err)
        }

        fmt.Println("Received response:", response.Data)
    }
}

Get the Source Code


git clone https://github.com/dfuse-io/example-graphql-grpc.git
git clone https://github.com/dfuse-io/graphql-over-grpc.git

Generate graphql.pb.go


protoc -I graphql-over-grpc graphql/graphql.proto --go_out=plugins=grpc:./example-graphql-grpc

Get the Project Dependencies


cd example-graphql-grpc
go mod init main
go mod tidy

Run it

First, you must visit https://app.dfuse.io to get your API key. Then, you can proceed to run the following command


go run main.go YOUR_API_KEY_HERE