async_graphql_test/matchers/
should_be_null.rs1use 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 = "shouldBeNull")]
8pub fn should_be_null() -> impl CustomDirective {
9 ShouldBeNull
10}
11
12struct ShouldBeNull;
13
14#[async_trait]
15impl CustomDirective for ShouldBeNull {
16 async fn resolve_field(
17 &self,
18 ctx: &Context<'_>,
19 resolve: ResolveFut<'_>,
20 ) -> ServerResult<Option<Value>> {
21 match resolve.await {
22 Ok(None) => Ok(None),
23 Ok(Some(Value::Null)) => Ok(Some(Value::Null)),
24 Ok(Some(other)) => Err(MatcherError::new(
25 ctx.item.pos,
26 format!("Expected: null\nReceived: {other}"),
27 )),
28 Err(mut err) => {
29 err.message = format!("Expected null, thrown error instead:\n\n{}", err.message);
30 err.source = Some(std::sync::Arc::new(MatcherError));
31 Err(err)
32 }
33 }
34 }
35}