|
7 | 7 | import re |
8 | 8 | from abc import ABC, abstractmethod |
9 | 9 | from math import isfinite |
10 | | -from typing import Any, Dict, Iterable, Mapping, Optional, Set, Tuple, Union, cast |
| 10 | +from typing import ( |
| 11 | + Any, |
| 12 | + Dict, |
| 13 | + Iterable, |
| 14 | + Literal, |
| 15 | + Mapping, |
| 16 | + Optional, |
| 17 | + Set, |
| 18 | + Tuple, |
| 19 | + Union, |
| 20 | + cast, |
| 21 | + overload, |
| 22 | +) |
11 | 23 |
|
12 | 24 | from graphql import ( |
13 | 25 | ArgumentNode, |
@@ -297,28 +309,48 @@ def __init__(self, schema: GraphQLSchema): |
297 | 309 |
|
298 | 310 | self._schema: GraphQLSchema = schema |
299 | 311 |
|
300 | | - def __call__(self, name: str) -> "DSLDirective": |
| 312 | + @overload |
| 313 | + def __call__( |
| 314 | + self, shortcut: Literal["__typename", "__schema", "__type"] |
| 315 | + ) -> "DSLMetaField": ... # pragma: no cover |
| 316 | + |
| 317 | + @overload |
| 318 | + def __call__( |
| 319 | + self, shortcut: Literal["..."] |
| 320 | + ) -> "DSLInlineFragment": ... # pragma: no cover |
| 321 | + |
| 322 | + @overload |
| 323 | + def __call__( |
| 324 | + self, shortcut: Literal["fragment"], name: str |
| 325 | + ) -> "DSLFragment": ... # pragma: no cover |
| 326 | + |
| 327 | + @overload |
| 328 | + def __call__(self, shortcut: Any) -> "DSLDirective": ... # pragma: no cover |
| 329 | + |
| 330 | + def __call__( |
| 331 | + self, shortcut: str, name: Optional[str] = None |
| 332 | + ) -> Union["DSLMetaField", "DSLInlineFragment", "DSLFragment", "DSLDirective"]: |
301 | 333 | """Factory method for creating DSL objects. |
302 | 334 |
|
303 | 335 | Currently, supports creating DSLDirective instances when name starts with '@'. |
304 | 336 | Future support planned for meta-fields (__typename), inline fragments (...), |
305 | 337 | and fragment definitions (fragment). |
306 | 338 |
|
307 | | - :param name: the name of the object to create |
308 | | - :type name: str |
| 339 | + :param shortcut: the name of the object to create |
| 340 | + :type shortcut: LiteralString |
309 | 341 |
|
310 | 342 | :return: DSLDirective instance |
311 | 343 |
|
312 | | - :raises ValueError: if name format is not supported |
| 344 | + :raises ValueError: if shortcut format is not supported |
313 | 345 | """ |
314 | | - if name.startswith("@"): |
315 | | - return DSLDirective(name=name[1:], dsl_schema=self) |
| 346 | + if shortcut.startswith("@"): |
| 347 | + return DSLDirective(name=shortcut[1:], dsl_schema=self) |
316 | 348 | # Future support: |
317 | 349 | # if name.startswith("__"): return DSLMetaField(name) |
318 | 350 | # if name == "...": return DSLInlineFragment() |
319 | 351 | # if name.startswith("fragment "): return DSLFragment(name[9:]) |
320 | 352 |
|
321 | | - raise ValueError(f"Unsupported name: {name}") |
| 353 | + raise ValueError(f"Unsupported shortcut: {shortcut}") |
322 | 354 |
|
323 | 355 | def __getattr__(self, name: str) -> "DSLType": |
324 | 356 |
|
@@ -549,7 +581,7 @@ def is_valid_directive(self, directive: "DSLDirective") -> bool: |
549 | 581 | """ |
550 | 582 | raise NotImplementedError( |
551 | 583 | "Any DSLDirectable concrete class must have an is_valid_directive method" |
552 | | - ) |
| 584 | + ) # pragma: no cover |
553 | 585 |
|
554 | 586 | def directives(self, *directives: DSLDirective) -> Any: |
555 | 587 | r"""Add directives to this DSL element. |
|
0 commit comments