graphqlgithub-apigithub-projects

GitHub GraphQL API Read custom field in Project V2


We are using Github Projects V2. I have created a custom field say 'MyCustomField'. I want to read the value of custom field MyCustomField using Github GraphQL API. I am following Gtihub GraphQL API Docs

So far I have got till reading a few predefined fields on Gtihub Issues like title, url, assignees and labels. I am using Windows PowerShell:

$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
  query($project_id: ID!){
    node(id: $project_id) {
        ... on ProjectV2 {
          items(last: 20) {
            nodes{
              id              
              content{              
                ...on Issue {
                  title
                  url
                  assignees(first: 10) {
                    nodes{
                      login
                    }
                  }
                  labels(first:5) {
                    edges {
                        node {
                          name
                        }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }' -f project_id=$project_id

I am not able to find a way to get the custom field MyCustomField. I am expecting to write some query like below:

$project_id="MyProjectIDFetchedUsingDifferentQuery"
gh api graphql -f query='
  query($project_id: ID!){
    node(id: $project_id) {
        ... on ProjectV2 {
          items(last: 20) {
            nodes{
              id              
              content{              
                ...on Issue {
                  title
                  url
                  assignees(first: 10) {
                    nodes{
                      login
                    }
                  }
                  labels(first:5) {
                    edges {
                        node {
                          name
                        }
                    }
                  }
                  customFields(first:5) {
                    nodes {
                        name
                    }
                  }
                }
              }
            }
          }
        }
      }
    }' -f project_id=$project_id

Solution

  • I came up with this which I think should get the custom fields on your board:

    query {
      node(id: "(my project id)") {
        ... on ProjectV2 {
          items(last: 20) {
            nodes {
              id
              content {
                ... on Issue {
                  title
                  url
                  state
                  assignees(first: 10) {
                    nodes {
                      login
                    }
                  }
                }
              }
              fieldValues(first: 20) {
                nodes {
                  ... on ProjectV2ItemFieldSingleSelectValue {
                    field {
                      ... on ProjectV2SingleSelectField {
                        name
                      }
                    }
                    name
                    id
                  }
                  ... on ProjectV2ItemFieldLabelValue {
                    labels(first: 20) {
                      nodes {
                        id
                        name
                      }
                    }
                  }
                  ... on ProjectV2ItemFieldTextValue {
                    text
                    id
                    updatedAt
                    creator {
                      url
                    }
                  }
                  ... on ProjectV2ItemFieldMilestoneValue {
                    milestone {
                      id
                    }
                  }
                  ... on ProjectV2ItemFieldRepositoryValue {
                    repository {
                      id
                      url
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Credit should go to https://stackoverflow.com/users/2312060/rich-kuzsma for posting https://gist.github.com/richkuz/e8842fce354edbd4e12dcbfa9ca40ff6

    This had the basic format for querying ProjectV2 fields, and I added the ProjectV2SingleSelectField bit to include both the field name and its value in the response.