-
Hello, I'm new to juniper and I'm loving it so far. I want to write tests but I don't know how to go around that. pub struct DemoQuery {}
#[graphql_object(context = Context)]
impl DemoQuery {
pub fn outputs(input: Input) -> Vec<Output> {
vec![Output {
id: input.id
}]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_outputs() {
let query = DemoQuery {};
let input = Input { id: 12 };
query.outputs(input); // but we can't access it like this
}
} What's the correct way to test these? Getting a couple of examples will be great. Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
ilslv
Apr 25, 2022
Replies: 1 comment 1 reply
-
You can create pub struct DemoQuery {}
#[graphql_object(context = Context)]
impl DemoQuery {
pub fn outputs(input: Input) -> Vec<Output> {
vec![Output {
id: input.id
}]
}
}
#[cfg(test)]
mod tests {
use juniper::{execute, graphql_value, graphql_vars, EmptyMutation, EmptySubscription, RootNode}
use super::*;
#[tokio::test]
async fn resolves() {
const QUERY: &str = r#"{
outputs(input: ...) {
id
}
}"#;
let schema = RootNode::new(
DemoQuery,
EmptyMutation::new(),
EmptySubscription::new(),
);
assert_eq!(
execute(QUERY, None, &schema, &graphql_vars! {}, &()).await,
Ok((
graphql_value!({"humanString": {"id": "mars", "planet": "mars"}}),
/* expected output */,
)),
);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
sanket143
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can create
RootNode
with anEmptyMutation
andEmptySubscription
and runexecute
on it. We do it like that in integration tests: