Consume GraphQL service from a Golang app - go

I have a Go Application that needs to consume a GraphQL service, now the documentation of graphQL is more oriented to the GraphQL server and not as a client. How can I do that?
I checked this example but some things are not clear to me:
Should I have a Resolve function to each field that I am going to retrieve?
Should I have defined the variable 'fields' with the data structure that I am expecting?
Where can I find a very simple example of a GraphQL client in Golang?

You should check this project: https://github.com/machinebox/graphql .
If you don't want to use an external library inside in your project, you could look the code and see how can you implement a simple client.

One of the module that I would recommend is shurcool
client = graphql.NewClient("http://<<graphqlEndpoint>>", nil)
err := client.Query(context.Background(), &query, nil)
if err != nil {
// Handle error.
}
fmt.Println(query.Me.Name)

Related

Firebase Go wants host "firebaseio.com"

I'm new to firebase and I'm trying to setup a small test with a simple database in Go.
I struggle a lot with the database connection. Here is my code:
tx := context.Background()
conf := &firebase.Config{
DatabaseURL: "https://mydb.europe-west1.firebasedatabase.app",
}
// Fetch the service account key JSON file contents
opt := option.WithCredentialsFile("./fireBasePrivateKey.json")
// Initialize the app with a service account, granting admin privileges
app, err := firebase.NewApp(ctx, conf, opt)
if err != nil {
log.Fatalln("Error initializing app:", err)
}
client, err := app.Database(ctx)
if err != nil {
log.Fatalln("Error initializing database client:", err)
}
With that code (which comes from the official documentation), I've got an error on the Database client initialization:
invalid database url: wants host: .firebaseio.com
I then tried with the requested url: mydb.firebaseio.com -> I've got another error telling me my db is not in that region and gives me the previous db address.
I also tried other things like mydb.europe-west1.firebaseio.com but here it says me the certificate is not valid for this url...
I'm a bit lost. I understand the problem has to do with the localization of the DB I choose when I created it, but I don't understand how to handle it with the go implementation.
The <projectname>.firebaseio.com format used to be the only format for Firebase Database URLs until early last year. Nowadays, databases in the US still use that format, but databases in other regions use the <dbname><region>.firebasedatabase.app format that you have.
Support for the newer URL format was added in PR #423 and released in version 4.6 of the Go Admin SDK, which was released in June. Upgrade to this version (or later), to ensure you don't get the error message anymore.

ERR unknown command `publish` Miniredis Golang

I'm testing this small code fragment using miniredis mock for Redis. repository.client is a functioning mock (used for other tests) that returns a Redis client.
err := repository.client.Publish(ctx, "UPDATE", "MESSAGE").Err()
if err != nil {
log.fatal(err.Error())
}
When I run the test it logs me the following error:
ERR unknown command `publish`, with args beginning with: `UPDATES`, `MESSAGE`,
I'm confused by the fact that miniredis should implement pub-sub functionalities. Any clue of what is the issue?
After researching on owner's repository I found out that the issue was related to the imported version. In online articles, it is normally cited the import github.com/alicebob/miniredis, but it doesn't have implemented pub/sub functionalities. To make them work it is important to import V2:
github.com/alicebob/miniredis/v2
Source: https://github.com/alicebob/miniredis/issues/157

Are there any config NOT to use trace on specific environment

Now I have a Golang Application deployed on GAE, with stackdriver trace.
About stackdriver Trace, to get custom span data, I did set up on my code, like
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
client := &http.Client{
Transport: &ochttp.Transport{
// Use Google Cloud propagation format.
Propagation: &propagation.HTTPFormat{},
},
}
ref. https://cloud.google.com/trace/docs/setup/go
On GAE, I succeed in viewing trace on my GCP console.
but, I DON'T want to trace these log on my local developing environment (I'm using docker).currently, I try to run my application on docker, nil pointer panic shows up on Span.Export() which may be called from Span.End().
So, I wonder if someone knows the way to DISABLE stackdriver trace on specific environment (with my case, on docker).
Otherwise, should I check condition of trace configuration, like as below ?
if trace.projectId != "" {
ctx := reque.Context()
_, span := trace.StartSpan(ctx,"Span blahblah")
defer span.End()
}
There is no point for Google in adding an extra logic like you need into the Trace code, the GAE apps are instrumented with, in order to disable that Trace code when the GAE App is executed somewhere in a third party environment like Docker on-prem. Most likely an answer to the question is "No, there is no magic config for that". Hence it is up-to-you how to sort this out.
As a general idea: following up an approach with [NoopExporter] offered by Emile Pels and having admitted the fact we can't get rid of the Trace code with "magic config", if I developed my app in Python I'd considered using decorator as a wrapper to bring piece of intelligence into the Trace calls, or redefining them as mock functions. It seems Golang does not have direct analog of Python decorators but this functionality could be implemented somehow. This is being discussed in the Internet, for example here:
Go Decorator Function Pattern
Redefine function so that it references its own self

How to correctly import an aws-sdk-go service?

I am fairly new to the Go programming language and completely new to the Go SDK from AWS. I am trying to use a service but I have a strange problem where the types defined by the imported service are found, but the functions of the service are undefined.
This question is not about using the particular service, but just how to correctly import it. My code:
package auth
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
func SignUpTest() {
input := cognitoidentityprovider.SignUpInput{
Username: aws.String("example#mail.com"),
Password: aws.String("test1234"),
}
_, err := cognitoidentityprovider.SignUp(&input)
if err != nil {
log.Fatal(err)
}
}
I get the following error when running go build:
auth/signup.go:18:12: undefined: cognitoidentityprovider.SignUp
The autocomplete in my IDE also states that it can find the cognitoidentityprovider.SingUpInput struct, but it is unable to find the cognitoidentityprovider.SignUp function.
I use Go 1.10.1 on WSL Ubuntu. I use DEP 0.4.1 for package management. I verified that the AWS SDK is available in the vendor folder and that the cognitoidentityprovider package is available (the SignUp) function is also there.
What am I missing here?
The error says it all. cognitoidentityprovider.SignUp isn't defined, because there is no symbol SignUp exported by the cognitoidentityprovider package.
I'm not really sure what you want to do instead, since I'm not familiar with that SDK, but you're trying to call a function that doesn't exist. I suggest re-examining the documentation or example you're following. You've probably made a simple mistake.
You seem to be confused by the CognitoIdentityProvider.SignUp instance method. But as that's an instance method, and not an exported function, it requires an instance of a CognitoIdentityProvider first:
cip := cognitoidentityprovider.New( ... )
_, err := cip.SignUp(input)

HyperLedger Fabric Get Block Info - Fabric Go SDK

I used to set up a Fabric network and deployed a fabric network and basic application using a Fabric and Fabric GoLang SDK. I'm able to do the query and write to the chain.
Is there any way to retrieve the Block Info? Like block height and current hash?
+ I'm unable to find out a documentation for GoLang Fabric SDK.
I followed following code and tutorial,
Fabric Basic App - Tutorial
https://chainhero.io/2017/07/tutorial-build-blockchain-app/
Fabric Basic App using GoLang SDK - Code
https://github.com/chainHero/heroes-service/
GoLang SDK - Official SDK
https://github.com/hyperledger/fabric-sdk-go
In general, the sdk will provide the basic method such you said GetBlockInfo,I have search for the GoLang SDK, it can not be found. While Java sdk provide this such method reference this java test .
Another way to use these method(you must know a little fabric source code), in fact these method are included in the system chaincode, you could invoke the system chancode just like you invoke the normal chaincode.
A example is following:
from the go sdk test ,you could see this,
response, err := chClient.Query(chclient.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()})
just change the params
response, err := chClient.Query(chclient.Request{ChaincodeID: "qscc", Fcn: "invoke", Args: integration.ExampleCCQueryArgs("GetChainInfo")})
qscc is a system chancode,you could download the fabric source code,and from qscc file,you could see(it provide many invoke service):
GetChainInfo string = "GetChainInfo"
GetBlockByNumber string = "GetBlockByNumber"
GetBlockByHash string = "GetBlockByHash"
GetTransactionByID string = "GetTransactionByID"
GetBlockByTxID string = "GetBlockByTxID"
Go sdk(fabric-sdk-go/pkg/client/ledger) provides several methods for getting information about blockchain. For example:
...
client, err := ledger.New(channelContext)
block, err := client.QueryBlockByHash(blockHash)
block, err = client.QueryBlock(blockNumber)
The Go SDK now includes methods for querying block information. These methods are contained within the ledger client package.
You can see an example in the ledger client integration test.

Resources