Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ghes graphql endpoint error #135

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading