Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(webserver): always use schema::Result for graphql api responses #1892

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions ee/tabby-webserver/src/juniper/relay/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::Future;

use juniper::FieldResult;
use anyhow::anyhow;

mod connection;
mod edge;
Expand All @@ -10,25 +10,33 @@
pub use connection::Connection;
pub use node_type::NodeType;

use crate::schema;

fn validate_first_last(
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<(Option<usize>, Option<usize>)> {
) -> schema::Result<(Option<usize>, Option<usize>)> {
if first.is_some() && last.is_some() {
return Err("The \"first\" and \"last\" parameters cannot exist at the same time".into());
Err(anyhow!(
"The \"first\" and \"last\" parameters cannot exist at the same time"
))?;

Check warning on line 22 in ee/tabby-webserver/src/juniper/relay/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/juniper/relay/mod.rs#L20-L22

Added lines #L20 - L22 were not covered by tests
}

let first = match first {
Some(first) if first < 0 => {
return Err("The \"first\" parameter must be a non-negative number".into());
return Err(anyhow!(
"The \"first\" parameter must be a non-negative number"
))?;

Check warning on line 29 in ee/tabby-webserver/src/juniper/relay/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/juniper/relay/mod.rs#L27-L29

Added lines #L27 - L29 were not covered by tests
}
Some(first) => Some(first as usize),
None => None,
};

let last = match last {
Some(last) if last < 0 => {
return Err("The \"last\" parameter must be a non-negative number".into());
return Err(anyhow!(
"The \"last\" parameter must be a non-negative number"
))?;

Check warning on line 39 in ee/tabby-webserver/src/juniper/relay/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/juniper/relay/mod.rs#L37-L39

Added lines #L37 - L39 were not covered by tests
}
Some(last) => Some(last as usize),
None => None,
Expand All @@ -43,15 +51,15 @@
first: Option<i32>,
last: Option<i32>,
f: F,
) -> FieldResult<Connection<Node>>
) -> schema::Result<Connection<Node>>

Check warning on line 54 in ee/tabby-webserver/src/juniper/relay/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/juniper/relay/mod.rs#L54

Added line #L54 was not covered by tests
where
Node: NodeType + Sync,
F: FnOnce(
Option<String>,
Option<String>,
Option<usize>,
Option<usize>,
) -> FieldResult<Vec<Node>>,
) -> schema::Result<Vec<Node>>,

Check warning on line 62 in ee/tabby-webserver/src/juniper/relay/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/juniper/relay/mod.rs#L62

Added line #L62 was not covered by tests
{
let (first, last) = validate_first_last(first, last)?;

Expand All @@ -75,11 +83,11 @@
first: Option<i32>,
last: Option<i32>,
f: F,
) -> FieldResult<Connection<Node>>
) -> schema::Result<Connection<Node>>
where
Node: NodeType + Sync,
F: FnOnce(Option<String>, Option<String>, Option<usize>, Option<usize>) -> R,
R: Future<Output = FieldResult<Vec<Node>>>,
R: Future<Output = schema::Result<Vec<Node>>>,
{
let (first, last) = validate_first_last(first, last)?;

Expand Down
61 changes: 23 additions & 38 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use chrono::{DateTime, Utc};
use job::{JobRun, JobService};
use juniper::{
graphql_object, graphql_value, EmptySubscription, FieldError, FieldResult, GraphQLObject,
IntoFieldError, Object, RootNode, ScalarValue, Value, ID,
graphql_object, graphql_value, EmptySubscription, FieldError, GraphQLObject, IntoFieldError,
Object, RootNode, ScalarValue, Value, ID,
};
use tabby_common::api::{code::CodeSearch, event::EventLogger};
use tracing::error;
Expand Down Expand Up @@ -182,23 +182,18 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<User>> {
) -> Result<Connection<User>> {
check_admin(ctx).await?;
return relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
match ctx
.locator
ctx.locator

Check warning on line 193 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L193

Added line #L193 was not covered by tests
.auth()
.list_users(after, before, first, last)
.await
{
Ok(users) => Ok(users),
Err(err) => Err(FieldError::from(err)),
}
},
)
.await;
Expand All @@ -210,23 +205,18 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<Invitation>> {
) -> Result<Connection<Invitation>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
match ctx
.locator
ctx.locator

Check warning on line 216 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L216

Added line #L216 was not covered by tests
.auth()
.list_invitations(after, before, first, last)
.await
{
Ok(invitations) => Ok(invitations),
Err(err) => Err(FieldError::from(err)),
}
},
)
.await
Expand All @@ -238,19 +228,18 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<GithubRepositoryProvider>> {
) -> Result<Connection<GithubRepositoryProvider>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
Ok(ctx
.locator
ctx.locator

Check warning on line 239 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L239

Added line #L239 was not covered by tests
.github_repository_provider()
.list_github_repository_providers(after, before, first, last)
.await?)
.await

Check warning on line 242 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L242

Added line #L242 was not covered by tests
},
)
.await
Expand All @@ -263,16 +252,15 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<GithubProvidedRepository>> {
) -> Result<Connection<GithubProvidedRepository>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
Ok(ctx
.locator
ctx.locator

Check warning on line 263 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L263

Added line #L263 was not covered by tests
.github_repository_provider()
.list_github_provided_repositories_by_provider(
provider_ids,
Expand All @@ -281,7 +269,7 @@
first,
last,
)
.await?)
.await

Check warning on line 272 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L272

Added line #L272 was not covered by tests
},
)
.await
Expand All @@ -295,26 +283,25 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<JobRun>> {
) -> Result<Connection<JobRun>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
Ok(ctx
.locator
ctx.locator

Check warning on line 294 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L294

Added line #L294 was not covered by tests
.job()
.list(ids, jobs, after, before, first, last)
.await?)
.await

Check warning on line 297 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L297

Added line #L297 was not covered by tests
},
)
.await
}

async fn job_run_stats(ctx: &Context, jobs: Option<Vec<String>>) -> FieldResult<JobStats> {
Ok(ctx.locator.job().compute_stats(jobs).await?)
async fn job_run_stats(ctx: &Context, jobs: Option<Vec<String>>) -> Result<JobStats> {
ctx.locator.job().compute_stats(jobs).await
}

async fn email_setting(ctx: &Context) -> Result<Option<EmailSetting>> {
Expand Down Expand Up @@ -345,19 +332,18 @@
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> FieldResult<Connection<Repository>> {
) -> Result<Connection<Repository>> {
check_admin(ctx).await?;
relay::query_async(
after,
before,
first,
last,
|after, before, first, last| async move {
Ok(ctx
.locator
ctx.locator

Check warning on line 343 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L343

Added line #L343 was not covered by tests
.repository()
.list_repositories(after, before, first, last)
.await?)
.await

Check warning on line 346 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L346

Added line #L346 was not covered by tests
},
)
.await
Expand All @@ -367,13 +353,12 @@
ctx: &Context,
repository_name: String,
pattern: String,
) -> FieldResult<Vec<FileEntrySearchResult>> {
) -> Result<Vec<FileEntrySearchResult>> {
check_claims(ctx)?;
Ok(ctx
.locator
ctx.locator
.repository()
.search_files(&repository_name, &pattern, 40)
.await?)
.await
}

async fn oauth_credential(
Expand Down
12 changes: 7 additions & 5 deletions rules/use-schema-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ language: rust
files:
- ./ee/tabby-webserver/src/schema/**
rule:
pattern: anyhow
not:
inside:
kind: enum_variant
stopBy: end
any:
- pattern: anyhow
not:
inside:
kind: enum_variant
stopBy: end
- pattern: FieldResult
Loading