@@ -22,10 +22,10 @@ with an array of expressions, each resolving into an `Option<T>` of the possible
22
22
instances:
23
23
24
24
```rust,ignore
25
- instance_resolvers: |&context| [
26
- context.get_human(self.id()), // returns Option<Human>
27
- context.get_droid(self.id()), // returns Option<Droid>
28
- ] ,
25
+ instance_resolvers: |&context| {
26
+ Human => context.get_human(self.id()), // returns Option<Human>
27
+ Droid => context.get_droid(self.id()), // returns Option<Droid>
28
+ } ,
29
29
```
30
30
31
31
Each item in the array will be executed in order when the concrete type is
@@ -70,10 +70,10 @@ graphql_object!(Droid: Database as "Droid" |&self| {
70
70
graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
71
71
field id() -> &str { self.id() }
72
72
73
- instance_resolvers: |&context| [
74
- context.humans.get(self.id()),
75
- context.droids.get(self.id()),
76
- ]
73
+ instance_resolvers: |&context| {
74
+ Human => context.humans.get(self.id()),
75
+ Droid => context.droids.get(self.id()),
76
+ }
77
77
});
78
78
79
79
# fn main() { }
@@ -89,8 +89,8 @@ macro_rules! graphql_interface {
89
89
90
90
// field deprecated <reason> <name>(...) -> <type> as <description> { ... }
91
91
(
92
- @gather_meta,
93
- $reg: expr, $acc: expr, $descr: expr,
92
+ @ gather_meta,
93
+ ( $reg: expr, $acc: expr, $descr: expr) ,
94
94
field deprecated $reason: tt $name: ident $args: tt -> $t: ty as $desc: tt $body: block $( $rest: tt ) *
95
95
) => {
96
96
$acc. push( __graphql__args!(
@@ -102,13 +102,13 @@ macro_rules! graphql_interface {
102
102
. deprecated( $reason) ,
103
103
$args) ) ;
104
104
105
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * ) ;
105
+ graphql_interface!( @ gather_meta, ( $reg, $acc, $descr) , $( $rest ) * ) ;
106
106
} ;
107
107
108
108
// field deprecated <reason> <name>(...) -> <type> { ... }
109
109
(
110
- @gather_meta,
111
- $reg: expr, $acc: expr, $descr: expr,
110
+ @ gather_meta,
111
+ ( $reg: expr, $acc: expr, $descr: expr) ,
112
112
field deprecated $reason: tt $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
113
113
) => {
114
114
$acc. push( __graphql__args!(
@@ -119,13 +119,13 @@ macro_rules! graphql_interface {
119
119
. deprecated( $reason) ,
120
120
$args) ) ;
121
121
122
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * ) ;
122
+ graphql_interface!( @ gather_meta, ( $reg, $acc, $descr) , $( $rest ) * ) ;
123
123
} ;
124
124
125
125
// field <name>(...) -> <type> as <description> { ... }
126
126
(
127
127
@gather_meta,
128
- $reg: expr, $acc: expr, $descr: expr,
128
+ ( $reg: expr, $acc: expr, $descr: expr) ,
129
129
field $name: ident $args: tt -> $t: ty as $desc: tt $body: block $( $rest: tt ) *
130
130
) => {
131
131
$acc. push( __graphql__args!(
@@ -136,13 +136,13 @@ macro_rules! graphql_interface {
136
136
. description( $desc) ,
137
137
$args) ) ;
138
138
139
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * ) ;
139
+ graphql_interface!( @ gather_meta, ( $reg, $acc, $descr) , $( $rest ) * ) ;
140
140
} ;
141
141
142
142
// field <name>(...) -> <type> { ... }
143
143
(
144
- @gather_meta,
145
- $reg: expr, $acc: expr, $descr: expr,
144
+ @ gather_meta,
145
+ ( $reg: expr, $acc: expr, $descr: expr) ,
146
146
field $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
147
147
) => {
148
148
$acc. push( __graphql__args!(
@@ -152,176 +152,60 @@ macro_rules! graphql_interface {
152
152
& $crate:: to_snake_case( stringify!( $name) ) ) ,
153
153
$args) ) ;
154
154
155
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * ) ;
155
+ graphql_interface!( @ gather_meta, ( $reg, $acc, $descr) , $( $rest ) * ) ;
156
156
} ;
157
157
158
158
// description: <description>
159
159
(
160
- @gather_meta,
161
- $reg: expr, $acc: expr, $descr: expr,
160
+ @ gather_meta,
161
+ ( $reg: expr, $acc: expr, $descr: expr) ,
162
162
description : $value: tt $( $rest: tt ) *
163
163
) => {
164
164
$descr = Some ( graphql_interface!( @as_expr, $value) ) ;
165
165
166
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * )
166
+ graphql_interface!( @gather_meta, ( $reg, $acc, $descr) , $( $rest ) * )
167
167
} ;
168
168
169
169
// instance_resolvers: | <ctxtvar> | [...]
170
170
(
171
- @gather_meta,
172
- $reg: expr, $acc: expr, $descr: expr,
173
- instance_resolvers: | $ctxtvar: pat | $resolvers: tt $( $rest: tt ) *
174
- ) => {
175
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * )
176
- } ;
177
-
178
- ( @gather_meta, $reg: expr, $acc: expr, $descr: expr, , $( $rest: tt ) * ) => {
179
- graphql_interface!( @gather_meta, $reg, $acc, $descr, $( $rest ) * )
180
- } ;
181
-
182
- ( @gather_meta, $reg: expr, $acc: expr, $descr: expr, ) => {
183
- } ;
184
-
185
- // field deprecated <reason> <name>(...) -> <type> as <description> { ... }
186
- (
187
- @resolve_into_type,
188
- $buildargs: tt,
189
- field deprecated $reason: tt $name: ident $args: tt -> $t: ty as $descr: tt $body: block $( $rest: tt ) *
190
- ) => {
191
- graphql_interface!( @resolve_into_type, $buildargs, $( $rest ) * )
192
- } ;
193
-
194
- // field deprecated <reason> <name>(...) -> <type> { ... }
195
- (
196
- @resolve_into_type,
197
- $buildargs: tt,
198
- field deprecated $reason: tt $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
199
- ) => {
200
- graphql_interface!( @resolve_into_type, $buildargs, $( $rest ) * )
201
- } ;
202
-
203
- // field <name>(...) -> <type> as <description> { ... }
204
- (
205
- @resolve_into_type,
206
- $buildargs: tt,
207
- field $name: ident $args: tt -> $t: ty as $descr: tt $body: block $( $rest: tt ) *
208
- ) => {
209
- graphql_interface!( @resolve_into_type, $buildargs, $( $rest ) * )
210
- } ;
211
-
212
- // field <name>(...) -> <type> { ... }
213
- (
214
- @resolve_into_type,
215
- $buildargs: tt,
216
- field $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
217
- ) => {
218
- graphql_interface!( @resolve_into_type, $buildargs, $( $rest ) * )
219
- } ;
220
-
221
- // description: <description>
222
- (
223
- @resolve_into_type,
224
- $buildargs: tt, description : $value: tt $( $rest: tt ) *
225
- ) => {
226
- graphql_interface!( @resolve_into_type, $buildargs, $( $rest ) * )
227
- } ;
228
-
229
- // field deprecated <reason> <name>(...) -> <type> as <description> { ... }
230
- (
231
- @concrete_type_name,
232
- $buildargs: tt,
233
- field deprecated $reason: tt $name: ident $args: tt -> $t: ty as $descr: tt $body: block $( $rest: tt ) *
234
- ) => {
235
- graphql_interface!( @concrete_type_name, $buildargs, $( $rest ) * )
236
- } ;
237
-
238
- // field deprecated <reason> <name>(...) -> <type> { ... }
239
- (
240
- @concrete_type_name,
241
- $buildargs: tt,
242
- field deprecated $reason: tt $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
243
- ) => {
244
- graphql_interface!( @concrete_type_name, $buildargs, $( $rest ) * )
245
- } ;
246
-
247
- // field <name>(...) -> <type> as <description> { ... }
248
- (
249
- @concrete_type_name,
250
- $buildargs: tt,
251
- field $name: ident $args: tt -> $t: ty as $descr: tt $body: block $( $rest: tt ) *
252
- ) => {
253
- graphql_interface!( @concrete_type_name, $buildargs, $( $rest ) * )
254
- } ;
255
-
256
- // field <name>(...) -> <type> { ... }
257
- (
258
- @concrete_type_name,
259
- $buildargs: tt,
260
- field $name: ident $args: tt -> $t: ty $body: block $( $rest: tt ) *
261
- ) => {
262
- graphql_interface!( @concrete_type_name, $buildargs, $( $rest ) * )
263
- } ;
264
-
265
- // description: <description>
266
- (
267
- @concrete_type_name,
268
- $buildargs: tt, description : $value: tt $( $rest: tt ) *
269
- ) => {
270
- graphql_interface!( @concrete_type_name, $buildargs, $( $rest ) * )
271
- } ;
272
-
273
- // instance_resolvers: | <ctxtvar> | [...]
274
- (
275
- @concrete_type_name,
171
+ @ concrete_type_name,
276
172
( $outname: tt, $ctxtarg: ident, $ctxttype: ty) ,
277
- instance_resolvers : | $ctxtvar: pat | [ $( $resolver: expr ) ,* $( , ) * ] $( $rest: tt ) *
173
+ instance_resolvers : | $ctxtvar: pat | { $( $srctype : ty => $ resolver: expr ) ,* $( , ) * } $( $rest: tt ) *
278
174
) => {
279
175
let $ctxtvar = & $ctxtarg;
280
176
281
- fn inner_type_of<T >( _: T ) -> String where T : $crate:: GraphQLType <$ctxttype> {
282
- T :: name( ) . unwrap( ) . to_owned( )
283
- }
284
-
285
177
$(
286
- if let Some ( ref v ) = $resolver {
287
- return inner_type_of ( v ) ;
178
+ if let Some ( _ ) = $resolver {
179
+ return ( <$srctype as $crate :: GraphQLType <$ctxttype>> :: name ( ) ) . unwrap ( ) . to_owned ( ) ;
288
180
}
289
181
) *
290
182
291
- panic!( "Concrete type not handled by instance resolvers on {}" , $outname) ;
292
- } ;
293
-
294
- ( @concrete_type_name, $buildargs: tt, ) => {
295
- ( )
183
+ panic!( "Concrete type not handled by instance resolvers on {}" , $outname) ;
296
184
} ;
297
185
298
186
// instance_resolvers: | <ctxtvar> |
299
187
(
300
- @resolve_into_type,
188
+ @ resolve_into_type,
301
189
( $outname: tt, $typenamearg: ident, $execarg: ident, $ctxttype: ty) ,
302
- instance_resolvers : | $ctxtvar: pat | [ $( $resolver: expr ) ,* $( , ) * ] $( $rest: tt ) *
190
+ instance_resolvers : | $ctxtvar: pat | { $( $srctype : ty => $ resolver: expr ) ,* $( , ) * } $( $rest: tt ) *
303
191
) => {
304
192
let $ctxtvar = & $execarg. context( ) ;
305
193
306
- fn inner_type_of<T >( _: T ) -> String where T : $crate:: GraphQLType <$ctxttype> {
307
- T :: name( ) . unwrap( ) . to_owned( )
308
- }
309
-
310
194
$(
311
- if let Some ( ref v) = $resolver {
312
- if inner_type_of( v) == $typenamearg {
313
- return $execarg. resolve( v) ;
314
- }
195
+ if $typenamearg == ( <$srctype as $crate:: GraphQLType <$ctxttype>>:: name( ) ) . unwrap( ) . to_owned( ) {
196
+ return $execarg. resolve( & $resolver) ;
315
197
}
316
198
) *
317
199
318
- return Ok ( $crate :: Value :: null ( ) ) ;
200
+ panic! ( "Concrete type not handled by instance resolvers on {}" , $outname ) ;
319
201
} ;
320
202
321
- ( @resolve_into_type , $buildargs : tt, ) => {
322
- ( )
203
+ ( @ $mfn : ident , $args : tt, $first : tt $ ( $rest : tt ) * ) => {
204
+ graphql_interface! ( @ $mfn , $args , $ ( $rest ) * ) ;
323
205
} ;
324
206
207
+ ( @ $mfn: ident, $buildargs: tt, ) => { } ;
208
+
325
209
(
326
210
( $( $lifetime: tt) ,* ) $name: ty : $ctxt: ty as $outname: tt | & $mainself: ident | {
327
211
$( $items: tt ) *
@@ -337,7 +221,7 @@ macro_rules! graphql_interface {
337
221
fn meta( registry: & mut $crate:: Registry <$ctxt>) -> $crate:: meta:: MetaType {
338
222
let mut fields = Vec :: new( ) ;
339
223
let mut description = None ;
340
- graphql_interface!( @gather_meta, registry, fields, description, $( $items) * ) ;
224
+ graphql_interface!( @ gather_meta, ( registry, fields, description) , $( $items) * ) ;
341
225
let mut mt = registry. build_interface_type:: <$name>( ) ( & fields) ;
342
226
343
227
if let Some ( description) = description {
@@ -350,19 +234,29 @@ macro_rules! graphql_interface {
350
234
#[ allow( unused_variables) ]
351
235
#[ allow( unused_mut) ]
352
236
fn resolve_field( & $mainself, field: & str , args: & $crate:: Arguments , mut executor: & mut $crate:: Executor <$ctxt>) -> $crate:: ExecutionResult {
353
- __graphql__build_field_matches!( ( $outname, $mainself, field, args, executor) , ( ) , $( $items) * ) ;
237
+ __graphql__build_field_matches!(
238
+ ( $outname, $mainself, field, args, executor) ,
239
+ ( ) ,
240
+ $( $items) * ) ;
354
241
}
355
242
356
243
fn concrete_type_name( & $mainself, context: & $ctxt) -> String {
357
244
graphql_interface!(
358
- @concrete_type_name,
245
+ @ concrete_type_name,
359
246
( $outname, context, $ctxt) ,
360
247
$( $items) * ) ;
361
248
}
362
249
363
- fn resolve_into_type( & $mainself, type_name: & str , _: Option <Vec <$crate:: Selection >>, executor: & mut $crate:: Executor <$ctxt>) -> $crate:: ExecutionResult {
250
+ fn resolve_into_type(
251
+ & $mainself,
252
+ type_name: & str ,
253
+ _: Option <Vec <$crate:: Selection >>,
254
+ executor: & mut $crate:: Executor <$ctxt>,
255
+ )
256
+ -> $crate:: ExecutionResult
257
+ {
364
258
graphql_interface!(
365
- @resolve_into_type,
259
+ @ resolve_into_type,
366
260
( $outname, type_name, executor, $ctxt) ,
367
261
$( $items) * ) ;
368
262
}
0 commit comments