[][src]Trait graphql_client::GraphQLQuery

pub trait GraphQLQuery {
    type Variables: Serialize;
    type ResponseData: for<'de> 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:

use graphql_client::*;
use serde_json::json;

#[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

type Variables: Serialize

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

type ResponseData: for<'de> Deserialize<'de>

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.

Loading content...

Required methods

fn build_query(variables: Self::Variables) -> QueryBody<Self::Variables>

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

Loading content...

Implementors

Loading content...