Ashutosh Kumar Singh
Ashutosh Kumar Singh

Reputation: 88

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

Upvotes: 1

Views: 3273

Answers (1)

Mat Schaffer
Mat Schaffer

Reputation: 1704

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.

Upvotes: 2

Related Questions