Skip to content

Commit f3ca4c5

Browse files
authored
feat: add support for GraphQL endpoint (#189)
1 parent 86fd49a commit f3ca4c5

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,31 @@ class FirstCrowdinClient(CrowdinClient):
159159
EXTENDED_REQUEST_PARAMS = {"proxies": proxies}
160160
```
161161

162+
### GraphQL API
163+
164+
This library also provides the possibility to use [GraphQL API](https://developer.crowdin.com/graphql-api/):
165+
166+
```python
167+
from crowdin_api import CrowdinClient
168+
169+
client = CrowdinClient(
170+
token='{token}',
171+
organization='{organization}'
172+
)
173+
174+
query = """
175+
query {
176+
viewer {
177+
id
178+
name
179+
}
180+
}
181+
"""
182+
183+
# Execute the GraphQL query
184+
response = client.graphql(query=query)
185+
```
186+
162187
## Seeking Assistance
163188

164189
If you find any problems or would like to suggest a feature, please read the [How can I contribute](https://github.com/crowdin/crowdin-api-client-python/blob/main/CONTRIBUTING.md#how-can-i-contribute) section in our contributing guidelines.

crowdin_api/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,19 @@ def get_api_requestor(self) -> APIRequester:
9191
default_headers=self.get_default_headers(),
9292
extended_params=self.EXTENDED_REQUEST_PARAMS
9393
)
94-
9594
return self._api_requestor
9695

96+
def graphql(self, query: str, variables: Optional[Dict] = None) -> Dict:
97+
data = {
98+
"query": query,
99+
"variables": variables or {}
100+
}
101+
return self.get_api_requestor().request(
102+
method="post",
103+
path="graphql",
104+
request_data=data
105+
)
106+
97107
@property
98108
def ai(self) -> Union[api_resources.AIResource, api_resources.EnterpriseAIResource]:
99109
if self._is_enterprise_platform:

crowdin_api/tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,25 @@ def test_storages(self, _m_api_requestor, property_name, class_name):
210210
requester="api_requestor", project_id=1, page_size=25
211211
)
212212

213+
@mock.patch("crowdin_api.client.CrowdinClient.get_api_requestor")
214+
def test_graphql(self, mock_get_requestor):
215+
"""Test GraphQL functionality with basic request validation."""
216+
client = CrowdinClient()
217+
assert isinstance(client, CrowdinClient)
218+
219+
mock_requestor = mock.Mock()
220+
mock_get_requestor.return_value = mock_requestor
221+
mock_requestor.request.return_value = {"data": {"test": True}}
222+
223+
query = "query { test }"
224+
client.graphql(query=query)
225+
226+
mock_requestor.request.assert_called_once()
227+
call_args = mock_requestor.request.call_args[1]
228+
assert call_args["method"] == "post"
229+
assert call_args["path"] == "graphql"
230+
assert call_args["request_data"]["query"] == query
231+
213232

214233
class TestCrowdinClientEnterprise:
215234
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)