Skip to content

Commit d02bed3

Browse files
committed
Modify GraphQLRequest to allow str input
1 parent 93274ea commit d02bed3

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

gql/graphql_request.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
from dataclasses import dataclass
2-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Optional, Union
32

43
from graphql import DocumentNode, GraphQLSchema, print_ast
54

5+
from .gql import gql
66
from .utilities import serialize_variable_values
77

88

9-
@dataclass(frozen=True)
109
class GraphQLRequest:
1110
"""GraphQL Request to be executed."""
1211

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.
1521
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
1833

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
2436

2537
def serialize_variable_values(self, schema: GraphQLSchema) -> "GraphQLRequest":
2638
assert self.variable_values
@@ -48,3 +60,6 @@ def payload(self) -> Dict[str, Any]:
4860
payload["variables"] = self.variable_values
4961

5062
return payload
63+
64+
def __str__(self):
65+
return str(self.payload)

tests/test_graphql_request.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from gql import GraphQLRequest, gql
2222

23-
from .conftest import MS
23+
from .conftest import MS, strip_braces_spaces
2424

2525
# Marking all tests in this file with the aiohttp marker
2626
pytestmark = pytest.mark.aiohttp
@@ -200,3 +200,13 @@ def test_serialize_variables_using_money_example():
200200
req = req.serialize_variable_values(schema)
201201

202202
assert req.variable_values == {"money": {"amount": 10, "currency": "DM"}}
203+
204+
205+
def test_graphql_request_using_string_instead_of_document():
206+
request = GraphQLRequest("{balance}")
207+
208+
expected_payload = "{'query': '{\\n balance\\n}'}"
209+
210+
print(request)
211+
212+
assert str(request) == strip_braces_spaces(expected_payload)

0 commit comments

Comments
 (0)