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

query pull request struct #103

Closed jurgen-weber-deltatre closed 2 years ago

jurgen-weber-deltatre commented 2 years ago

Apologies in advanced, golang noob and graphql super noob. I am attempting to get working the following query:

https://gist.github.com/mccutchen/9a0530a440470b4dee57c53284a18b52#file-query-pull-request-by-ref-graphql

Here is the struct and code I have:

        type LoopStruct struct {
            Author struct {
                login string
            }
            baseRefOid     string
            headRefName    string
            headRefOid     string
            number         int
            id             string
            mergeable      string
            reviewDecision string
            state          string
            title          string
        }
        var getPR struct {
            Repository struct {
                Object struct {
                    Commit struct {
                        AssociatedPullRequests struct {
                            Nodes []LoopStruct
                        } `graphql:"associatedPullRequests(first: 10)"`
                    } `graphql:"... on Commit"`
                } `graphql:"object(expression: $ref)"`
            } `graphql:"repository(owner: $repoOwner, name: $repoName)"`
        }
        prvariables := map[string]interface{}{
            "ref":       githubv4.String(*prs.Head.Ref),
            "repoOwner": githubv4.String(repoOwner),
            "repoName":  githubv4.String(repoName),
        }
        err = clientv4.Query(context.Background(), &getPR, prvariables)
        if err != nil {
            zl.With(zap.Error(err)).Fatal("Get PR in v4 fail")
        }

error

"struct field for \"login\" doesn't exist in any of 2 places to unmarshal"

any assistance appreciated. :)

dmitshur commented 2 years ago

The query struct fields likely need to be exported (that is, begin with an upper-case letter) to be writeable by the githubv4 package. Does it help if you try that?

 type LoopStruct struct {
    Author struct {
-       login string
+       Login string
    }
-   baseRefOid     string
+   BaseRefOid     string
    ...
jurgen-weber-deltatre commented 2 years ago

wow, just wow... So simple and amazing.

Thank you very much, worked.