DEV Community

Tom Houlé
Tom Houlé

Posted on

graphql_client 0.6, to the browser and beyond

graphql_client is getting a new release today! The most visible new feature is a new graphql_client_web crate that makes calling a GraphQL API from Rust code running in browsers boilerplate-free.

Another notable change is the move into the graphql-rust organization on GitHub, where we can pool documentation and branding efforts with the other GraphQL crates. Thanks to the graphql-rust team for their support with and feedback on graphql_client!

Web client

Calling a GraphQL API in WebAssembly code inside a browser is now as simple as this:

  • Add graphql_client_web as a dependency.
  • Construct a client with Client::new("<GraphQL endpoint URL>").
  • Call it with a normal graphql_client query struct and the query variables struct.
  • You get a Rust future that returns a strongly typed response or a client error.

This is how it looks (copy-pasted directly from the test suite):

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "tests/countries_schema.json",
    query_path = "tests/Germany.graphql"
)]
struct Country;

#[wasm_bindgen_test(async)]
fn test_country() -> impl Future<Item = (), Error = JsValue> {
    Client::new("https://countries.trevorblades.com/")
        .call(
            Country,
            country::Variables {
                country_code: "CN".to_owned(),
            },
        )
        .map(|response| {
            let continent_name = response
                .data
                .expect("response data is not null")
                .country
                .expect("country is not null")
                .continent
                .expect("continent is not null")
                .name
                .expect("country is on a continent");

            assert_eq!(continent_name, "Asia");
        })
        .map_err(|err| {
            panic!("{:?}", err);
        })
}

Network (via the fetch API) and serialization/deserialization are taken care of by graphql_client_web.

This crate works with the wasm-bindgen ecosystem only (web-sys, js-sys), support for stdweb could be investigated if there is demand for it.

This is a first release, so expect some minor breaking changes, especially around error handling.

CLI

@h-michael has been pushing for code generation using the CLI, and it is shaping up rapidly with new features such as:

  • Support for query documents defining multiple queries
  • Optional formatting of the output code with rustfmt

Other fixes and improvements

There were a few other new features, bug fixes and breaking (naming) changes. You can take a look at the changelog for the full list of changes.

Next steps

reform the ninja empire

The library is now in a very usable state, and we can start thinking about a 1.0 release. There are many enhancement ideas in the issues, but the core abstractions are stable, so we should not be too afraid to take that step once the documentation is in better shape.

Thanks

As always, many warm thanks to everyone who contributed to make the crate happen!

Top comments (0)