Skip to content

Commit 4dc19fa

Browse files
authored
Add public API for resolver-based execution (#983)
1 parent b5748f7 commit 4dc19fa

File tree

16 files changed

+1443
-793
lines changed

16 files changed

+1443
-793
lines changed

crates/apollo-compiler/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717
## Maintenance
1818
## Documentation-->
1919

20+
# [x.x.x] (unreleased) - 2025-mm-dd
21+
22+
## Features
23+
24+
- **Add public API for resolver-based execution - [SimonSapin], [pull/983]**
25+
26+
[SimonSapin]: https://github.com/SimonSapin
27+
[pull/983]: https://github.com/apollographql/apollo-rs/pull/983
28+
29+
2030
# [1.28.0](https://crates.io/crates/apollo-compiler/1.28.0) - 2025-04-24
2131

2232
## Features

crates/apollo-compiler/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ autotests = false # Most tests/*.rs files are modules of tests/main.rs
2121
ahash = "0.8.11"
2222
apollo-parser = { path = "../apollo-parser", version = "0.8.0" }
2323
ariadne = { version = "0.5.1", features = ["auto-color"] }
24+
futures = "0.3"
2425
indexmap = "2.0.0"
2526
rowan = "0.16.0"
2627
serde = { version = "1.0", features = ["derive"] }
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use apollo_compiler::resolvers::AsyncObjectValue;
2+
use apollo_compiler::resolvers::AsyncResolvedValue;
3+
use apollo_compiler::resolvers::Execution;
4+
use apollo_compiler::resolvers::FieldError;
5+
use apollo_compiler::resolvers::ResolveInfo;
6+
use apollo_compiler::ExecutableDocument;
7+
use apollo_compiler::Schema;
8+
use futures::future::BoxFuture;
9+
10+
async fn async_resolvers_example() {
11+
let sdl = "
12+
type Query {
13+
field1: String
14+
field2: [Int]
15+
}
16+
";
17+
18+
struct Query;
19+
20+
impl AsyncObjectValue for Query {
21+
fn type_name(&self) -> &str {
22+
"Query"
23+
}
24+
25+
fn resolve_field<'a>(
26+
&'a self,
27+
info: &'a ResolveInfo<'a>,
28+
) -> BoxFuture<'a, Result<AsyncResolvedValue<'a>, FieldError>> {
29+
Box::pin(async move {
30+
match info.field_name() {
31+
"field1" => Ok(AsyncResolvedValue::leaf(self.resolve_field1().await)),
32+
"field2" => Ok(AsyncResolvedValue::list(self.resolve_field2().await)),
33+
_ => Err(self.unknown_field_error(info)),
34+
}
35+
})
36+
}
37+
}
38+
39+
impl Query {
40+
async fn resolve_field1(&self) -> String {
41+
// totally doing asynchronous I/O here
42+
"string".into()
43+
}
44+
45+
async fn resolve_field2(&self) -> [AsyncResolvedValue<'_>; 4] {
46+
// very await
47+
[7, 42, 0, 0].map(AsyncResolvedValue::leaf)
48+
}
49+
}
50+
51+
let query = "
52+
query($skp: Boolean!) {
53+
field1 @skip(if: $skp)
54+
field2
55+
}
56+
";
57+
let variables_values = &serde_json_bytes::json!({
58+
"skp": false,
59+
});
60+
61+
let schema = Schema::parse_and_validate(sdl, "schema.graphql").unwrap();
62+
let document = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();
63+
let response = Execution::new(&schema, &document)
64+
.raw_variable_values(variables_values.as_object().unwrap())
65+
.execute_async(&Query)
66+
.await
67+
.unwrap();
68+
let response = serde_json::to_string_pretty(&response).unwrap();
69+
expect_test::expect![[r#"
70+
{
71+
"data": {
72+
"field1": "string",
73+
"field2": [
74+
7,
75+
42,
76+
0,
77+
0
78+
]
79+
}
80+
}"#]]
81+
.assert_eq(&response);
82+
}
83+
84+
fn main() {
85+
futures::executor::block_on(async_resolvers_example())
86+
}

crates/apollo-compiler/src/execution/mod.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

crates/apollo-compiler/src/execution/resolver.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)