Skip to content

Commit 612a3af

Browse files
committed
Update syntax of interface instance resolvers
1 parent d59e6c2 commit 612a3af

File tree

8 files changed

+81
-185
lines changed

8 files changed

+81
-185
lines changed

src/executor_tests/introspection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ graphql_interface!(Interface: () as "SampleInterface" |&self| {
3636
Sample::One
3737
}
3838

39-
instance_resolvers: |&_| [
40-
Some(Root {}),
41-
]
39+
instance_resolvers: |&_| {
40+
Root => Some(Root {}),
41+
}
4242
});
4343

4444
graphql_object!(Root: () as "Root" |&self| {

src/macros/interface.rs

Lines changed: 51 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ with an array of expressions, each resolving into an `Option<T>` of the possible
2222
instances:
2323
2424
```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+
},
2929
```
3030
3131
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| {
7070
graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
7171
field id() -> &str { self.id() }
7272
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+
}
7777
});
7878
7979
# fn main() { }
@@ -89,8 +89,8 @@ macro_rules! graphql_interface {
8989

9090
// field deprecated <reason> <name>(...) -> <type> as <description> { ... }
9191
(
92-
@gather_meta,
93-
$reg:expr, $acc:expr, $descr:expr,
92+
@ gather_meta,
93+
($reg:expr, $acc:expr, $descr:expr),
9494
field deprecated $reason:tt $name:ident $args:tt -> $t:ty as $desc:tt $body:block $( $rest:tt )*
9595
) => {
9696
$acc.push(__graphql__args!(
@@ -102,13 +102,13 @@ macro_rules! graphql_interface {
102102
.deprecated($reason),
103103
$args));
104104

105-
graphql_interface!(@gather_meta, $reg, $acc, $descr, $( $rest )*);
105+
graphql_interface!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*);
106106
};
107107

108108
// field deprecated <reason> <name>(...) -> <type> { ... }
109109
(
110-
@gather_meta,
111-
$reg:expr, $acc:expr, $descr:expr,
110+
@ gather_meta,
111+
($reg:expr, $acc:expr, $descr:expr),
112112
field deprecated $reason:tt $name:ident $args:tt -> $t:ty $body:block $( $rest:tt )*
113113
) => {
114114
$acc.push(__graphql__args!(
@@ -119,13 +119,13 @@ macro_rules! graphql_interface {
119119
.deprecated($reason),
120120
$args));
121121

122-
graphql_interface!(@gather_meta, $reg, $acc, $descr, $( $rest )*);
122+
graphql_interface!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*);
123123
};
124124

125125
// field <name>(...) -> <type> as <description> { ... }
126126
(
127127
@gather_meta,
128-
$reg:expr, $acc:expr, $descr:expr,
128+
($reg:expr, $acc:expr, $descr:expr),
129129
field $name:ident $args:tt -> $t:ty as $desc:tt $body:block $( $rest:tt )*
130130
) => {
131131
$acc.push(__graphql__args!(
@@ -136,13 +136,13 @@ macro_rules! graphql_interface {
136136
.description($desc),
137137
$args));
138138

139-
graphql_interface!(@gather_meta, $reg, $acc, $descr, $( $rest )*);
139+
graphql_interface!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*);
140140
};
141141

142142
// field <name>(...) -> <type> { ... }
143143
(
144-
@gather_meta,
145-
$reg:expr, $acc:expr, $descr:expr,
144+
@ gather_meta,
145+
($reg:expr, $acc:expr, $descr:expr),
146146
field $name:ident $args:tt -> $t:ty $body:block $( $rest:tt )*
147147
) => {
148148
$acc.push(__graphql__args!(
@@ -152,176 +152,60 @@ macro_rules! graphql_interface {
152152
&$crate::to_snake_case(stringify!($name))),
153153
$args));
154154

155-
graphql_interface!(@gather_meta, $reg, $acc, $descr, $( $rest )*);
155+
graphql_interface!(@ gather_meta, ($reg, $acc, $descr), $( $rest )*);
156156
};
157157

158158
// description: <description>
159159
(
160-
@gather_meta,
161-
$reg:expr, $acc:expr, $descr:expr,
160+
@ gather_meta,
161+
($reg:expr, $acc:expr, $descr:expr),
162162
description : $value:tt $( $rest:tt )*
163163
) => {
164164
$descr = Some(graphql_interface!(@as_expr, $value));
165165

166-
graphql_interface!(@gather_meta, $reg, $acc, $descr, $( $rest )*)
166+
graphql_interface!(@gather_meta, ($reg, $acc, $descr), $( $rest )*)
167167
};
168168

169169
// instance_resolvers: | <ctxtvar> | [...]
170170
(
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,
276172
($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 )*
278174
) => {
279175
let $ctxtvar = &$ctxtarg;
280176

281-
fn inner_type_of<T>(_: T) -> String where T: $crate::GraphQLType<$ctxttype> {
282-
T::name().unwrap().to_owned()
283-
}
284-
285177
$(
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();
288180
}
289181
)*
290182

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);
296184
};
297185

298186
// instance_resolvers: | <ctxtvar> |
299187
(
300-
@resolve_into_type,
188+
@ resolve_into_type,
301189
($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 )*
303191
) => {
304192
let $ctxtvar = &$execarg.context();
305193

306-
fn inner_type_of<T>(_: T) -> String where T: $crate::GraphQLType<$ctxttype> {
307-
T::name().unwrap().to_owned()
308-
}
309-
310194
$(
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);
315197
}
316198
)*
317199

318-
return Ok($crate::Value::null());
200+
panic!("Concrete type not handled by instance resolvers on {}", $outname);
319201
};
320202

321-
( @resolve_into_type, $buildargs:tt, ) => {
322-
()
203+
( @ $mfn:ident, $args:tt, $first:tt $($rest:tt)* ) => {
204+
graphql_interface!(@ $mfn, $args, $($rest)*);
323205
};
324206

207+
( @ $mfn:ident, $buildargs:tt, ) => {};
208+
325209
(
326210
( $($lifetime:tt),* ) $name:ty : $ctxt:ty as $outname:tt | &$mainself:ident | {
327211
$( $items:tt )*
@@ -337,7 +221,7 @@ macro_rules! graphql_interface {
337221
fn meta(registry: &mut $crate::Registry<$ctxt>) -> $crate::meta::MetaType {
338222
let mut fields = Vec::new();
339223
let mut description = None;
340-
graphql_interface!(@gather_meta, registry, fields, description, $($items)*);
224+
graphql_interface!(@ gather_meta, (registry, fields, description), $($items)*);
341225
let mut mt = registry.build_interface_type::<$name>()(&fields);
342226

343227
if let Some(description) = description {
@@ -350,19 +234,29 @@ macro_rules! graphql_interface {
350234
#[allow(unused_variables)]
351235
#[allow(unused_mut)]
352236
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)*);
354241
}
355242

356243
fn concrete_type_name(&$mainself, context: &$ctxt) -> String {
357244
graphql_interface!(
358-
@concrete_type_name,
245+
@ concrete_type_name,
359246
($outname, context, $ctxt),
360247
$($items)*);
361248
}
362249

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+
{
364258
graphql_interface!(
365-
@resolve_into_type,
259+
@ resolve_into_type,
366260
($outname, type_name, executor, $ctxt),
367261
$($items)*);
368262
}

src/macros/object.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ struct Implementor { id: String }
100100
graphql_interface!(<'a> &'a Interface: () as "Interface" |&self| {
101101
field id() -> &str { self.id() }
102102
103-
instance_resolvers: |&context| [
104-
self.as_implementor(),
105-
]
103+
instance_resolvers: |&context| {
104+
Implementor => self.as_implementor(),
105+
}
106106
});
107107
108108
graphql_object!(Implementor: () as "Implementor" |&self| {

src/macros/tests/field.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ graphql_interface!(Interface: () as "Interface" |&self| {
4242
field deprecated "Deprecation reason"
4343
deprecated_descr() -> i64 as "Field description" { 0 }
4444

45-
instance_resolvers: |&_| [
46-
Some(Root {}),
47-
]
45+
instance_resolvers: |&_| {
46+
Root => Some(Root {}),
47+
}
4848
});
4949

5050
fn run_field_info_query<F>(type_name: &str, field_name: &str, f: F)

0 commit comments

Comments
 (0)