strawberry-graphql 0.173.1__py3-none-any.whl → 0.174.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ from .disable_validation import DisableValidation
6
6
  from .field_extension import FieldExtension
7
7
  from .mask_errors import MaskErrors
8
8
  from .max_aliases import MaxAliasesLimiter
9
+ from .max_tokens import MaxTokensLimiter
9
10
  from .parser_cache import ParserCache
10
11
  from .query_depth_limiter import QueryDepthLimiter
11
12
  from .validation_cache import ValidationCache
@@ -36,4 +37,5 @@ __all__ = [
36
37
  "ValidationCache",
37
38
  "MaskErrors",
38
39
  "MaxAliasesLimiter",
40
+ "MaxTokensLimiter",
39
41
  ]
@@ -19,7 +19,7 @@ class MaxAliasesLimiter(AddValidationRules):
19
19
  Example:
20
20
 
21
21
  >>> import strawberry
22
- >>> from strawberry.extensions import QueryDepthLimiter
22
+ >>> from strawberry.extensions import MaxAliasesLimiter
23
23
  >>>
24
24
  >>> schema = strawberry.Schema(
25
25
  ... Query,
@@ -0,0 +1,44 @@
1
+ from typing import Iterator
2
+
3
+ from strawberry.extensions.base_extension import SchemaExtension
4
+
5
+
6
+ class MaxTokensLimiter(SchemaExtension):
7
+ """
8
+ Add a validator to limit the number of tokens in a GraphQL document.
9
+
10
+ Example:
11
+
12
+ >>> import strawberry
13
+ >>> from strawberry.extensions import MaxTokensLimiter
14
+ >>>
15
+ >>> schema = strawberry.Schema(
16
+ ... Query,
17
+ ... extensions=[
18
+ ... MaxTokensLimiter(max_token_count=1000)
19
+ ... ]
20
+ ... )
21
+
22
+ Arguments:
23
+
24
+ `max_token_count: int`
25
+ The maximum number of tokens allowed in a GraphQL document.
26
+
27
+ The following things are counted as tokens:
28
+ * various brackets: "{", "}", "(", ")"
29
+ * colon :
30
+ * words
31
+
32
+ Not counted:
33
+ * quotes
34
+ """
35
+
36
+ def __init__(
37
+ self,
38
+ max_token_count: int,
39
+ ):
40
+ self.max_token_count = max_token_count
41
+
42
+ def on_operation(self) -> Iterator[None]:
43
+ self.execution_context.parse_options["max_tokens"] = self.max_token_count
44
+ yield
@@ -27,6 +27,8 @@ from strawberry.types import ExecutionResult
27
27
  from .exceptions import InvalidOperationTypeError
28
28
 
29
29
  if TYPE_CHECKING:
30
+ from typing_extensions import Unpack
31
+
30
32
  from graphql import ExecutionContext as GraphQLExecutionContext
31
33
  from graphql import ExecutionResult as GraphQLExecutionResult
32
34
  from graphql import GraphQLSchema
@@ -35,11 +37,12 @@ if TYPE_CHECKING:
35
37
 
36
38
  from strawberry.extensions import SchemaExtension
37
39
  from strawberry.types import ExecutionContext
40
+ from strawberry.types.execution import ParseOptions
38
41
  from strawberry.types.graphql import OperationType
39
42
 
40
43
 
41
- def parse_document(query: str) -> DocumentNode:
42
- return parse(query)
44
+ def parse_document(query: str, **kwargs: Unpack[ParseOptions]) -> DocumentNode:
45
+ return parse(query, **kwargs)
43
46
 
44
47
 
45
48
  def validate_document(
@@ -90,7 +93,7 @@ async def execute(
90
93
  try:
91
94
  if not execution_context.graphql_document:
92
95
  execution_context.graphql_document = parse_document(
93
- execution_context.query
96
+ execution_context.query, **execution_context.parse_options
94
97
  )
95
98
 
96
99
  except GraphQLError as error:
@@ -183,7 +186,7 @@ def execute_sync(
183
186
  try:
184
187
  if not execution_context.graphql_document:
185
188
  execution_context.graphql_document = parse_document(
186
- execution_context.query
189
+ execution_context.query, **execution_context.parse_options
187
190
  )
188
191
 
189
192
  except GraphQLError as error:
@@ -1,13 +1,24 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import dataclasses
4
- from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type
4
+ from typing import (
5
+ TYPE_CHECKING,
6
+ Any,
7
+ Dict,
8
+ List,
9
+ Optional,
10
+ Tuple,
11
+ Type,
12
+ )
13
+ from typing_extensions import TypedDict
5
14
 
6
15
  from graphql import specified_rules
7
16
 
8
17
  from strawberry.utils.operation import get_first_operation, get_operation_type
9
18
 
10
19
  if TYPE_CHECKING:
20
+ from typing_extensions import NotRequired
21
+
11
22
  from graphql import ASTValidationRule
12
23
  from graphql import ExecutionResult as GraphQLExecutionResult
13
24
  from graphql.error.graphql_error import GraphQLError
@@ -24,6 +35,9 @@ class ExecutionContext:
24
35
  schema: Schema
25
36
  context: Any = None
26
37
  variables: Optional[Dict[str, Any]] = None
38
+ parse_options: ParseOptions = dataclasses.field(
39
+ default_factory=lambda: ParseOptions()
40
+ )
27
41
  root_value: Optional[Any] = None
28
42
  validation_rules: Tuple[Type[ASTValidationRule], ...] = dataclasses.field(
29
43
  default_factory=lambda: tuple(specified_rules)
@@ -76,3 +90,7 @@ class ExecutionResult:
76
90
  data: Optional[Dict[str, Any]]
77
91
  errors: Optional[List[GraphQLError]]
78
92
  extensions: Optional[Dict[str, Any]] = None
93
+
94
+
95
+ class ParseOptions(TypedDict):
96
+ max_tokens: NotRequired[int]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: strawberry-graphql
3
- Version: 0.173.1
3
+ Version: 0.174.0
4
4
  Summary: A library for creating GraphQL APIs
5
5
  Home-page: https://strawberry.rocks/
6
6
  License: MIT
@@ -88,7 +88,7 @@ strawberry/ext/dataclasses/LICENSE,sha256=WZgm35K_3NJwLqxpEHJJi7CWxVrwTumEz5D3Dt
88
88
  strawberry/ext/dataclasses/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
89
89
  strawberry/ext/dataclasses/dataclasses.py,sha256=znJ1R_TwZDO3lvDeMiy0YijFK8e6i3OkFMdtSdvf83k,2175
90
90
  strawberry/ext/mypy_plugin.py,sha256=Z7U2gguKjnDdggiAP5by-Xa4hbmvgLx37zkvDewsvpY,36343
91
- strawberry/extensions/__init__.py,sha256=48dT5z6VXSJ4Uh7UKyMrhE49090TrZEjofJlOWFiPaU,1062
91
+ strawberry/extensions/__init__.py,sha256=DS0SPbZVhiFTIsCuZqQT4ibxlCMZ6TALq3qTwrqaKrs,1127
92
92
  strawberry/extensions/add_validation_rules.py,sha256=U-_U3aTNGqWd88zbzqedrLGjP1R-Pv1KyF8x_hIOoEw,1358
93
93
  strawberry/extensions/base_extension.py,sha256=Cz_ez807YB9CNe_9KrikrbJ_1WMADjGiXdwCbmW2P3s,1773
94
94
  strawberry/extensions/context.py,sha256=hBCbOEzf3S0V3Q0-xj9CaLKgO-rSVrtjGtl_JoTnvgg,6964
@@ -96,7 +96,8 @@ strawberry/extensions/directives.py,sha256=UqdH3wmK0z0TfE6bYDsBitEVdicr3qCzjzbzF
96
96
  strawberry/extensions/disable_validation.py,sha256=eLlZ0SEuvK3HUSvLm4PpdgrtP30ckG25sV3hGd2W-sM,720
97
97
  strawberry/extensions/field_extension.py,sha256=vvcL4I9BXkoVgm5nojA0TFqqOMxG6LxeKGrb9sa8H_c,5584
98
98
  strawberry/extensions/mask_errors.py,sha256=5uClsvhuRjsvU4Va9eHoWn5ePGuoWXrEudEIaS41sp8,1385
99
- strawberry/extensions/max_aliases.py,sha256=pDEoLnXBThgjurwbypme-UH6JZ4n1_jqyRQwPP_2u_0,2142
99
+ strawberry/extensions/max_aliases.py,sha256=turajK8BMOmp59o0Wr4MwyISrkn17XJmGQPVCwNkXjk,2142
100
+ strawberry/extensions/max_tokens.py,sha256=Fa-Zcugo2Y1WRINTdTRWzsRhAnBFCLAoplfnlS8j_nY,1014
100
101
  strawberry/extensions/parser_cache.py,sha256=pyZ71_oeEn0YLAZKZYidOIzpBPKp570l8GBQvW-VuN4,1186
101
102
  strawberry/extensions/pyinstrument.py,sha256=Ac21atKEyOuZs1s_1-NEes3vB8QlM5ZGw8xU6EMdMPM,892
102
103
  strawberry/extensions/query_depth_limiter.py,sha256=_Q2AI2iE51dCCTqFMl8henaHR_GEc51sPA4xviQdsGM,7852
@@ -162,7 +163,7 @@ strawberry/schema/base.py,sha256=rj4pMnVSdTclZpYIPoyWlwEtSYohqJPJzEgInsoON08,289
162
163
  strawberry/schema/compat.py,sha256=fEOig4-X5Q3TrbuHcWXk1t69TzMfOr8vy3U-xB03yEU,1949
163
164
  strawberry/schema/config.py,sha256=G1MC5DNKvSFNmg4bcu9XjQhceW8xZMIHf1hG3aS4SGA,599
164
165
  strawberry/schema/exceptions.py,sha256=_9Ua-lLRCJfbtd8B6MXILNKGI2SwHkcBNkAHYM7ITp8,534
165
- strawberry/schema/execute.py,sha256=vUjrEGzbO5L4YMIosx3RTVt9TQyogf6AeBbKWoMFdO0,10085
166
+ strawberry/schema/execute.py,sha256=6U-JOTtYn6qQgEpHpJvqkm9z5n5IwsdCSjy8yFgMVcw,10295
166
167
  strawberry/schema/name_converter.py,sha256=eQh4A-8fQAkhwgFhGk0_qFUQ2NPfqKO8DzKJVrxf0vk,6284
167
168
  strawberry/schema/schema.py,sha256=X_i3TE0Vvm9AV9QrV9qDBdSisbxAkpajpv1RXmQoYG8,11915
168
169
  strawberry/schema/schema_converter.py,sha256=eCGqb9TA3B8ipByB9Dv5bqfZ_yY4UerhbQcZXts4UhQ,30619
@@ -191,7 +192,7 @@ strawberry/tools/create_type.py,sha256=Hv2bk6uFsNeBqqXp8egA6vYzxCdudSsqpZRE1M_lk
191
192
  strawberry/tools/merge_types.py,sha256=31AqGSEn-z5FhMNUEyfFNovX_w_WGHWd9WxtXJ8X-w0,957
192
193
  strawberry/type.py,sha256=Z8WVkD_hm6qfsUSodAb4ILpOjjGQB_rz88josm0WlfM,4441
193
194
  strawberry/types/__init__.py,sha256=APb1Cjy6bxqFxIfDfempP6eb9NE3LYDwQ3gX7r07lXI,139
194
- strawberry/types/execution.py,sha256=fKvwWwOeB6zBXMyPh8GCApeL5HsJZOsqPOyeYifL9dc,2476
195
+ strawberry/types/execution.py,sha256=wPXvgaWDaF1o_qJ3WzqpVOp64_uGbB87nbyL69Gdy_4,2768
195
196
  strawberry/types/fields/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
196
197
  strawberry/types/fields/resolver.py,sha256=gvhjPbBhjaZVPe5jbt61hgKwpMIutC3IzKTMi8JgaYg,12010
197
198
  strawberry/types/graphql.py,sha256=3SWZEsa0Zy1eVW6vy75BnB7t9_lJVi6TBV3_1j3RNBs,687
@@ -214,8 +215,8 @@ strawberry/utils/logging.py,sha256=pz1pRJtx5xX2ejGKNN-fG8zPA4SJjnq1HK9xaQpjd4s,1
214
215
  strawberry/utils/operation.py,sha256=Um-tBCPl3_bVFN2Ph7o1mnrxfxBes4HFCj6T0x4kZxE,1135
215
216
  strawberry/utils/str_converters.py,sha256=VdOnzaxhwJnmQl1Lon0VOjl72uXhM8tLfGxoGwn3arI,657
216
217
  strawberry/utils/typing.py,sha256=KtfgwoTTNYSw6IDZWf1McwzxZs5D1guNCVmXt8Mh9z8,10071
217
- strawberry_graphql-0.173.1.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
218
- strawberry_graphql-0.173.1.dist-info/METADATA,sha256=yO2dD0i9N5Q9M3D2buMwmfC42-kIidof-_9ovcoMeb0,7644
219
- strawberry_graphql-0.173.1.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
220
- strawberry_graphql-0.173.1.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
221
- strawberry_graphql-0.173.1.dist-info/RECORD,,
218
+ strawberry_graphql-0.174.0.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
219
+ strawberry_graphql-0.174.0.dist-info/METADATA,sha256=qdjW1II07Hr3SeMHtNfr7FN26QgItKxcWrZJc0L_PIw,7644
220
+ strawberry_graphql-0.174.0.dist-info/WHEEL,sha256=kLuE8m1WYU0Ig0_YEGrXyTtiJvKPpLpDEiChiNyei5Y,88
221
+ strawberry_graphql-0.174.0.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
222
+ strawberry_graphql-0.174.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.5.2
2
+ Generator: poetry-core 1.5.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any