Skip to content

Commit 561bfd9

Browse files
committed
Split GithubClient::graphql_query into two methods.
This adds a method to add a generic "errors" handler, so callers don't need to worry about errors if they don't need specific ones. This also drops the generic type, since it wasn't really used.
1 parent d5e4458 commit 561bfd9

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/github.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,24 +2203,50 @@ impl GithubClient {
22032203
}
22042204

22052205
/// Issues an ad-hoc GraphQL query.
2206-
pub async fn graphql_query<T: serde::de::DeserializeOwned>(
2206+
///
2207+
/// You are responsible for checking the `errors` array when calling this
2208+
/// function to determine if there is an error. Only use this if you are
2209+
/// looking for specific error codes, or don't care about errors. Use
2210+
/// [`GithubClient::graphql_query`] if you would prefer to have a generic
2211+
/// error message.
2212+
pub async fn graphql_query_with_errors(
22072213
&self,
22082214
query: &str,
22092215
vars: serde_json::Value,
2210-
) -> anyhow::Result<T> {
2216+
) -> anyhow::Result<serde_json::Value> {
22112217
self.json(self.post(&self.graphql_url).json(&serde_json::json!({
22122218
"query": query,
22132219
"variables": vars,
22142220
})))
22152221
.await
22162222
}
22172223

2224+
/// Issues an ad-hoc GraphQL query.
2225+
///
2226+
/// See [`GithubClient::graphql_query_with_errors`] if you need to check
2227+
/// for specific errors.
2228+
pub async fn graphql_query(
2229+
&self,
2230+
query: &str,
2231+
vars: serde_json::Value,
2232+
) -> anyhow::Result<serde_json::Value> {
2233+
let result: serde_json::Value = self.graphql_query_with_errors(query, vars).await?;
2234+
if let Some(errors) = result["errors"].as_array() {
2235+
let messages: Vec<_> = errors
2236+
.iter()
2237+
.map(|err| err["message"].as_str().unwrap_or_default())
2238+
.collect();
2239+
anyhow::bail!("error: {}", messages.join("\n"));
2240+
}
2241+
Ok(result)
2242+
}
2243+
22182244
/// Returns the object ID of the given user.
22192245
///
22202246
/// Returns `None` if the user doesn't exist.
22212247
pub async fn user_object_id(&self, user: &str) -> anyhow::Result<Option<String>> {
22222248
let user_info: serde_json::Value = self
2223-
.graphql_query(
2249+
.graphql_query_with_errors(
22242250
"query($user:String!) {
22252251
user(login:$user) {
22262252
id
@@ -2273,7 +2299,7 @@ impl GithubClient {
22732299
// work on forks. This GraphQL query seems to work fairly reliably,
22742300
// and seems to cost only 1 point.
22752301
match self
2276-
.graphql_query::<serde_json::Value>(
2302+
.graphql_query_with_errors(
22772303
"query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
22782304
repository(owner: $repository_owner, name: $repository_name) {
22792305
defaultBranchRef {

0 commit comments

Comments
 (0)