Skip to content

Run GraphQL requests from CLI

Run a single GraphQL script from a file using the built-in CLI in CloudBeaver

Prerequisites

  • make sure the server container is running
  • have an API access token
  • prepare a file with exactly one GraphQL operation, save it in any format (such as.json or .txt), and make sure the content is valid JSON

Tip

with Docker Compose, either mount the script file or a folder of scripts into the service container at startup, or copy the file into the running container.

Write a GraphQL script file

Create a JSON file with query, variables, and operationName. For schema details and more operations, see GraphQL API overview.

Example

{
  "query": "mutation createConnection($projectId: ID!, $config: ConnectionConfig!) { connection: createConnection(projectId: $projectId, config: $config) { ...DatabaseConnection } } fragment DatabaseConnection on ConnectionInfo { id projectId name }",
  "variables": {
    "projectId": "g_GlobalConfiguration",
    "config": {
      "configurationType": "MANUAL",
      "credentials": { "userPassword": "qwe", "userName": "qwe" },
      "mainPropertyValues": { "host": "localhost", "port": "3306" },
      "networkHandlersConfig": [],
      "providerProperties": {},
      "driverId": "mysql-ee:mysql8",
      "name": "MySQL@localhost",
      "host": "localhost",
      "port": "3306",
      "databaseName": null,
      "authModelId": "native",
      "properties": {}
    }
  },
  "operationName": "createConnection"
}

For more ready-to-run examples, see the CloudBeaver graphql examples repository.

Enter the running container

Open a shell in the Compose service container:

docker compose exec <service_name> bash

List services, and get the container id for a service:

docker compose ps
docker compose ps -q <service_name>

Copy your script from the host into the service container:

docker cp /path/on/host/create-connection.json $(docker compose ps -q <service_name>):/tmp/create-connection.json

Tip

if you use the older v1 CLI, replace docker compose with docker-compose.

Execute the script

Run the command:

docker compose exec <service_name> ./server-cli.sh \
  -input-file /tmp/create-connection.json \
  -gql \
  -access-token <your_api_token>
  • -input-file - path to the script file inside the container
  • -gql - runs the file as a GraphQL request
  • -access-token - your API token string

The command prints the GraphQL response to stdout in JSON, including data, and, if present, errors.

Save output to a file

By default, the server CLI prints the GraphQL response to stdout. If you want to save the response into a file inside the container, use the -output-file option:

docker compose exec <service_name> ./server-cli.sh \
  -input-file /tmp/create-connection.json \
  -output-file /opt/cloudbeaver/workspace/out.txt \
  -gql \
  -access-token <your_api_token>
  • -output-file - path to the output file inside the container. The server CLI writes the GraphQL response to this file.

    Tip

    Not every path inside the container is valid for writing. A safe location is /opt/cloudbeaver/workspace/.

Use in AWS

If you run CloudBeaver on AWS, do the same steps inside the instance: place the JSON file on the instance, then run the CLI there.

Info

For connection and file transfer basics, see AWS documentation: