1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @mat-schaffer for your solution and pointing to the gist where I can find many examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.