Trait graphql_client::GraphQLQuery[][src]

pub trait GraphQLQuery<'de> {
    type Variables: Serialize;
    type ResponseData: Deserialize<'de>;
    fn build_query(variables: Self::Variables) -> QueryBody<Self::Variables>;
}

A convenience trait that can be used to build a GraphQL request body.

This will be implemented for you by codegen in the normal case. It is implemented on the struct you place the derive on.

Example:

extern crate failure;
#[macro_use]
extern crate graphql_client;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate serde;

#[derive(GraphQLQuery)]
#[graphql(
  query_path = "graphql_client_codegen/src/tests/star_wars_query.graphql",
  schema_path = "graphql_client_codegen/src/tests/star_wars_schema.graphql"
)]
struct StarWarsQuery;

fn main() -> Result<(), failure::Error> {
    use graphql_client::GraphQLQuery;

    let variables = star_wars_query::Variables {
        episode_for_hero: star_wars_query::Episode::NEWHOPE,
    };

    let expected_body = json!({
        "operationName": star_wars_query::OPERATION_NAME,
        "query": star_wars_query::QUERY,
        "variables": {
            "episodeForHero": "NEWHOPE"
        },
    });

    let actual_body = serde_json::to_value(
        StarWarsQuery::build_query(variables)
    )?;

    assert_eq!(actual_body, expected_body);

    Ok(())
}

Associated Types

The shape of the variables expected by the query. This should be a generated struct most of the time.

The top-level shape of the response data (the data field in the GraphQL response). In practice this should be generated, since it is hard to write by hand without error.

Required Methods

Produce a GraphQL query struct that can be JSON serialized and sent to a GraphQL API.

Implementors