Skip to content

Instantly share code, notes, and snippets.

@gbaman
Created November 1, 2017 00:18
Show Gist options
  • Save gbaman/b3137e18c739e0cf98539bf4ec4366ad to your computer and use it in GitHub Desktop.
Save gbaman/b3137e18c739e0cf98539bf4ec4366ad to your computer and use it in GitHub Desktop.
An example on using the Github GraphQL API with Python 3
# An example to get the remaining rate limit using the Github GraphQL API.
import requests
headers = {"Authorization": "Bearer YOUR API KEY"}
def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
# The GraphQL query (with a few aditional bits included) itself defined as a multi-line string.
query = """
{
viewer {
login
}
rateLimit {
limit
cost
remaining
resetAt
}
}
"""
result = run_query(query) # Execute the query
remaining_rate_limit = result["data"]["rateLimit"]["remaining"] # Drill down the dictionary
print("Remaining rate limit - {}".format(remaining_rate_limit))
@els-pnw
Copy link

els-pnw commented May 27, 2022

Is there a way to search for all issues across an org? Using REST I would do https://api.github.com/search/issues?q=user:{org_name}&per_page=100&page=1 Any help reformatting this for Graphql would be appreciated.

@SD-13
Copy link

SD-13 commented Jul 13, 2023

@els-pnw I was trying to find a way of doing it with graphql explorer but got stuck for this error

"message": "Although you appear to have the correct authorization credentials, the kubernetes organization has enabled OAuth App access restrictions, meaning that data access to third-parties is limited. For more information on these restrictions, including how to enable this app, visit https://docs.github.com/articles/restricting-access-to-your-organization-s-data/"

I ended up with something like this

query ($org_name: String!) {
    organization(login: $org_name) {
        repositories(last: 3) {
            nodes {
                issues(last: 2) {
                    nodes {
                        number
                        assignees {
                            edges {
                                node {
                                    name
                                }
                            }
                        }
                        author {
                            login
                        }
                        state
                    }
                }
            }
        }
    }
}

Not sure how much it will help you, again the explorer is nice, just play with it, hope you will get your answer.

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