Skip to content

Commit

Permalink
🐛 Fix: ghes graphql endpoint error (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Sep 28, 2024
1 parent 7c25ee3 commit c82b0e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ github = GitHub(
)
```

The `base_url` option is used to set the base URL of the GitHub API. If you are using GitHub Enterprise Server, you need to include the `/api/v3` path in the base URL.

The `accept_format` and `previews` are used to set the default `Accept` header, you can find more details in [GitHub API docs](https://docs.github.com/en/rest/overview/media-types).

The `http_cache` option enables the http caching feature powered by [Hishel](https://hishel.com/) for HTTPX. GitHub API limits the number of requests that you can make within a specific amount of time. This feature is useful to reduce the number of requests to GitHub API and avoid hitting the rate limit.
Expand Down
23 changes: 21 additions & 2 deletions githubkit/graphql/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from weakref import ref
from typing import TYPE_CHECKING, Any, Dict, Optional, cast

Expand Down Expand Up @@ -35,6 +36,18 @@ def build_graphql_request(
json["variables"] = variables
return json

def _get_graphql_endpoint(self) -> str:
base_url = self._github.config.base_url
path = base_url.path
# workaround for GitHub Enterprise baseUrl set with /api/v3 suffix
# https://github.com/octokit/graphql.js/pull/186
# https://github.com/octokit/graphql.js/blob/dae781b027c19bcd458577cd9ac6ca888b2fdfeb/src/graphql.ts#L67-L72
path, n = re.subn(r"/api/v3/?$", "/api/graphql", path, count=1)
# if replaced, return the new path
if n:
return str(base_url.copy_with(path=path))
return "/graphql"

def parse_graphql_response(
self, response: "Response[GraphQLResponse]"
) -> Dict[str, Any]:
Expand All @@ -57,7 +70,10 @@ def _request(
json = self.build_graphql_request(query, variables)

return self._github.request(
"POST", "/graphql", json=json, response_model=GraphQLResponse
"POST",
self._get_graphql_endpoint(),
json=json,
response_model=GraphQLResponse,
)

def request(
Expand All @@ -71,7 +87,10 @@ async def _arequest(
json = self.build_graphql_request(query, variables)

return await self._github.arequest(
"POST", "/graphql", json=json, response_model=GraphQLResponse
"POST",
self._get_graphql_endpoint(),
json=json,
response_model=GraphQLResponse,
)

async def arequest(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_graphql/test_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from githubkit import GitHub


def test_github_api_endpoint():
g = GitHub(base_url="https://api.github.com")
assert g.graphql._get_graphql_endpoint() == "/graphql"


def test_ghes_api_endpoint():
g = GitHub(base_url="https://example.github.com/api/v3")
assert g.graphql._get_graphql_endpoint() == "https://example.github.com/api/graphql"

0 comments on commit c82b0e7

Please sign in to comment.