# GraphQL Python Client

### Install the client

Install a GraphQL client like "python-graphql-client":

```
pip install python-graphql-client
```

### Run the ObjectBox server

Your ObjectBox Sync Server also comes with a native GraphQL server, making ObjectBox a GraphQL database that clients can query.

Note that the port used for GraphQL is different than the one used for Sync.

### Get the session ID

In order to make GraphQL requests, you must first obtain a session ID.

Make an empty plain HTTP POST request to `/api/v2/sessions` in order to receive a session ID. E.g. using the [requests](https://pypi.org/project/requests/) library:

```
import requests

url = 'http://localhost:8081/api/v2/sessions'

print(requests.post(url).text)
```

### GraphQL requests

Now everything is ready for you to do the GraphQL requests. Note that the session ID you found in the previous step must be passed to all GraphQL requests. You can provide it via e.g. using argparse. It's passed via the standard `Authorization` header (see example below).

Here is a simple example of using the client to read all existing objects from the database.

```
from python_graphql_client import GraphqlClient
import requests

url = 'http://localhost:8081/api/v2/sessions'
token = requests.post(url).text
header = {
    'Authorization': 'Bearer ' + token[1:-1]
}

client = GraphqlClient(endpoint="http://localhost:8081/api/graphql", headers = header)

# Read existing objects
read = """
query getAll {
  testEntity {
    id
    simpleInt
    simpleString
  }
}
"""

# Synchronous request
data = client.execute(query=put)
print(data)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sync.objectbox.io/sync-server/graphql-database/graphql-python-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
