shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.1k stars 89 forks source link

Not parsing Node query correctly #92

Closed superbeeny closed 2 years ago

superbeeny commented 2 years ago

Hi Everyone,

Hopefully this is an error on my part. The query in the comment works in places like graphql explorer and postman but when I try to run it in Go I get the error: Parse error on "{" (LCURLY) at [1, 36]

Can someone point out what I'm doing wrong?

Thanks in advance!

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/shurcooL/githubv4"
    "golang.org/x/oauth2"
)

/*
    query($repoId: ID!) {
        node(id: $repoId){
        ... on Repository {
          id
          name
        }
      }
    }
*/
var query struct {
    Node struct {
        Repository struct {
            ID string `graphql:"id"`
            Name string `graphql:"name"`
        }`graphql:"... on Repository"`
    } `graphql:"node(id: $repoId")`
}

func main() {

    repoID := "<INSERT REPO ID>"
    GithubURL := os.Getenv("GITHUB_URL")
    AccessToken := os.Getenv("ACCESS_TOKEN")

    // Create a new GHE graphQL client
    src := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: AccessToken},
    )
    httpClient := oauth2.NewClient(context.Background(), src)
    v4client := githubv4.NewEnterpriseClient(GithubURL+"/api/graphql", httpClient)

    variables := map[string]interface{}{
        "repoId": githubv4.ID(repoID),
    }

    if err := v4client.Query(context.Background(), &query, variables); err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Printf("%#v", query)
}
dmitshur commented 2 years ago

This line looks off:

} `graphql:"node(id: $repoId")`

Perhaps it should be:

} `graphql:"node(id: $repoId)"`
superbeeny commented 2 years ago

and you were absolutely correct