1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crateenv_var;
use crateprint_formatted_error;
use cratetokio_runtime;
use Response;
use ;
/// Executes a GraphQL request and returns the response data.
///
/// This function takes care of sending a GraphQL request to the specified GraphQL server,
/// handling errors, and parsing the response data into the specified type `D`.
///
/// # Arguments
///
/// * `authorization_headers` - The authorization headers to include in the request.
/// * `build_query_fn` - A function that builds the GraphQL query body based on the provided variables.
/// * `client` - The Reqwest HTTP client used to make the request.
/// * `error_message` - The error message to display and exit with if an error occurs.
/// * `variables` - The variables to include in the GraphQL request.
///
/// # Returns
///
/// The response data of type `D` from the GraphQL server.
///
/// # Panics
///
/// This function will panic and terminate the program if there are errors during the request or
/// if the response does not contain valid data.
///
/// # Example
///
/// ```rust
/// use reqwest::Client;
/// use graphql_client::QueryBody;
///
/// // Define a custom GraphQL query type.
/// struct MyQuery;
/// struct MyVariables;
/// struct MyResponseData;
///
/// // Implement the `GraphQLQuery` trait for the custom query type.
/// impl graphql_client::GraphQLQuery for MyQuery {
/// type Variables = MyVariables;
/// type ResponseData = MyResponseData;
/// fn build_query(variables: Self::Variables) -> QueryBody<Self::Variables> {
/// // Build and return the GraphQL query body here.
/// // This function should be specific to your GraphQL schema.
/// unimplemented!();
/// }
/// }
///
/// // Define authorization headers, client, and variables.
/// let authorization_headers = HeaderMap::new();
/// let client = Client::new();
/// let variables = MyVariables;
///
/// // Execute the GraphQL request.
/// let response_data: MyResponseData = execute_graphql_request(
/// authorization_headers,
/// MyQuery::build_query,
/// &client,
/// "Failed to fetch data from GraphQL server.",
/// variables,
/// );
/// ```