AlexUA
AlexUA

Reputation: 747

Aggregate specific properties from GitHub API

Instead of getting all properties, I just need to have certain.

This is what I am doing right now, but this way, I am getting a bunch of the properties I don't need:

await fetch(
          "https://api.github.com/search/repositories?q=calculator&per_page=100&type=all&language=&sort=stargazers",
          {
            json: true,
          }
        ).then((res) => res.json())

I need to have only these properties: html_url, name, description, updated_at, stargazers_count, forks_count, id

Upvotes: 0

Views: 86

Answers (1)

Slava Knyazev
Slava Knyazev

Reputation: 6081

The GitHub REST API does not provide options for limiting your response, but the GraphQL API does. In your case, your query would look like this:

{
  search(query: "calculator", type: REPOSITORY, first: 100) {
    edges {
      node {
        __typename
        ... on Repository {
          id
          name
          url
          description
          updatedAt
          stargazerCount
          forkCount
        }
      }
    }
  }
}

You can try it in the explorer: https://docs.github.com/en/graphql/overview/explorer

Sample output:

{
  "data": {
    "search": {
      "edges": [
        {
          "node": {
            "__typename": "Repository",
            "id": "MDEwOlJlcG9zaXRvcnkxNjgwMDg3OTc=",
            "name": "calculator",
            "url": "https://github.com/microsoft/calculator",
            "description": "Windows Calculator: A simple yet powerful calculator that ships with Windows",
            "updatedAt": "2023-01-19T16:35:31Z",
            "stargazerCount": 26550,
            "forkCount": 4820
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions