pub struct GraphQLClient { /* private fields */ }Expand description
A GraphQL client for communicating with a GraphQL API endpoint.
GraphQLClient provides an easy way to execute GraphQL queries and handle their responses.
It is built on top of the RestClient from the deployment_changelog::api::rest module to handle
HTTP requests and uses the graphql_client crate for type-safe query execution.
§Example
Below is an example of how to use the GraphQLClient to execute a query:
use anyhow::Result;
use deployment_changelog::api::rest::graphql::GraphQLClient;
use graphql_client::{GraphQLQuery, Response};
// Define a query using the graphql_client macro.
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "path/to/schema.graphql",
query_path = "path/to/query.graphql",
response_derives = "Debug"
)]
struct MyQuery;
async fn execute_query() -> Result<Response<MyQuery::ResponseData>> {
// Create a new GraphQLClient instance.
let graphql_client = GraphQLClient::new("https://api.example.com")?;
// Set the query variables.
let variables = MyQuery::Variables { /* ... */ };
// Execute the query and return the result.
graphql_client.post(variables).await
}§Errors
Errors are handled using the anyhow crate, and the Result type is used to return errors
from functions. The post method can return errors related to HTTP requests, response
handling, or GraphQL-specific issues.
Implementations§
Source§impl GraphQLClient
impl GraphQLClient
Sourcepub fn new(base_url: &str) -> Result<Self>
pub fn new(base_url: &str) -> Result<Self>
Creates a new GraphQLClient instance with the given base URL.
The base URL is the root URL of the API server, without any path components. The client will append the GraphQL endpoint path to this base URL.
§Example
use deployment_changelog::api::rest::graphql::GraphQLClient;
let graphql_client = GraphQLClient::new("https://api.example.com")?;§Errors
Returns an error if the base URL cannot be parsed or if there is an error
creating the underlying RestClient.
Sourcepub fn from_client(client: RestClient) -> Self
pub fn from_client(client: RestClient) -> Self
Creates a new GraphQLClient instance using an existing RestClient.
This method can be useful if you want to share a single RestClient instance
between multiple clients, or if you need to customize the RestClient before
passing it to the GraphQLClient.
§Example
use deployment_changelog::api::rest::{graphql::GraphQLClient, RestClient};
let rest_client = RestClient::new("https://api.example.com")?;
let graphql_client = GraphQLClient::from_client(rest_client);Sourcepub async fn post<Q: GraphQLQuery>(
&self,
variables: Q::Variables,
) -> Result<Response<Q::ResponseData>>
pub async fn post<Q: GraphQLQuery>( &self, variables: Q::Variables, ) -> Result<Response<Q::ResponseData>>
Executes a GraphQL query with the given variables and returns the response.
The query is defined using the GraphQLQuery trait from the graphql_client crate.
This method takes the variables required by the query and returns a Response
containing the parsed response data or an error if the request fails.
§Example
use deployment_changelog::api::rest::graphql::GraphQLClient;
use graphql_client::{GraphQLQuery, Response};
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "path/to/schema.graphql",
query_path = "path/to/query.graphql",
response_derives = "Debug"
)]
struct MyQuery;
async fn execute_query() -> Result<Response<MyQuery::ResponseData>> {
let graphql_client = GraphQLClient::new("https://api.example.com")?;
let variables = MyQuery::Variables { /* ... */ };
graphql_client.post(variables).await
}§Errors
Returns an error if there is an issue with the HTTP request, response handling, or if the GraphQL API returns an error.