Repository: opsmill/infrahub-sdk-python

Issue:
bug: `execute_graphql` does not consider `default_branch` setting on `InfrahubClient`
### Component

Python SDK

### Infrahub SDK version

0.13.1

### Current Behavior

The `execute_graphql` method of `InfrahubClient` does not consider the `default_branch` setting on `InfrahubClient`.

### Expected Behavior

`execute_graphql` executes the the graphql query in the branch that is set as `default_branch`

### Steps to Reproduce

- Spin up an instance of Infrahub
- Create a branch `test`
- Create a `BuiltinTag` node in the `test` branch
- Run the following script
```python
from infrahub_sdk import InfrahubClientSync, Config
client = InfrahubClientSync(config=Config(default_branch="test"))
client.execute_graphql(query="query { BuiltinTag { count }}") # result count will be 0
client.execute_graphql(query="query { BuiltinTag { count }}", branch_name="test") # result count will be 1
```

### Additional Information

The `branch_name` argument that you can use seems to deviate from how we typically name this argument in other methods in the SDK. Typically the name would be `branch`.

Discussion:

Patch:
diff --git a/changelog/46.fixed.md b/changelog/46.fixed.md
new file mode 100644
index 0000000..b21af46
--- /dev/null
+++ b/changelog/46.fixed.md
@@ -0,0 +1,1 @@
+`execute_graphql` method for InfrahubClient(Sync) now properly considers the `default_branch` setting
diff --git a/infrahub_sdk/client.py b/infrahub_sdk/client.py
index 3422535..74f32d0 100644
--- a/infrahub_sdk/client.py
+++ b/infrahub_sdk/client.py
@@ -575,6 +575,7 @@ class InfrahubClient(BaseClient):
             _type_: _description_
         """
 
+        branch_name = branch_name or self.default_branch
         url = self._graphql_url(branch_name=branch_name, at=at)
 
         payload: dict[str, Union[str, dict]] = {"query": query}
@@ -1112,6 +1113,7 @@ class InfrahubClientSync(BaseClient):
             dict: The result of the GraphQL query or mutation.
         """
 
+        branch_name = branch_name or self.default_branch
         url = self._graphql_url(branch_name=branch_name, at=at)
 
         payload: dict[str, Union[str, dict]] = {"query": query}


Test patch:
diff --git a/tests/unit/sdk/conftest.py b/tests/unit/sdk/conftest.py
index a870cfd..680bc4a 100644
--- a/tests/unit/sdk/conftest.py
+++ b/tests/unit/sdk/conftest.py
@@ -1958,25 +1958,25 @@ async def mock_rest_api_artifact_generate(httpx_mock: HTTPXMock) -> HTTPXMock:
 @pytest.fixture
 async def mock_query_mutation_schema_dropdown_add(httpx_mock: HTTPXMock) -> None:
     response = {"data": {"SchemaDropdownAdd": {"ok": True}}}
-    httpx_mock.add_response(method="POST", url="http://mock/graphql", json=response)
+    httpx_mock.add_response(method="POST", url="http://mock/graphql/main", json=response)
 
 
 @pytest.fixture
 async def mock_query_mutation_schema_dropdown_remove(httpx_mock: HTTPXMock) -> None:
     response = {"data": {"SchemaDropdownRemove": {"ok": True}}}
-    httpx_mock.add_response(method="POST", url="http://mock/graphql", json=response)
+    httpx_mock.add_response(method="POST", url="http://mock/graphql/main", json=response)
 
 
 @pytest.fixture
 async def mock_query_mutation_schema_enum_add(httpx_mock: HTTPXMock) -> None:
     response = {"data": {"SchemaEnumAdd": {"ok": True}}}
-    httpx_mock.add_response(method="POST", url="http://mock/graphql", json=response)
+    httpx_mock.add_response(method="POST", url="http://mock/graphql/main", json=response)
 
 
 @pytest.fixture
 async def mock_query_mutation_schema_enum_remove(httpx_mock: HTTPXMock) -> None:
     response = {"data": {"SchemaEnumRemove": {"ok": True}}}
-    httpx_mock.add_response(method="POST", url="http://mock/graphql", json=response)
+    httpx_mock.add_response(method="POST", url="http://mock/graphql/main", json=response)
 
 
 @pytest.fixture
diff --git a/tests/unit/sdk/test_client.py b/tests/unit/sdk/test_client.py
index 50dc0b3..eb0f093 100644
--- a/tests/unit/sdk/test_client.py
+++ b/tests/unit/sdk/test_client.py
@@ -607,7 +607,7 @@ async def test_allocate_next_ip_prefix(
     assert ip_prefix.description.value == "test"
 
 
-EXPECTED_ECHO = """URL: http://mock/graphql
+EXPECTED_ECHO = """URL: http://mock/graphql/main
 QUERY:
 
     query GetTags($name: String!) {