@@ -12,6 +12,7 @@ struct TransparentAttributes {
12
12
transparent : Option < bool > ,
13
13
name : Option < String > ,
14
14
description : Option < String > ,
15
+ scalar : Option < syn:: Type > ,
15
16
}
16
17
17
18
impl syn:: parse:: Parse for TransparentAttributes {
@@ -20,6 +21,7 @@ impl syn::parse::Parse for TransparentAttributes {
20
21
transparent : None ,
21
22
name : None ,
22
23
description : None ,
24
+ scalar : None ,
23
25
} ;
24
26
25
27
while !input. is_empty ( ) {
@@ -38,6 +40,11 @@ impl syn::parse::Parse for TransparentAttributes {
38
40
"transparent" => {
39
41
output. transparent = Some ( true ) ;
40
42
}
43
+ "scalar" | "Scalar" => {
44
+ input. parse :: < token:: Eq > ( ) ?;
45
+ let val = input. parse :: < syn:: Type > ( ) ?;
46
+ output. scalar = Some ( val) ;
47
+ }
41
48
_ => return Err ( syn:: Error :: new ( ident. span ( ) , "unknown attribute" ) ) ,
42
49
}
43
50
input. try_parse :: < token:: Comma > ( ) ?;
@@ -99,22 +106,34 @@ fn impl_scalar_struct(
99
106
None => quote ! ( ) ,
100
107
} ;
101
108
109
+ let scalar = attrs
110
+ . scalar
111
+ . as_ref ( )
112
+ . map ( |s| quote ! ( #s ) )
113
+ . unwrap_or_else ( || quote ! ( __S) ) ;
114
+
115
+ let impl_generics = attrs
116
+ . scalar
117
+ . as_ref ( )
118
+ . map ( |_| quote ! ( ) )
119
+ . unwrap_or_else ( || quote ! ( <__S>) ) ;
120
+
102
121
let _async = quote ! (
103
- impl <__S> :: juniper:: GraphQLValueAsync <__S > for #ident
122
+ impl #impl_generics :: juniper:: GraphQLValueAsync <#scalar > for #ident
104
123
where
105
124
Self : Sync ,
106
125
Self :: TypeInfo : Sync ,
107
126
Self :: Context : Sync ,
108
- __S : :: juniper:: ScalarValue + Send + Sync ,
127
+ #scalar : :: juniper:: ScalarValue + Send + Sync ,
109
128
{
110
129
fn resolve_async<' a>(
111
130
& ' a self ,
112
131
info: & ' a Self :: TypeInfo ,
113
- selection_set: Option <& ' a [ :: juniper:: Selection <__S >] >,
114
- executor: & ' a :: juniper:: Executor <Self :: Context , __S >,
115
- ) -> :: juniper:: BoxFuture <' a, :: juniper:: ExecutionResult <__S >> {
132
+ selection_set: Option <& ' a [ :: juniper:: Selection <#scalar >] >,
133
+ executor: & ' a :: juniper:: Executor <Self :: Context , #scalar >,
134
+ ) -> :: juniper:: BoxFuture <' a, :: juniper:: ExecutionResult <#scalar >> {
116
135
use :: juniper:: futures:: future;
117
- let v = :: juniper:: GraphQLValue :: resolve( self , info, selection_set, executor) ;
136
+ let v = :: juniper:: GraphQLValue :: <#scalar> :: resolve( self , info, selection_set, executor) ;
118
137
Box :: pin( future:: ready( v) )
119
138
}
120
139
}
@@ -123,80 +142,84 @@ fn impl_scalar_struct(
123
142
let content = quote ! (
124
143
#_async
125
144
126
- impl < S > :: juniper:: GraphQLType <S > for #ident
145
+ impl #impl_generics :: juniper:: GraphQLType <#scalar > for #ident
127
146
where
128
- S : :: juniper:: ScalarValue ,
147
+ #scalar : :: juniper:: ScalarValue ,
129
148
{
130
149
fn name( _: & Self :: TypeInfo ) -> Option <& ' static str > {
131
150
Some ( #name)
132
151
}
133
152
134
153
fn meta<' r>(
135
154
info: & Self :: TypeInfo ,
136
- registry: & mut :: juniper:: Registry <' r, S >,
137
- ) -> :: juniper:: meta:: MetaType <' r, S >
155
+ registry: & mut :: juniper:: Registry <' r, #scalar >,
156
+ ) -> :: juniper:: meta:: MetaType <' r, #scalar >
138
157
where
139
- S : ' r,
158
+ #scalar : ' r,
140
159
{
141
160
registry. build_scalar_type:: <Self >( info)
142
161
#description
143
162
. into_meta( )
144
163
}
145
164
}
146
165
147
- impl < S > :: juniper:: GraphQLValue <S > for #ident
166
+ impl #impl_generics :: juniper:: GraphQLValue <#scalar > for #ident
148
167
where
149
- S : :: juniper:: ScalarValue ,
168
+ #scalar : :: juniper:: ScalarValue ,
150
169
{
151
170
type Context = ( ) ;
152
171
type TypeInfo = ( ) ;
153
172
154
173
fn type_name<' __i>( & self , info: & ' __i Self :: TypeInfo ) -> Option <& ' __i str > {
155
- <Self as :: juniper:: GraphQLType <S >>:: name( info)
174
+ <Self as :: juniper:: GraphQLType <#scalar >>:: name( info)
156
175
}
157
176
158
177
fn resolve(
159
178
& self ,
160
179
info: & ( ) ,
161
- selection: Option <& [ :: juniper:: Selection <S >] >,
162
- executor: & :: juniper:: Executor <Self :: Context , S >,
163
- ) -> :: juniper:: ExecutionResult <S > {
164
- :: juniper:: GraphQLValue :: resolve( & self . 0 , info, selection, executor)
180
+ selection: Option <& [ :: juniper:: Selection <#scalar >] >,
181
+ executor: & :: juniper:: Executor <Self :: Context , #scalar >,
182
+ ) -> :: juniper:: ExecutionResult <#scalar > {
183
+ :: juniper:: GraphQLValue :: <#scalar> :: resolve( & self . 0 , info, selection, executor)
165
184
}
166
185
}
167
186
168
- impl < S > :: juniper:: ToInputValue <S > for #ident
187
+ impl #impl_generics :: juniper:: ToInputValue <#scalar > for #ident
169
188
where
170
- S : :: juniper:: ScalarValue ,
189
+ #scalar : :: juniper:: ScalarValue ,
171
190
{
172
- fn to_input_value( & self ) -> :: juniper:: InputValue <S > {
173
- :: juniper:: ToInputValue :: to_input_value( & self . 0 )
191
+ fn to_input_value( & self ) -> :: juniper:: InputValue <#scalar > {
192
+ :: juniper:: ToInputValue :: <#scalar> :: to_input_value( & self . 0 )
174
193
}
175
194
}
176
195
177
- impl < S > :: juniper:: FromInputValue <S > for #ident
196
+ impl #impl_generics :: juniper:: FromInputValue <#scalar > for #ident
178
197
where
179
- S : :: juniper:: ScalarValue ,
198
+ #scalar : :: juniper:: ScalarValue ,
180
199
{
181
- fn from_input_value( v: & :: juniper:: InputValue <S >) -> Option <#ident> {
182
- let inner: #inner_ty = :: juniper:: FromInputValue :: from_input_value( v) ?;
200
+ fn from_input_value( v: & :: juniper:: InputValue <#scalar >) -> Option <#ident> {
201
+ let inner: #inner_ty = :: juniper:: FromInputValue :: <#scalar> :: from_input_value( v) ?;
183
202
Some ( #ident( inner) )
184
203
}
185
204
}
186
205
187
- impl < S > :: juniper:: ParseScalarValue <S > for #ident
206
+ impl #impl_generics :: juniper:: ParseScalarValue <#scalar > for #ident
188
207
where
189
- S : :: juniper:: ScalarValue ,
208
+ #scalar : :: juniper:: ScalarValue ,
190
209
{
191
210
fn from_str<' a>(
192
211
value: :: juniper:: parser:: ScalarToken <' a>,
193
- ) -> :: juniper:: ParseScalarResult <' a, S > {
194
- <#inner_ty as :: juniper:: ParseScalarValue <S >>:: from_str( value)
212
+ ) -> :: juniper:: ParseScalarResult <' a, #scalar > {
213
+ <#inner_ty as :: juniper:: ParseScalarValue <#scalar >>:: from_str( value)
195
214
}
196
215
}
197
216
198
- impl <S : :: juniper:: ScalarValue > :: juniper:: marker:: IsOutputType <S > for #ident { }
199
- impl <S : :: juniper:: ScalarValue > :: juniper:: marker:: IsInputType <S > for #ident { }
217
+ impl #impl_generics :: juniper:: marker:: IsOutputType <#scalar> for #ident
218
+ where #scalar: :: juniper:: ScalarValue ,
219
+ { }
220
+ impl #impl_generics :: juniper:: marker:: IsInputType <#scalar> for #ident
221
+ where #scalar: :: juniper:: ScalarValue ,
222
+ { }
200
223
) ;
201
224
202
225
Ok ( content)
0 commit comments