DEV Community

Cover image for Access GitHub GraphQL queries easily in Python
Dillon Barnes
Dillon Barnes

Posted on

Access GitHub GraphQL queries easily in Python

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
Enter fullscreen mode Exit fullscreen mode

Usage

For full information on the functions, check out the documentation. However, here's a small sample of what you can do.

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"]}')
Enter fullscreen mode Exit fullscreen mode

This returns the following:

{
    "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
Enter fullscreen mode Exit fullscreen mode

Links:

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.

Top comments (0)