Skip to main content

GraphQLClient

Struct GraphQLClient 

Source
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

Source

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.

Source

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);
Source

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.

Trait Implementations§

Source§

impl Debug for GraphQLClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more