Skip to content

Commit d3e8d05

Browse files
committed
runtime agnostic register
1 parent fe3d94c commit d3e8d05

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ uuid = { version = "1.7", features = ["v4"] } # A library to ge
3939
# Non-feature optional dependencies
4040
libflate = { version = "2", optional = true }
4141
tokio = { version = "1", features = ["full"], optional = true }
42-
async-std = { version = "1", features = ["default"], optional = true }
42+
async-std = { version = "1", features = ["default", "tokio1"], optional = true }
4343

4444
[build-dependencies]
4545
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }

src/register.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn sha_dynamic(schema: &dynamic::Schema) -> String {
4949
/// * `user_version` - An arbitrary string you can set to distinguish data sent by different versions of your edge server. For example, this can be the SHA of the Git commit for your deployed server code. We plan to make this value visible in Apollo Studio.
5050
/// * `platform` - The infrastructure environment that your edge server is running in (localhost, kubernetes/deployment, aws lambda, google cloud run, google cloud function, AWS ECS, etc.)
5151
#[instrument(err, skip(authorization_token, schema))]
52-
pub async fn register<
52+
pub fn register<
5353
Q: ObjectType + 'static,
5454
M: ObjectType + 'static,
5555
S: SubscriptionType + 'static,
@@ -120,8 +120,9 @@ pub async fn register<
120120
}))
121121
.header("content-type", "application/json")
122122
.header("X-Api-Key", authorization_token)
123-
.send()
124-
.await;
123+
.send();
124+
125+
let result = eval_future(result);
125126

126127
match result {
127128
Ok(data) => {
@@ -130,7 +131,7 @@ pub async fn register<
130131
message = "Schema correctly registered",
131132
response = &tracing::field::debug(&data)
132133
);
133-
let text = data.text().await;
134+
let text = eval_future(data.text());
134135
debug!(target: TARGET_LOG, data = ?text);
135136
Ok(())
136137
}
@@ -151,7 +152,7 @@ pub async fn register<
151152
/// * `user_version` - An arbitrary string you can set to distinguish data sent by different versions of your edge server. For example, this can be the SHA of the Git commit for your deployed server code. We plan to make this value visible in Apollo Studio.
152153
/// * `platform` - The infrastructure environment that your edge server is running in (localhost, kubernetes/deployment, aws lambda, google cloud run, google cloud function, AWS ECS, etc.)
153154
#[instrument(err, skip(authorization_token, schema))]
154-
pub async fn register_dynamic(
155+
pub fn register_dynamic(
155156
authorization_token: &str,
156157
schema: &dynamic::Schema,
157158
server_id: &str,
@@ -218,8 +219,9 @@ pub async fn register_dynamic(
218219
}))
219220
.header("content-type", "application/json")
220221
.header("X-Api-Key", authorization_token)
221-
.send()
222-
.await;
222+
.send();
223+
224+
let result = eval_future(result);
223225

224226
match result {
225227
Ok(data) => {
@@ -228,7 +230,7 @@ pub async fn register_dynamic(
228230
message = "Schema correctly registered",
229231
response = &tracing::field::debug(&data)
230232
);
231-
let text = data.text().await;
233+
let text = eval_future(data.text());
232234
debug!(target: TARGET_LOG, data = ?text);
233235
Ok(())
234236
}
@@ -239,3 +241,15 @@ pub async fn register_dynamic(
239241
}
240242
}
241243
}
244+
245+
fn eval_future<T>(fut: impl std::future::Future<Output = T>) -> T {
246+
cfg_if::cfg_if! {
247+
if #[cfg(feature = "tokio-comp")] {
248+
tokio::runtime::Runtime::new().unwrap().block_on(fut)
249+
} else if #[cfg(feature = "async-std-comp")] {
250+
async_std::task::block_on(result)
251+
} else {
252+
compile_error!("tokio-comp or async-std-comp features required");
253+
}
254+
}
255+
}

0 commit comments

Comments
 (0)