Skip to main content

async_graphql_test/matchers/
should_be_defined.rs

1use async_graphql::async_trait::async_trait;
2use async_graphql::{Context, CustomDirective, Directive, ResolveFut, ServerResult, Value};
3
4use super::MatcherError;
5
6#[Directive(location = "Field", name = "shouldBeDefined")]
7pub fn should_be_defined() -> impl CustomDirective {
8    ShouldBeDefined
9}
10
11struct ShouldBeDefined;
12
13#[async_trait]
14impl CustomDirective for ShouldBeDefined {
15    async fn resolve_field(
16        &self,
17        ctx: &Context<'_>,
18        resolve: ResolveFut<'_>,
19    ) -> ServerResult<Option<Value>> {
20        match resolve.await {
21            Ok(None) => Err(MatcherError::new(
22                ctx.item.pos,
23                "Received: undefined".to_string(),
24            )),
25            Ok(Some(other)) => Ok(Some(other)),
26            Err(err) => Err(MatcherError::unexpected_error(err)),
27        }
28    }
29}