construct_query.RdSends a GraphQL query to the Monday.com v2 API with optional variables,
adds sane timeouts, and automatically retries on transient failures
(HTTP 429 rate limits and 5xx server errors). If the HTTP request
succeeds but the GraphQL response contains an errors field, the function
throws an informative R error. On success, it returns the parsed data
payload from the response.
construct_query(
init_query,
variables = NULL,
api_key = Sys.getenv("MONDAY_API_KEY"),
max_tries = 5L,
timeout = 30
)Character scalar. The GraphQL query string. You may
declare variables in the signature (e.g., query ($boardId: ID!) { ... })
and reference them within the query using $boardId.
Named list or NULL. Concrete values for the GraphQL
variables declared in init_query, e.g., list(boardId=9595268725, limit=25).
Names must match the variable names in init_query without the $.
Use this to avoid string-pasting values into the query.
Character scalar. Monday API key. Defaults to
Sys.getenv("MONDAY_API_KEY"). Must be a valid "User" or "OAuth" token
with access to the requested resources.
Integer. Maximum number of attempts when retrying transient
failures (HTTP 429 or >=500). Defaults to 5L.
Numeric. Request timeout in seconds for each attempt.
Defaults to 30.
A named list corresponding to the GraphQL data object
(i.e., not the full HTTP response). If the response includes GraphQL
errors, the function calls stop() with the server-provided messages.
Adds headers Authorization and API-Version: 2023-10.
Retries only when httr2::resp_status() is 429 or >=500.
Distinguishes HTTP-layer errors from GraphQL-layer errors:
non-2xx responses are converted to informative errors that include the
response body when available; 2xx responses with a non-empty errors
field raise a GraphQL error in R.
HTTP errors: Transient (429, 5xx) → retried up to max_tries.
Non-transient (e.g., 400, 401, 403, 404) → immediate error with
status code and any response text.
GraphQL errors: If {"errors":[...]} is present, their messages are
concatenated and raised via stop(call.=FALSE).
httr2::req_retry() for retry behavior
httr2::req_timeout() for request timeouts
# Basic query (no variables)
qry <- "query { me { id name } }"
me <- construct_query(qry)
#> Error in abort(c(message, resp_auth_message(resp), i = info), status = status, resp = resp, request = req, class = c(glue("httr2_http_{status}"), "httr2_http", "httr2_error", "rlang_error"), use_cli_format = TRUE, call = error_call): `message` must be a character vector, not a list.
# Query with variables (preferred for dynamic values)
qry2 <- "
query ($boardId: ID!, $limit: Int = 25) {
boards(ids: [$boardId]) {
id
name
items_page(limit: $limit) { items { id name } }
}
}
"
res <- construct_query(
init_query = qry2,
variables = list(boardId = 9595268725, limit = 10)
)
#> Error in abort(c(message, resp_auth_message(resp), i = info), status = status, resp = resp, request = req, class = c(glue("httr2_http_{status}"), "httr2_http", "httr2_error", "rlang_error"), use_cli_format = TRUE, call = error_call): `message` must be a character vector, not a list.