iota_graphql_rpc/types/
string_input.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5/// Opt-in to an implementation of `ScalarType` for a `$Type` that implements
6/// `FromStr`, solely for use as an input (not an output). The type masquarades
7/// as a `String` in the GraphQL schema, to avoid adding a new scalar.
8macro_rules! impl_string_input {
9    ($Type:ident) => {
10        #[Scalar(name = "String", visible = false)]
11        impl ScalarType for $Type {
12            fn parse(value: Value) -> InputValueResult<Self> {
13                if let Value::String(s) = value {
14                    Ok(Self::from_str(&s)?)
15                } else {
16                    Err(InputValueError::expected_type(value))
17                }
18            }
19
20            fn to_value(&self) -> Value {
21                unimplemented!("String inputs should not be output");
22            }
23        }
24    };
25}
26
27pub(crate) use impl_string_input;