Debian Codegraph @

src/graphql/utilities/assert_valid_name.py

raw
1 from typing import Optional
2
3 from ..type.assert_name import assert_name
4 from ..error import GraphQLError
5
6 __all__ = ["assert_valid_name", "is_valid_name_error"]
7
8
9 def assert_valid_name(name: str) -> str:
10 """Uphold the spec rules about naming.
11
12 This deprecated helper is retained for backwards compatibility; call ``assert_name``
13 instead because ``assert_valid_name`` will be removed in v3.3.
14
15 .. deprecated:: 3.2
16 Please use ``assert_name`` instead. Will be removed in v3.3.
17 """
18 error = is_valid_name_error(name)
19 if error:
20 raise error
21 return name
22
23
24 def is_valid_name_error(name: str) -> Optional[GraphQLError]:
25 """Return an Error if a name is invalid.
26
27 This deprecated helper is retained for backwards compatibility; call ``assert_name``
28 and catch the raised GraphQLError instead because ``is_valid_name_error`` will be
29 removed in v3.3.
30
31 .. deprecated:: 3.2
32 Please use ``assert_name`` instead. Will be removed in v3.3.
33 """
34 if not isinstance(name, str):
35 raise TypeError("Expected name to be a string.")
36 if name.startswith("__"):
37 return GraphQLError(
38 f"Name {name!r} must not begin with '__',"
39 " which is reserved by GraphQL introspection."
40 )
41 try:
42 assert_name(name)
43 except GraphQLError as error:
44 return error
45 return None