async_graphql_test/matchers/
should_equal.rs1use std::fmt::{Display, Formatter};
2
3use async_graphql::async_trait::async_trait;
4use async_graphql::{Any, Context, CustomDirective, Directive, ResolveFut, ServerResult, Value};
5
6use super::MatcherError;
7
8#[Directive(location = "Field", name = "shouldEqual")]
9pub fn should_equal(to: Any) -> impl CustomDirective {
10 ShouldEqual { val: to.0 }
11}
12
13struct ShouldEqual {
14 val: Value,
15}
16
17impl Display for ShouldEqual {
18 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19 self.val.fmt(f)
20 }
21}
22
23#[async_trait]
24impl CustomDirective for ShouldEqual {
25 async fn resolve_field(
26 &self,
27 ctx: &Context<'_>,
28 resolve: ResolveFut<'_>,
29 ) -> ServerResult<Option<Value>> {
30 match resolve.await {
31 Ok(None) => Ok(None),
32 Ok(Some(value)) => {
33 if value == self.val {
34 Ok(Some(value))
35 } else {
36 Err(MatcherError::new(
37 ctx.item.pos,
38 format!("Expected: {self}\nReceived: {value}"),
39 ))
40 }
41 }
42 Err(err) => Err(MatcherError::unexpected_error(err)),
43 }
44 }
45}