Skip to content

Instantly share code, notes, and snippets.

@richkuz
Created August 7, 2022 13:56
Show Gist options
  • Select an option

  • Save richkuz/e8842fce354edbd4e12dcbfa9ca40ff6 to your computer and use it in GitHub Desktop.

Select an option

Save richkuz/e8842fce354edbd4e12dcbfa9ca40ff6 to your computer and use it in GitHub Desktop.
Notes about using the new GitHub ProjectV2 API

Notes on GitHub ProjectV2 API

GitHub Projects Beta aka GitHub Projects Next aka GitHub ProjectNext is now known as GitHub ProjectV2 in the API.

It's not immediately obvious how to use the new API, especially if you're coming from the old beta API.

You no longer need to set the HTTP header: GraphQL-Features: projects_next_graphql.

You still need to add your GitHub personal access token to the Authorization header, e.g. Authorization: Basic ghp_...

Here's a query to help you get started. It queries information about the items in the project https://github.com/orgs/richkuz-org/projects/2/views/1

query richkuzProjectItems
{
  viewer {
    id
    login
    organization(login: "richkuz-org") {
      
      #projectNext(number: 2) {
      projectV2(number:2) {
        
        id
        title # Project title
        
        items(first: 20) {
          nodes {
            databaseId
						id
            content {
              __typename
              ... on Issue {
                # Find associated issue with Project item:
                url
                #... all fields associated with an Issue are available
              }
            }
            
            # Query custom fields associated with the Project Item:
            fieldValueByName(name:"foo") {
              __typename
              ... on ProjectV2ItemFieldTextValue {
                id
                text
              }
              ... on ProjectV2ItemFieldDateValue {
                id
                date
              }
              ... on ProjectV2ItemFieldSingleSelectValue {
                name
                optionId
              }
            }
            
            # # List all field types
            # fieldValues(first:20) {
            #   nodes { 
            #     __typename
            #   }
            # }
            
            fieldValues(first:20) {
              nodes {
                ... on ProjectV2ItemFieldSingleSelectValue {
                  name
                  id
                }
                ... on ProjectV2ItemFieldLabelValue {
                  labels(first:20) {
                    nodes {
                      id
                      name # e.g. "my-custom-label", "label1", ...
                      
                      # Query a list of PRs that have this label:
                      pullRequests(first:20) {
                        nodes {
                          url
                        }
                      }
                      # Query a list of Issues that have this label:
                      issues(first:20) {
                        nodes {
                          url
                        }
                      }
                    }
                  }
                }
                ... on ProjectV2ItemFieldTextValue {
                  text
                  id
                  updatedAt
                  creator {
                    url
                  }
                }
                ... on ProjectV2ItemFieldMilestoneValue {
                  milestone {
                    id
                  }
                }
                ... on ProjectV2ItemFieldRepositoryValue {
                  repository {
                    id
                    url
                  }
                }
            	}
            }
          }
        }
      }
    }
  }
}

Example response:

{
  "data": {
    "viewer": {
      "id": "MDQ6VXNlcjYyNTIyMjQ4",
      "login": "richkuz",
      "organization": {
        "projectV2": {
          "id": "PVT_kwDOBQfyVc0FoQ",
          "title": "My Project",
          "items": {
            "nodes": [
              {
                "databaseId": 79558,
                "id": "PVTI_lADOBQfyVc0Foc4AATbG",
                "content": {
                  "__typename": "Issue",
                  "url": "https://github.com/richkuz-org/repo1/issues/2"
                },
                "fieldValueByName": {
                  "__typename": "ProjectV2ItemFieldTextValue",
                  "id": "PVTFTV_lQDOBQfyVc0Foc4AATbGzgFbUdY",
                  "text": "issue2's foo value"
                },
                "fieldValues": {
                  "nodes": [
                    {
                      "repository": {
                        "id": "MDEwOlJlcG9zaXRvcnkzNjg2MTc3NzU=",
                        "url": "https://github.com/richkuz-org/repo1"
                      }
                    },
                    {
                      "labels": {
                        "nodes": [
                          {
                            "id": "MDU6TGFiZWwzMTU2OTk1NzM2",
                            "name": "label1",
                            "pullRequests": {
                              "nodes": []
                            },
                            "issues": {
                              "nodes": [
                                {
                                  "url": "https://github.com/richkuz-org/repo1/issues/1"
                                },
                                {
                                  "url": "https://github.com/richkuz-org/repo1/issues/2"
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "text": "issue2",
                      "id": "PVTFTV_lQDOBQfyVc0Foc4AATbGzgAEkIQ",
                      "updatedAt": "2021-08-20T19:44:57Z",
                      "creator": {
                        "url": "https://github.com/richkuz"
                      }
                    },
                    {
                      "name": "3",
                      "id": "PVTFSV_lQDOBQfyVc0Foc4AATbGzgFbUIU"
                    },
                    {
                      "text": "issue2's foo value",
                      "id": "PVTFTV_lQDOBQfyVc0Foc4AATbGzgFbUdY",
                      "updatedAt": "2022-08-07T13:45:32Z",
                      "creator": {
                        "url": "https://github.com/richkuz"
                      }
                    }
                  ]
                }
              },
              {
                "databaseId": 155869,
                "id": "PVTI_lADOBQfyVc0Foc4AAmDd",
                "content": {
                  "__typename": "Issue",
                  "url": "https://github.com/richkuz-org/repo1/issues/1"
                },
                "fieldValueByName": {
                  "__typename": "ProjectV2ItemFieldTextValue",
                  "id": "PVTFTV_lQDOBQfyVc0Foc4AAmDdzgFbUdc",
                  "text": "issue1's foo value"
                },
                "fieldValues": {
                  "nodes": [
                    {
                      "repository": {
                        "id": "MDEwOlJlcG9zaXRvcnkzNjg2MTc3NzU=",
                        "url": "https://github.com/richkuz-org/repo1"
                      }
                    },
                    {
                      "labels": {
                        "nodes": [
                          {
                            "id": "MDU6TGFiZWwzMTU2OTk1NzM2",
                            "name": "label1",
                            "pullRequests": {
                              "nodes": []
                            },
                            "issues": {
                              "nodes": [
                                {
                                  "url": "https://github.com/richkuz-org/repo1/issues/1"
                                },
                                {
                                  "url": "https://github.com/richkuz-org/repo1/issues/2"
                                }
                              ]
                            }
                          }
                        ]
                      }
                    },
                    {
                      "text": "Issue1",
                      "id": "PVTFTV_lQDOBQfyVc0Foc4AAmDdzgAICrM",
                      "updatedAt": "2021-10-01T20:30:23Z",
                      "creator": {
                        "url": "https://github.com/richkuz"
                      }
                    },
                    {
                      "name": "1",
                      "id": "PVTFSV_lQDOBQfyVc0Foc4AAmDdzgFbUIQ"
                    },
                    {
                      "text": "issue1's foo value",
                      "id": "PVTFTV_lQDOBQfyVc0Foc4AAmDdzgFbUdc",
                      "updatedAt": "2022-08-07T13:45:36Z",
                      "creator": {
                        "url": "https://github.com/richkuz"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }
}
@erikbrgr
Copy link

Hi @richkuz ! I hope you can help me. I have a custom field Type and want to retrieve all items that have Type set to Module. Retrieving the value isn't the issue (and you for very good examples there) but for the life of me I cannot figure out how to filter on the value.

Many thanks!
Erik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment