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

Unable to query info for issues in Projects #104

Closed nashafa closed 1 year ago

nashafa commented 1 year ago

Hi. I'm running into a problem where I am unable query titles, ids, etc. for items in projects that are issues. I have not tested with pull requests, but I am able to query info for draft issues.

For example, the following GraphQL query works fine in the GitHub GraphQL API Explorer and with gh api (filling in a valid ProjectV2Item's node_id).

query GitHubProjectItemQuery($node_id: ID = "") {
  node(id: $node_id) {
    ... on ProjectV2Item {
      content {
        ... on DraftIssue {
          title
        }
        ... on Issue {
          title
        }
        ... on PullRequest {
          title
        }
      }
    }
  }
}

This is the equivalent I am using in Go.

var GitHubProjectItemQuery struct {
  Node struct {
    ProjectV2Item struct {
      Content struct {
        DraftIssue struct {
          Title string
        } `graphql:"... on DraftIssue"`
        Issue struct {
          Title string
        } `graphql:"... on Issue"`
        PullRequest struct {
          Title string
        } `graphql:"... on PullRequest"`
      }
    } `graphql:"... on ProjectV2Item"`
  } `graphql:"node(id: $node_id)"`
}
variables := map[string]interface{}{
  "node_id": githubv4.ID(node_id),
}
err := githubClient.Query(context.Background(), &GitHubProjectItemQuery, variables)

Oddly, for draft issues, the title appears in GitHubProjectItemQuery.Node.ProjectV2Item.Content.DraftIssue.Title, GitHubProjectItemQuery.Node.ProjectV2Item.Content.Issue.Title, and GitHubProjectItemQuery.Node.ProjectV2Item.Content.PullRequest.Title. For issues, nothing appears for any title, id, etc.

Any ideas as to what exactly is going on here? Thanks for the help.

leandregagnonlewis commented 1 year ago

I have the same problem.

I am able to get info using command line

gh api graphql -f query='
  query{
    node(id: "PVTI_xxxxxx") {

            ... on ProjectV2Item {
              fieldValueByName(name: "Status") {            
                  ... on ProjectV2ItemFieldSingleSelectValue {
                    name
                    id
                    field {
                      ... on ProjectV2FieldCommon {
                        name
                      }
                    }
                  }
                }              
              }
            }
          }

'

but with the following go query

type ProjectItemDetailsQuery struct {
    Node struct {
        ID            githubv4.ID
        ProjectV2Item struct {
            StatusField struct {
                SingleSelectFieldValue struct {
                    ID       string
                    Name     string
                    OptionID string
                } `graphql:"... on ProjectV2ItemFieldSingleSelectValue"`
            } `graphql:"status: fieldValueByName(name: \"Status\")"`
        } `graphql:"... on ProjectV2Item"`
    } `graphql:"node(id: $id)"`
}

it is returning null values, except if I do it with a draft issue.

Any lead towards a solution?

leandregagnonlewis commented 1 year ago

Well we can close that. The problem was that you need to add permission to the app for accessing issues as well as projects.

nashafa commented 1 year ago

Thanks for looking into this. That would actually make a lot of sense; I've run into similar problems with GitHub Projects elsewhere, but didn't think specifically to check that the permission for Issues was provided.

I'm no longer using this API, but I'll go ahead and close this assuming that is the correct fix.