|
1 |
| -from dataclasses import dataclass |
2 |
| -from typing import Any, Dict, Optional |
| 1 | +from typing import Any, Dict, Optional, Union |
3 | 2 |
|
4 | 3 | from graphql import DocumentNode, GraphQLSchema, print_ast
|
5 | 4 |
|
| 5 | +from .gql import gql |
6 | 6 | from .utilities import serialize_variable_values
|
7 | 7 |
|
8 | 8 |
|
9 |
| -@dataclass(frozen=True) |
10 | 9 | class GraphQLRequest:
|
11 | 10 | """GraphQL Request to be executed."""
|
12 | 11 |
|
13 |
| - document: DocumentNode |
14 |
| - """GraphQL query as AST Node object.""" |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + document: Union[DocumentNode, str], |
| 15 | + *, |
| 16 | + variable_values: Optional[Dict[str, Any]] = None, |
| 17 | + operation_name: Optional[str] = None, |
| 18 | + ): |
| 19 | + """ |
| 20 | + Initialize a GraphQL request. |
15 | 21 |
|
16 |
| - variable_values: Optional[Dict[str, Any]] = None |
17 |
| - """Dictionary of input parameters (Default: None).""" |
| 22 | + Args: |
| 23 | + document: GraphQL query as AST Node object or as a string. |
| 24 | + If string, it will be converted to DocumentNode using gql(). |
| 25 | + variable_values: Dictionary of input parameters (Default: None). |
| 26 | + operation_name: Name of the operation that shall be executed. |
| 27 | + Only required in multi-operation documents (Default: None). |
| 28 | + """ |
| 29 | + if isinstance(document, str): |
| 30 | + self.document = gql(document) |
| 31 | + else: |
| 32 | + self.document = document |
18 | 33 |
|
19 |
| - operation_name: Optional[str] = None |
20 |
| - """ |
21 |
| - Name of the operation that shall be executed. |
22 |
| - Only required in multi-operation documents (Default: None). |
23 |
| - """ |
| 34 | + self.variable_values = variable_values |
| 35 | + self.operation_name = operation_name |
24 | 36 |
|
25 | 37 | def serialize_variable_values(self, schema: GraphQLSchema) -> "GraphQLRequest":
|
26 | 38 | assert self.variable_values
|
@@ -48,3 +60,6 @@ def payload(self) -> Dict[str, Any]:
|
48 | 60 | payload["variables"] = self.variable_values
|
49 | 61 |
|
50 | 62 | return payload
|
| 63 | + |
| 64 | + def __str__(self): |
| 65 | + return str(self.payload) |
0 commit comments