@@ -236,61 +236,6 @@ def ast_from_value(value: Any, type_: GraphQLInputType) -> Optional[ValueNode]:
236236 raise TypeError (f"Unexpected input type: { inspect (type_ )} ." )
237237
238238
239- def dsl_gql (
240- * operations : "DSLExecutable" , ** operations_with_name : "DSLExecutable"
241- ) -> GraphQLRequest :
242- r"""Given arguments instances of :class:`DSLExecutable`
243- containing GraphQL operations or fragments,
244- generate a Document which can be executed later in a
245- gql client or a gql session.
246-
247- Similar to the :func:`gql.gql` function but instead of parsing a python
248- string to describe the request, we are using operations which have been generated
249- dynamically using instances of :class:`DSLField`, generated
250- by instances of :class:`DSLType` which themselves originated from
251- a :class:`DSLSchema` class.
252-
253- :param \*operations: the GraphQL operations and fragments
254- :type \*operations: DSLQuery, DSLMutation, DSLSubscription, DSLFragment
255- :param \**operations_with_name: the GraphQL operations with an operation name
256- :type \**operations_with_name: DSLQuery, DSLMutation, DSLSubscription
257-
258- :return: a :class:`GraphQLRequest <gql.GraphQLRequest>`
259- which can be later executed or subscribed by a
260- :class:`Client <gql.client.Client>`, by an
261- :class:`async session <gql.client.AsyncClientSession>` or by a
262- :class:`sync session <gql.client.SyncClientSession>`
263-
264- :raises TypeError: if an argument is not an instance of :class:`DSLExecutable`
265- :raises AttributeError: if a type has not been provided in a :class:`DSLFragment`
266- """
267-
268- # Concatenate operations without and with name
269- all_operations : Tuple ["DSLExecutable" , ...] = (
270- * operations ,
271- * (operation for operation in operations_with_name .values ()),
272- )
273-
274- # Set the operation name
275- for name , operation in operations_with_name .items ():
276- operation .name = name
277-
278- # Check the type
279- for operation in all_operations :
280- if not isinstance (operation , DSLExecutable ):
281- raise TypeError (
282- "Operations should be instances of DSLExecutable "
283- "(DSLQuery, DSLMutation, DSLSubscription or DSLFragment).\n "
284- f"Received: { type (operation )} ."
285- )
286-
287- document = DocumentNode (
288- definitions = [operation .executable_ast for operation in all_operations ]
289- )
290-
291- return GraphQLRequest (document )
292-
293-
294239class DSLSchema :
295240 """The DSLSchema is the root of the DSL code.
296241
@@ -1520,3 +1465,58 @@ def is_valid_directive(self, directive: DSLDirective) -> bool:
15201465
15211466 def __repr__ (self ) -> str :
15221467 return f"<{ self .__class__ .__name__ } { self .name !s} >"
1468+
1469+
1470+ def dsl_gql (
1471+ * operations : DSLExecutable , ** operations_with_name : DSLExecutable
1472+ ) -> GraphQLRequest :
1473+ r"""Given arguments instances of :class:`DSLExecutable`
1474+ containing GraphQL operations or fragments,
1475+ generate a Document which can be executed later in a
1476+ gql client or a gql session.
1477+
1478+ Similar to the :func:`gql.gql` function but instead of parsing a python
1479+ string to describe the request, we are using operations which have been generated
1480+ dynamically using instances of :class:`DSLField`, generated
1481+ by instances of :class:`DSLType` which themselves originated from
1482+ a :class:`DSLSchema` class.
1483+
1484+ :param \*operations: the GraphQL operations and fragments
1485+ :type \*operations: DSLQuery, DSLMutation, DSLSubscription, DSLFragment
1486+ :param \**operations_with_name: the GraphQL operations with an operation name
1487+ :type \**operations_with_name: DSLQuery, DSLMutation, DSLSubscription
1488+
1489+ :return: a :class:`GraphQLRequest <gql.GraphQLRequest>`
1490+ which can be later executed or subscribed by a
1491+ :class:`Client <gql.client.Client>`, by an
1492+ :class:`async session <gql.client.AsyncClientSession>` or by a
1493+ :class:`sync session <gql.client.SyncClientSession>`
1494+
1495+ :raises TypeError: if an argument is not an instance of :class:`DSLExecutable`
1496+ :raises AttributeError: if a type has not been provided in a :class:`DSLFragment`
1497+ """
1498+
1499+ # Concatenate operations without and with name
1500+ all_operations : Tuple ["DSLExecutable" , ...] = (
1501+ * operations ,
1502+ * (operation for operation in operations_with_name .values ()),
1503+ )
1504+
1505+ # Set the operation name
1506+ for name , operation in operations_with_name .items ():
1507+ operation .name = name
1508+
1509+ # Check the type
1510+ for operation in all_operations :
1511+ if not isinstance (operation , DSLExecutable ):
1512+ raise TypeError (
1513+ "Operations should be instances of DSLExecutable "
1514+ "(DSLQuery, DSLMutation, DSLSubscription or DSLFragment).\n "
1515+ f"Received: { type (operation )} ."
1516+ )
1517+
1518+ document = DocumentNode (
1519+ definitions = [operation .executable_ast for operation in all_operations ]
1520+ )
1521+
1522+ return GraphQLRequest (document )
0 commit comments