-
Notifications
You must be signed in to change notification settings - Fork 0
Batch Operations
The batch command executes multiple GraphQL operations in a single HTTP request. It's useful for pipelines, bulk operations, and feeding results through jq transformations.
batch reads from stdin or --file. Each operation is a JSON object:
{"query": "{ users { id name } }"}
{"query": "{ posts { id title } }"}One JSON object per line. Uses Content-Type: application/x-ndjson:
printf '{"query":"{ users { id } }"}\n{"query":"{ posts { id } }"}\n' \
| gqlcli batchStandard GraphQL batch format. Uses Content-Type: application/json:
printf '[{"query":"{ users { id } }"},{"query":"{ posts { id } }"}]' \
| gqlcli batch --arraygqlcli batch --file operations.ndjsonExample operations.ndjson:
{"query":"{ users { id name status } }"}
{"query":"{ posts { id title } }"}
{"query":"query GetUser($id: ID!) { user(id: $id) { id name } }","variables":{"id":"1"}}Each operation can include a "jq" field. The response is filtered before being returned:
printf '{"query":"{ users { id name status } }","jq":".data.users[] | select(.status == \"active\") | .name"}\n' \
| gqlcli batchUseful jq expressions for the "jq" field:
| Expression | Effect |
|---|---|
.data |
Strip the GraphQL envelope |
.data.users[].name |
Extract a field from each element |
.data.users | length |
Count results |
.data.users[] | select(.active) |
Filter array items |
.data.logs[] | select(.message | test("error")) |
Regex match |
.data | {count: (.users | length)} |
Reshape response |
Apply a single jq expression to every response in the batch:
printf '{"query":"{ users { id name } }"}\n{"query":"{ posts { id title } }"}\n' \
| gqlcli batch --jq '.data'Query inactive users, then archive each one:
gqlcli query -q '{ users { id status } }' -f json \
| jq -c '.data.users[] | select(.status == "inactive") | {query: "mutation($id:ID!){archive(id:$id){ok}}", variables: {id: .id}}' \
| gqlcli batchRun multiple queries and aggregate output:
cat <<'EOF' | gqlcli batch --jq '.data'
{"query":"{ users { id name } }"}
{"query":"{ groups { id name } }"}
{"query":"{ roles { id name } }"}
EOF# Generate one mutation per ID from a file
cat ids.txt | while read id; do
echo "{\"query\":\"mutation(\$id:ID!){deleteUser(id:\$id){ok}}\",\"variables\":{\"id\":\"$id\"}}"
done | gqlcli batchEach operation can include a "variables" field:
{"query":"query GetUser($id: ID!) { user(id: $id) { id name } }","variables":{"id":"1"}}
{"query":"query GetUser($id: ID!) { user(id: $id) { id name } }","variables":{"id":"2"}}gqlcli batch --file ops.ndjson --debugPrints request and response details for each operation.