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
/**
Expose GraphQL unions

Like interfaces, mapping unions can be tricky in idiomatic Rust. Because of
their similarity, the helper macros are similar, too: you provide a set of
expressions that resolve the union into the actual concrete type.

## Syntax

See the documentation for [`graphql_object!`][1] on the general item and type
syntax. `graphql_union!` supports only `description` and `interface_resolvers`
items, no fields or interfaces can be declared.

See the documentation for [`graphql_interface!`][2] on the syntax for interface
resolvers.

[1]: macro.graphql_object!.html
[2]: macro.graphql_interface!.html
*/
#[macro_export]
macro_rules! graphql_union {
    ( @as_item, $i:item) => { $i };
    ( @as_expr, $e:expr) => { $e };
    ( @as_path, $p:path) => { $p };
    ( @as_type, $t:ty) => { $t };

    // description: <description>
    (
        @ gather_meta,
        ($reg:expr, $acc:expr, $descr:expr),
        description : $value:tt $( $rest:tt )*
    ) => {
        $descr = Some(graphql_interface!(@as_expr, $value));

        graphql_union!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*)
    };

    // Gathering meta for instance resolvers
    // instance_resolvers: | <ctxtvar> | [...]
    (
        @ gather_meta,
        ($reg:expr, $acc:expr, $descr:expr),
        instance_resolvers: | $ctxtvar:pat | { $( $srctype:ty => $resolver:expr ),* $(,)* } $( $rest:tt )*
    ) => {
        $acc = vec![
            $(
                $reg.get_type::<$srctype>()
            ),*
        ];

        graphql_union!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*)
    };

    // To generate the "concrete type name" resolver, syntax case:
    // instance_resolvers: | <ctxtvar> | [...]
    (
        @ concrete_type_name,
        ($outname:tt, $ctxtarg:ident, $ctxttype:ty),
        instance_resolvers: | $ctxtvar:pat | { $( $srctype:ty => $resolver:expr ),* $(,)* } $( $rest:tt )*
    ) => {
        let $ctxtvar = &$ctxtarg;

        $(
            if let Some(_) = $resolver as Option<$srctype> {
                return (<$srctype as $crate::GraphQLType<$ctxttype>>::name()).unwrap().to_owned();
            }
        )*

            panic!("Concrete type not handled by instance resolvers on {}", $outname);
    };

    // To generate the "resolve into type" resolver, syntax case:
    // instance_resolvers: | <ctxtvar> | [...]
    (
        @ resolve_into_type,
        ($outname:tt, $typenamearg:ident, $execarg:ident, $ctxttype:ty),
        instance_resolvers: | $ctxtvar:pat | { $( $srctype:ty => $resolver:expr ),* $(,)* } $( $rest:tt )*
    ) => {
        let $ctxtvar = &$execarg.context();

        $(
            if $typenamearg == (<$srctype as $crate::GraphQLType<$ctxttype>>::name()).unwrap().to_owned() {
                return $execarg.resolve(&$resolver);
            }
        )*

            panic!("Concrete type not handled by instance resolvers on {}", $outname);
    };

    // eat commas
    ( @ $mfn:ident, $args:tt, , $($rest:tt)* ) => {
        graphql_union!(@ $mfn, $args, $($rest)*);
    };

    // eat one tt
    ( @ $mfn:ident, $args:tt, $item:tt $($rest:tt)* ) => {
        graphql_union!(@ $mfn, $args, $($rest)*);
    };

    // end case
    ( @ $mfn:ident, $args:tt, ) => {};

    (
        ( $($lifetime:tt),* ) $name:ty : $ctxt:ty as $outname:tt | &$mainself:ident | {
            $( $items:tt )*
        }
    ) => {
        graphql_union!(@as_item, impl<$($lifetime)*> $crate::GraphQLType<$ctxt> for $name {
            fn name() -> Option<&'static str> {
                Some($outname)
            }

            #[allow(unused_assignments)]
            #[allow(unused_mut)]
            fn meta(registry: &mut $crate::Registry<$ctxt>) -> $crate::meta::MetaType {
                let mut types;
                let mut description = None;
                graphql_union!(@ gather_meta, (registry, types, description), $($items)*);
                let mut mt = registry.build_union_type::<$name>()(&types);

                if let Some(description) = description {
                    mt = mt.description(description);
                }

                mt.into_meta()
            }

            fn concrete_type_name(&$mainself, context: &$ctxt) -> String {
                graphql_union!(
                    @ concrete_type_name,
                    ($outname, context, $ctxt),
                    $($items)*);
            }

            fn resolve_into_type(
                &$mainself,
                type_name: &str,
                _: Option<Vec<$crate::Selection>>,
                executor: &mut $crate::Executor<$ctxt>,
            )
                -> $crate::ExecutionResult
            {
                graphql_union!(
                    @ resolve_into_type,
                    ($outname, type_name, executor, $ctxt),
                    $($items)*);
            }
        });

        impl<$($lifetime)*> $crate::IntoFieldResult<$name> for $name {
            fn into(self) -> $crate::FieldResult<$name> {
                Ok(self)
            }
        }
    };

    (
        <$($lifetime:tt),*> $name:ty : $ctxt:ty as $outname:tt | &$mainself:ident | {
            $( $items:tt )*
        }
    ) => {
        graphql_union!(
            ($($lifetime),*) $name : $ctxt as $outname | &$mainself | { $( $items )* });
    };

    (
        $name:ty : $ctxt:ty as $outname:tt | &$mainself:ident | {
            $( $items:tt )*
        }
    ) => {
        graphql_union!(() $name : $ctxt as $outname | &$mainself | { $( $items )* });
    };

    (
        $name:ty : $ctxt:ty | &$mainself:ident | {
            $( $items:tt )*
        }
    ) => {
        graphql_union!(() $name : $ctxt as (stringify!($name)) | &$mainself | { $( $items )* });
    };
}