Construct a resilient Monday.com GraphQL request
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.
Usage
construct_query(
init_query,
variables = NULL,
api_key = Sys.getenv("MONDAY_API_KEY"),
max_tries = 5L,
timeout = 60
)Arguments
- init_query
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.- variables
Named list or
NULL. Concrete values for the GraphQL variables declared ininit_query, e.g.,list(boardId=9595268725, limit=25). Names must match the variable names ininit_querywithout the$. Use this to avoid string-pasting values into the query.- api_key
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.- max_tries
Integer. Maximum number of attempts when retrying transient failures (HTTP
429or>=500). Defaults to5L.- timeout
Numeric. Request timeout in seconds for each attempt. Defaults to
60.
Value
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.
Details
Adds headers
AuthorizationandAPI-Version: 2023-10.Retries when
httr2::resp_status()is429or>=500, and on outright request failures such as connection timeouts (retry_on_failure = TRUE).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
errorsfield raise a GraphQL error in R.
Error handling
HTTP errors: Transient (
429,5xx) and curl-level failures (timeouts, dropped connections) → retried up tomax_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 viastop(call.=FALSE).
See also
httr2::req_retry()for retry behaviorhttr2::req_timeout()for request timeouts
Examples
# Basic query (no variables)
qry <- "query { me { id name } }"
me <- construct_query(qry)
#> Error in httr2::req_perform(req): HTTP 401 Unauthorized.
#> HTTP error 401 from monday.com
#> {"errors":["Not Authenticated"]}
# 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 httr2::req_perform(req): HTTP 401 Unauthorized.
#> HTTP error 401 from monday.com
#> {"errors":["Not Authenticated"]}