1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
Expose GraphQL scalars

The GraphQL language defines a number of built-in scalars: strings, numbers, and
booleans. This macro can be used either to define new types of scalars (e.g.
timestamps), or expose other types as one of the built-in scalars (e.g. bigints
as numbers or strings).

Since the preferred transport protocol for GraphQL responses is JSON, most
custom scalars will be transferred as strings. You therefore need to ensure that
the client library you are sending data to can parse the custom value into a
datatype appropriate for that platform.

By default the trait is implemented in terms of the default scalar value
representation provided by juniper. If that does not fit your needs it is
possible to use the same syntax as on `graphql_object!` to specify a custom
representation.

```rust
# #[macro_use] extern crate juniper;
# use juniper::{Value, FieldResult, ParseScalarValue, ParseScalarResult};
struct UserID(String);

graphql_scalar!(UserID {
    description: "An opaque identifier, represented as a string"

    resolve(&self) -> Value {
        Value::string(&self.0)
    }

    from_input_value(v: &InputValue) -> Option<UserID> {
        v.as_string_value().map(|s| UserID(s.to_owned()))
    }

    from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a> {
        <String as ParseScalarValue>::from_str(value)
    }
});

# fn main() { }
```

In addition to implementing `GraphQLType` for the type in question,
`FromInputValue` and `ToInputValue` is also implemented. This makes the type
usable as arguments and default values.

*/
#[macro_export]
macro_rules! graphql_scalar {
    ( @as_expr $e:expr) => { $e };
    (
        @generate,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {
            self_var = $resolve_self_var:ident,
            body = $resolve_body: block,
            return_type = $resolve_retun_type: ty,
        },
        from_input_value = {
            arg = $from_input_value_arg: ident,
            result = $from_input_value_result: ty,
            body = $from_input_value_body: block,
        },
        from_str = {
            value_arg = $from_str_arg: ident,
            result = $from_str_result: ty,
            body = $from_str_body: block,
            lifetime = $from_str_lt: tt,
        },

    ) => {
        $crate::__juniper_impl_trait!(
            impl <$($scalar)+> GraphQLType for $name {
                type Context = ();
                type TypeInfo = ();

                fn name(_: &Self::TypeInfo) -> Option<&str> {
                    Some($crate::graphql_scalar!(@as_expr $($outname)+))
                }

                fn meta<'r>(
                    info: &Self::TypeInfo,
                    registry: &mut $crate::Registry<'r, $crate::__juniper_insert_generic!($($scalar)+)>
                ) -> $crate::meta::MetaType<'r, $crate::__juniper_insert_generic!($($scalar)+)>
                where for<'__b> &'__b $crate::__juniper_insert_generic!($($scalar)+): $crate::ScalarRefValue<'__b>,
                    $crate::__juniper_insert_generic!($($scalar)+): 'r
                {
                    let meta = registry.build_scalar_type::<Self>(info);
                    $(
                        let meta = meta.description($descr);
                    )*
                    meta.into_meta()
                }

                fn resolve(
                    &$resolve_self_var,
                    _: &(),
                    _: Option<&[$crate::Selection<$crate::__juniper_insert_generic!($($scalar)+)>]>,
                    _: &$crate::Executor<
                        Self::Context,
                        $crate::__juniper_insert_generic!($($scalar)+)
                    >) -> $crate::Value<$crate::__juniper_insert_generic!($($scalar)+)> {
                    $resolve_body
                }
            });

        $crate::__juniper_impl_trait!(
            impl<$($scalar)+> ToInputValue for $name {
                fn to_input_value(&$resolve_self_var) -> $crate::InputValue<$crate::__juniper_insert_generic!($($scalar)+)> {
                    let v = $resolve_body;
                    $crate::ToInputValue::to_input_value(&v)
                }
            }
        );

        $crate::__juniper_impl_trait!(
            impl<$($scalar)+> FromInputValue for $name {
                fn from_input_value(
                    $from_input_value_arg: &$crate::InputValue<$crate::__juniper_insert_generic!($($scalar)+)>
                ) -> $from_input_value_result {
                    $from_input_value_body
                }
            }
        );

        $crate::__juniper_impl_trait!(
            impl<$($scalar)+> ParseScalarValue for $name {
                fn from_str<$from_str_lt>($from_str_arg: $crate::parser::ScalarToken<$from_str_lt>) -> $from_str_result {
                    $from_str_body
                }
            }
        );
    };

    // No more items to parse
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        from_input_value = {$($from_input_value_body:tt)+},
        from_str = {$($from_str_body:tt)+},
        rest =
    ) => {
        $crate::graphql_scalar!(
            @generate,
            meta = {
                name = $name,
                outname = {$($outname)+},
                scalar = {$($scalar)+},
                $(description = $descr,)*
            },
            resolve = {$($resolve_body)+},
            from_input_value = {$($from_input_value_body)+},
            from_str = {$($from_str_body)+},
        );
    };

    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        $(from_input_value = {$($from_input_value_body:tt)+})*,
        $(from_str = {$($from_str_body:tt)+})*,
        rest =
    ) => {
        compile_error!("Missing resolve function");
    };

    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        $(from_str = {$($from_str_body:tt)+})*,
        rest =
    ) => {
        compile_error!("Missing from_input_value function");
    };

    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        from_input_value = {$($from_input_value_body:tt)+},
        rest =
    ) =>{
        compile_error!("Missing from_str function");
    };


    // resolve(&self) -> Value { ... }
    (
        @parse_functions,
        meta = {$($meta:tt)*},
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = resolve(&$selfvar:ident) -> $return_ty:ty $body:block $($rest:tt)*
    ) => {
        $crate::graphql_scalar!(
            @parse_functions,
            meta = {$($meta)*},
            resolve = {
                self_var = $selfvar,
                body = $body,
                return_type = $return_ty,
            },
            $(from_input_value = {$($from_input_value_body)+},)*
            $(from_str = {$($from_str_body)+},)*
            rest = $($rest)*
        );
    };

    // from_input_value(arg: &InputValue) -> ... { ... }
    (
        @parse_functions,
        meta = { $($meta:tt)* },
        $(resolve = {$($resolve_body:tt)+})*,
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = from_input_value($arg:ident: &InputValue) -> $result:ty $body:block $($rest:tt)*
    ) => {
        $crate::graphql_scalar!(
            @parse_functions,
            meta = { $($meta)* },
            $(resolve = {$($resolve_body)+},)*
            from_input_value = {
                arg = $arg,
                result = $result,
                body = $body,
            },
            $(from_str = {$($from_str_body)+},)*
            rest = $($rest)*
        );
    };

    // from_str(value: &str) -> Result<S, ParseError>
    (
        @parse_functions,
        meta = { $($meta:tt)* },
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = from_str<$from_str_lt: tt>($value_arg:ident: ScalarToken<$ignored_lt2: tt>) -> $result:ty $body:block $($rest:tt)*
    ) => {
        $crate::graphql_scalar!(
            @parse_functions,
            meta = { $($meta)* },
            $(resolve = {$($resolve_body)+},)*
            $(from_input_value = {$($from_input_value_body)+},)*
            from_str = {
                value_arg = $value_arg,
                result = $result,
                body = $body,
                lifetime = $from_str_lt,
            },
            rest = $($rest)*
        );
    };

    // description: <description>
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
        },
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = description: $descr:tt $($rest:tt)*
    ) => {
        $crate::graphql_scalar!(
            @parse_functions,
            meta = {
                name = $name,
                outname = {$($outname)+},
                scalar = {$($scalar)+},
                description = $descr,
            },
            $(resolve = {$($resolve_body)+},)*
            $(from_input_value = {$($from_input_value_body)+},)*
            $(from_str = {$($from_str_body)+},)*
            rest = $($rest)*
        );
    };

    (
        @parse,
        meta = {
            lifetimes = [],
            name = $name: ty,
            outname = {$($outname:tt)*},
            scalar = {$($scalar:tt)*},
        },
        rest = $($rest:tt)*
    ) => {
         $crate::graphql_scalar!(
            @parse_functions,
            meta = {
                name = $name,
                outname = {$($outname)*},
                scalar = {$($scalar)*},
            },
            rest = $($rest)*
        );
    };

    (@$($stuff:tt)*) => {
        compile_error!("Invalid syntax for `graphql_scalar!`");
    };

    ($($rest:tt)*) => {
        $crate::__juniper_parse_object_header!(
            callback = graphql_scalar,
            rest = $($rest)*
        );
    }
}