sui_graphql_rpc/types/
string_input.rs

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