11from __future__ import annotations
22
3+ from enum import Enum
34from typing import Any
45
56from pydantic import BaseModel
67
78VARIABLE_TYPE_MAPPING = ((str , "String!" ), (int , "Int!" ), (float , "Float!" ), (bool , "Boolean!" ))
89
910
10- def convert_to_graphql_as_string (value : str | bool | list ) -> str :
11+ def convert_to_graphql_as_string (value : str | bool | list | BaseModel | Enum | Any , convert_enum : bool = False ) -> str : # noqa: PLR0911
1112 if isinstance (value , str ) and value .startswith ("$" ):
1213 return value
14+ if isinstance (value , Enum ):
15+ if convert_enum :
16+ return convert_to_graphql_as_string (value = value .value , convert_enum = True )
17+ return value .name
1318 if isinstance (value , str ):
1419 return f'"{ value } "'
1520 if isinstance (value , bool ):
1621 return repr (value ).lower ()
1722 if isinstance (value , list ):
18- values_as_string = [convert_to_graphql_as_string (item ) for item in value ]
23+ values_as_string = [convert_to_graphql_as_string (value = item , convert_enum = convert_enum ) for item in value ]
1924 return "[" + ", " .join (values_as_string ) + "]"
2025 if isinstance (value , BaseModel ):
2126 data = value .model_dump ()
22- return "{ " + ", " .join (f"{ key } : { convert_to_graphql_as_string (val )} " for key , val in data .items ()) + " }"
27+ return (
28+ "{ "
29+ + ", " .join (
30+ f"{ key } : { convert_to_graphql_as_string (value = val , convert_enum = convert_enum )} "
31+ for key , val in data .items ()
32+ )
33+ + " }"
34+ )
2335
2436 return str (value )
2537
@@ -38,7 +50,7 @@ def render_variables_to_string(data: dict[str, type[str | int | float | bool]])
3850 return ", " .join ([f"{ key } : { value } " for key , value in vars_dict .items ()])
3951
4052
41- def render_query_block (data : dict , offset : int = 4 , indentation : int = 4 ) -> list [str ]:
53+ def render_query_block (data : dict , offset : int = 4 , indentation : int = 4 , convert_enum : bool = False ) -> list [str ]:
4254 FILTERS_KEY = "@filters"
4355 ALIAS_KEY = "@alias"
4456 KEYWORDS_TO_SKIP = [FILTERS_KEY , ALIAS_KEY ]
@@ -60,25 +72,36 @@ def render_query_block(data: dict, offset: int = 4, indentation: int = 4) -> lis
6072
6173 if value .get (FILTERS_KEY ):
6274 filters_str = ", " .join (
63- [f"{ key2 } : { convert_to_graphql_as_string (value2 )} " for key2 , value2 in value [FILTERS_KEY ].items ()]
75+ [
76+ f"{ key2 } : { convert_to_graphql_as_string (value = value2 , convert_enum = convert_enum )} "
77+ for key2 , value2 in value [FILTERS_KEY ].items ()
78+ ]
6479 )
6580 lines .append (f"{ offset_str } { key_str } ({ filters_str } ) " + "{" )
6681 else :
6782 lines .append (f"{ offset_str } { key_str } " + "{" )
6883
69- lines .extend (render_query_block (data = value , offset = offset + indentation , indentation = indentation ))
84+ lines .extend (
85+ render_query_block (
86+ data = value , offset = offset + indentation , indentation = indentation , convert_enum = convert_enum
87+ )
88+ )
7089 lines .append (offset_str + "}" )
7190
7291 return lines
7392
7493
75- def render_input_block (data : dict , offset : int = 4 , indentation : int = 4 ) -> list [str ]:
94+ def render_input_block (data : dict , offset : int = 4 , indentation : int = 4 , convert_enum : bool = False ) -> list [str ]:
7695 offset_str = " " * offset
7796 lines = []
7897 for key , value in data .items ():
7998 if isinstance (value , dict ):
8099 lines .append (f"{ offset_str } { key } : " + "{" )
81- lines .extend (render_input_block (data = value , offset = offset + indentation , indentation = indentation ))
100+ lines .extend (
101+ render_input_block (
102+ data = value , offset = offset + indentation , indentation = indentation , convert_enum = convert_enum
103+ )
104+ )
82105 lines .append (offset_str + "}" )
83106 elif isinstance (value , list ):
84107 lines .append (f"{ offset_str } { key } : " + "[" )
@@ -90,14 +113,17 @@ def render_input_block(data: dict, offset: int = 4, indentation: int = 4) -> lis
90113 data = item ,
91114 offset = offset + indentation + indentation ,
92115 indentation = indentation ,
116+ convert_enum = convert_enum ,
93117 )
94118 )
95119 lines .append (f"{ offset_str } { ' ' * indentation } " + "}," )
96120 else :
97- lines .append (f"{ offset_str } { ' ' * indentation } { convert_to_graphql_as_string (item )} ," )
121+ lines .append (
122+ f"{ offset_str } { ' ' * indentation } { convert_to_graphql_as_string (value = item , convert_enum = convert_enum )} ,"
123+ )
98124 lines .append (offset_str + "]" )
99125 else :
100- lines .append (f"{ offset_str } { key } : { convert_to_graphql_as_string (value )} " )
126+ lines .append (f"{ offset_str } { key } : { convert_to_graphql_as_string (value = value , convert_enum = convert_enum )} " )
101127 return lines
102128
103129
@@ -127,9 +153,13 @@ def render_first_line(self) -> str:
127153class Query (BaseGraphQLQuery ):
128154 query_type = "query"
129155
130- def render (self ) -> str :
156+ def render (self , convert_enum : bool = False ) -> str :
131157 lines = [self .render_first_line ()]
132- lines .extend (render_query_block (data = self .query , indentation = self .indentation , offset = self .indentation ))
158+ lines .extend (
159+ render_query_block (
160+ data = self .query , indentation = self .indentation , offset = self .indentation , convert_enum = convert_enum
161+ )
162+ )
133163 lines .append ("}" )
134164
135165 return "\n " + "\n " .join (lines ) + "\n "
@@ -143,14 +173,15 @@ def __init__(self, *args: Any, mutation: str, input_data: dict, **kwargs: Any):
143173 self .mutation = mutation
144174 super ().__init__ (* args , ** kwargs )
145175
146- def render (self ) -> str :
176+ def render (self , convert_enum : bool = False ) -> str :
147177 lines = [self .render_first_line ()]
148178 lines .append (" " * self .indentation + f"{ self .mutation } (" )
149179 lines .extend (
150180 render_input_block (
151181 data = self .input_data ,
152182 indentation = self .indentation ,
153183 offset = self .indentation * 2 ,
184+ convert_enum = convert_enum ,
154185 )
155186 )
156187 lines .append (" " * self .indentation + "){" )
@@ -159,6 +190,7 @@ def render(self) -> str:
159190 data = self .query ,
160191 indentation = self .indentation ,
161192 offset = self .indentation * 2 ,
193+ convert_enum = convert_enum ,
162194 )
163195 )
164196 lines .append (" " * self .indentation + "}" )
0 commit comments