Access GitHub GraphQL queries easily in Python
Tags: programming, python, github, utilities
Published: 2022-02-21 18:35:05

Hi!

In this post I'm going to share a Python package that aims to make it easy to use GitHub's GraphQL API in projects. No more fiddling around with queries and turning them into Python dictionaries! Interested? Read on!

## What is it?

Recently, I've been developing a package for Python called `pygitapi`. This package will utilise the GitHub GraphQL API to get data for the user. *BitBucket and GitLab support are planned for future releases.*

The main aim of the package is to make it easy to use the GraphQL API and get the info you need.

## Installation

To install via pip, do the following:
```
pip install --upgrade pygitapi
```

## Usage

For full information on the functions, check out the [documentation](https://docs.pygitapi.cf). However, here's a small sample of what you can do.

```python
from pygitapi import HubAPI
import json

h = HubAPI('PERSONAL_ACCESS_TOKEN')

response = h.user_info('DillonB07')

print(json.dumps(response, indent=4))

print(f'Portfolio - {response["user"]["websiteUrl"]}')
```

This returns the following:
```json
{
    "user": {
        "avatarUrl": "https://avatars.githubusercontent.com/u/83948303?v=4",
        "bio": "An aspiring web developer who likes making weird and useless projects",
        "company": "Replit Coder | Hobby Coder",
        "email": "dillonbarnes07@gmail.com",
        "followers": {
            "totalCount": 7
        },
        "following": {
            "totalCount": 17
        },
        "isCampusExpert": false,
        "isDeveloperProgramMember": true,
        "issues": {
            "totalCount": 60
        },
        "itemShowcase": {
            "hasPinnedItems": true,
            "items": {
                "nodes": [
                    {
                        "name": "GitAPI"
                    },
                    {
                        "name": "Portfolio"
                    },
                    {
                        "name": "QAPI"
                    },
                    {
                        "name": "Dizzle"
                    },
                    {
                        "name": "Spotter"
                    },
                    {
                        "name": "FlaskBoilerplate"
                    }
                ]
            }
        },
        "location": "GMT, Leicester",
        "login": "DillonB07",
        "name": "Dillon Barnes",
        "organizations": {
            "nodes": [],
            "totalCount": 0
        },
        "pullRequests": {
            "totalCount": 38
        },
        "repositories": {
            "totalCount": 41
        },
        "starredRepositories": {
            "totalCount": 76
        },
        "status": null,
        "twitterUsername": null,
        "url": "https://github.com/DillonB07",
        "websiteUrl": "https://dillonb07.is-a.dev"
    }
}

Portfolio - https://dillonb07.is-a.dev
```

## Links:
* GitHub - https://github.com/DillonB07/GitAPI
* PyPi - https://pypi.org/project/pygitapi
* Docs - https://docs.pygitapi.cf


Thanks for reading! I hope that this package will be useful to you. If there are any features that you'd like to see added, write a comment below, or create an issue on GitHub.