Hi, I am writing some tests and need to construct graphql query against my service, I am doing format! at the moment, but wondering if there is any composable way to construct the query, thanks!
You can try graphql-client.
Here is a reference: https://vector.dev/blog/graphql-api/
@phungleson if you want to do it manually you can do it with something like the following:
pub const CUSTOMER_GRAPHQL_FIELDS: &str = "#
id,
firstName,
lastName,
email,
createdAt,
lastModified
#";
// ....
let graphql_mutatation = format!(
r#"
mutation login($email: String!, $password: String!) {{
login(email: $email, password: $password) {{
{}
}}
}}
"#,
CUSTOMER_GRAPHQL_FIELDS,
);
let body = json!({
"query": graphql_mutatation,
"variables": {
"email": email,
"password": password
}
});
Then send the request to the server with any standard HTTP Client eg. Reqwest.
Hopefully that helps?
(ps. the double {{ is escaping the {)
One more example of the client with the test infrastructure: https://github.com/rkudryashov/graphql-rust-demo/blob/master/planets-service/tests/query_tests.rs#L117
Thanks everyone for the suggestions.
json! with variables looks good, at least I can avoid escaping some double quotes.
fragment also looks interesting, I will wrap my head around it and see if it can make some code re-usable.