What causes seemingly sporatic APIv4 502 errors?

Hello,

I’m using the following Python 3.8 snippet to query GitHub’s v4 GraphQL API for commit information on a repository (I’ve copy pasted this snippet from a longer script so excuse me if there are typos! but the gist of it is definitely here):

from string import Template
import requests

# GitHub API GraphQL API endpoint
GITHUB_API_URL: str = "https://api.github.com/graphql"
# Set up GitHub v4 API GraphQL request headers
auth_token = None # actual token goes here
graphql_headers: dict = {"Authorization": f"token {auth_token}"}

# GitHub repository's owner
GITHUB_REPO_OWNER: str = "AngelLM"
# GitHub repository's name
GITHUB_REPO_NAME: str = "Thor"

# Declare a function for GraphQL queries
def graphql_query(query: str):
    request = requests.post(url=GITHUB_API_URL, json={"query": query}, headers=graphql_headers)
    if request.status_code == 200:
        return request.json()["data"]
    else:
        raise Exception(f"Problem with query with return code {request.status_code}.")

# Actual GraphQL template
query_commits_template = Template(
"""
{
  repository(owner: "$owner", name: "$name") {
    refs(query: "$branch", refPrefix: "refs/heads/", first: 1) {
      nodes {
        target {
          ... on Commit {
            history(first: 100, after: $after, since: null, until: null) {
              nodes {
                oid
                commitUrl
                url
                messageHeadline
                changedFiles
                authoredByCommitter
                authoredDate
                author {
                  name
                  email
                  user {
                    email
                    login
                    name
                    twitterUsername
                  }
                  date
                }
                committedDate
                committer {
                  name
                  email
                  user {
                    email
                    login
                    name
                    twitterUsername
                  }
                  date
                }
                parents(first: 100) {
                  nodes {
                    oid
                  }
                  pageInfo {
                    hasNextPage
                    endCursor
                  }
                }
              }
              pageInfo {
                hasNextPage
                endCursor
              }
            }
          }
        }
      }
    }
  }
}

"""

# Prepare and execute GraphQL query for commits
branch = "BlenderController"
query_commits = '"9e70a1ca09933119e91d3ca189b591f869973d0b 99"'
query_commits_template.substitute(owner=GITHUB_REPO_OWNER, name=GITHUB_REPO_NAME, branch=branch, after=end_cursor)

# Make the query
graphql_query(query_commits)

Problem is, my queries sporadically get me 502 errors but not always and not for every repository.

In the snippet above, I’m querying the AngelLM/thor repository, and I often get 502s. But for others such as SafeCast/bGeigieNanoKit or one of my own open-next/wp2.2_dev, I never get 502 errors!

What are the causes of 502 API response codes, why do I get them sporadically (and not always), and how do I troubleshoot this? Thank you!

UPDATE: The text content of the 502 response includes the following which I don’t know what to do with:

“message”:“Something went wrong while executing your query. This may be the result of a timeout, or it could be a GitHub bug. Please include 5C5B:4274:284CCF0:5A43AEA:5F633DF0 when reporting this issue.”

UPDATE 2: The (practically) identical query works flawlessly with the GraphiQL Explorer (why?):

{
  repository(owner: "AngelLM", name: "thor") {
    refs(first: 100, query: "BlenderController", refPrefix: "refs/heads/") {
      nodes {
        name
        target {
          ... on Commit {
            history(first: 100, after: "9e70a1ca09933119e91d3ca189b591f869973d0b 99") {
              nodes {
                id
              }
            }
          }
        }
      }
    }
  }
}