Async-graphql: How to construct graphql query

Created on 31 Dec 2020  路  4Comments  路  Source: async-graphql/async-graphql

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!

question

All 4 comments

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 {)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AurelienFT picture AurelienFT  路  5Comments

chipsenkbeil picture chipsenkbeil  路  4Comments

Kestrer picture Kestrer  路  5Comments

leebenson picture leebenson  路  4Comments

danielSanchezQ picture danielSanchezQ  路  4Comments