QueryString with Search creating issue
I have my code for fetching the information from repo based on programming language
func generateDataGithub(githubToken string, githubURL string, org string, language string, count int) {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewEnterpriseClient(githubURL, httpClient)
var query struct {
Search struct {
RepositoryCount githubv4.Int
Edges []struct {
Node struct {
Repository struct {
Name githubv4.String
NameWithOwner githubv4.String
Owner struct {
Login githubv4.String
}
PrimaryLanguage struct {
Name githubv4.String
}
DefaultBranchRef struct {
Name githubv4.String
}
} `graphql:"... on Repository"`
}
}
}`graphql:"search(first: $count, query: $searchQuery, type: REPOSITORY)"`
}
variables := map[string]interface{}{
"searchQuery": githubv4.String(fmt.Sprintf(`user:%s language:%s`,githubv4.String(org),githubv4.String(language))),
"count": githubv4.Int(count),
}
Executing them but not getting any result back
err := client.Query(context.Background(), &query, variables)
if err != nil {
Error.Println(err)
}
See Below
{
"Search": {
"RepositoryCount": 0,
"Edges": []
}
}
While from Graphql
{
search(query: "user:<your org> language:java", type: REPOSITORY, first: 2) {
repositoryCount
edges {
node {
... on Repository {
owner {
login
}
name
nameWithOwner
owner {
login
}
primaryLanguage {
name
}
defaultBranchRef {
name
}
}
}
}
}
}
I am getting this
{
"data": {
"search": {
"repositoryCount": 10,
"edges": [
{
"node": {
"owner": {
"login": "***"
},
"name": "mule",
"nameWithOwner": "***/**",
"primaryLanguage": {
"name": "Java"
},
"defaultBranchRef": {
"name": "master"
}
}
}
]
}
}
}
Any help, as I have seen most of the closed tickets, and implemented what has been suggested.
I don't see anything wrong in the snippets you posted.
I tried putting them together into a complete program, and can't reproduce the problem with it. It seems to work fine:
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
func main() {
flag.Parse()
err := run()
if err != nil {
log.Println(err)
}
}
func run() error {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_GRAPHQL_TEST_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
// Query some details about a repository, an issue in it, and its comments.
{
var q struct {
Search struct {
RepositoryCount githubv4.Int
Edges []struct {
Node struct {
Repository struct {
Name githubv4.String
NameWithOwner githubv4.String
Owner struct {
Login githubv4.String
}
PrimaryLanguage struct {
Name githubv4.String
}
DefaultBranchRef struct {
Name githubv4.String
}
} `graphql:"... on Repository"`
}
}
} `graphql:"search(first: $count, query: $searchQuery, type: REPOSITORY)"`
}
variables := map[string]interface{}{
"searchQuery": githubv4.String(fmt.Sprintf(`user:%s language:%s`, githubv4.String("shurcooL"), githubv4.String("Go"))),
"count": githubv4.Int(2),
}
fmt.Println("searchQuery:", variables["searchQuery"])
err := client.Query(context.Background(), &q, variables)
if err != nil {
return err
}
printJSON(q)
}
return nil
}
// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
func printJSON(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}
Output:
searchQuery: user:shurcooL language:Go
{
"Search": {
"RepositoryCount": 36,
"Edges": [
{
"Node": {
"Repository": {
"Name": "vfsgen",
"NameWithOwner": "shurcooL/vfsgen",
"Owner": {
"Login": "shurcooL"
},
"PrimaryLanguage": {
"Name": "Go"
},
"DefaultBranchRef": {
"Name": "master"
}
}
}
},
{
"Node": {
"Repository": {
"Name": "Go-Package-Store",
"NameWithOwner": "shurcooL/Go-Package-Store",
"Owner": {
"Login": "shurcooL"
},
"PrimaryLanguage": {
"Name": "Go"
},
"DefaultBranchRef": {
"Name": "master"
}
}
}
}
]
}
}
Are you sure your values for org and language are okay? Maybe they have extra quotes or so.
If you're still running into this, please provide a complete program that reproduces it.
In general, a surefire way to debug these kind of issues is to inspect the GraphQL query generated and sent by the library. That can be done by modifying its source code or providing an http.Client with a custom Transport that prints the request body.
Looks like the issue is coming due to not resetting token, I am getting below response on a few of git repo, however, others are giving me the right response {"search":{"repositoryCount":0,"edges":[]}}
I have modified the query a little bit to fine search githubv4.String(fmt.Sprintf(repo:%s, githubv4.String("shurcooL"), githubv4.String("githubv4"))),
So I am not sure how I will reset the token, so get the desire response.