strawberry-graphql 0.288.4__py3-none-any.whl → 0.289.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.
strawberry/types/enum.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import dataclasses
2
2
  from collections.abc import Callable, Iterable, Mapping
3
3
  from enum import EnumMeta
4
- from typing import TYPE_CHECKING, Any, TypeGuard, TypeVar, overload
4
+ from typing import TYPE_CHECKING, Any, Literal, TypeGuard, TypeVar, overload
5
5
 
6
6
  from strawberry.exceptions import ObjectIsNotAnEnumError
7
7
  from strawberry.types.base import (
@@ -103,6 +103,7 @@ def enum_value(
103
103
 
104
104
 
105
105
  EnumType = TypeVar("EnumType", bound=EnumMeta)
106
+ GraphqlEnumNameFrom = Literal["key", "value"]
106
107
 
107
108
 
108
109
  def _process_enum(
@@ -110,6 +111,7 @@ def _process_enum(
110
111
  name: str | None = None,
111
112
  description: str | None = None,
112
113
  directives: Iterable[object] = (),
114
+ graphql_name_from: GraphqlEnumNameFrom = "key",
113
115
  ) -> EnumType:
114
116
  if not isinstance(cls, EnumMeta):
115
117
  raise ObjectIsNotAnEnumError(cls)
@@ -119,6 +121,7 @@ def _process_enum(
119
121
 
120
122
  values = []
121
123
  for item in cls: # type: ignore
124
+ graphql_name = None
122
125
  item_value = item.value
123
126
  item_name = item.name
124
127
  deprecation_reason = None
@@ -135,13 +138,19 @@ def _process_enum(
135
138
  cls._value2member_map_[item_value.value] = item
136
139
  cls._member_map_[item_name]._value_ = item_value.value
137
140
 
138
- if item_value.graphql_name:
139
- item_name = item_value.graphql_name
140
-
141
+ graphql_name = item_value.graphql_name
141
142
  item_value = item_value.value
142
143
 
144
+ if not graphql_name:
145
+ if graphql_name_from == "key":
146
+ graphql_name = item_name
147
+ elif graphql_name_from == "value":
148
+ graphql_name = item_value
149
+ else:
150
+ raise ValueError(f"Invalid mode: {graphql_name_from}")
151
+
143
152
  value = EnumValue(
144
- item_name,
153
+ graphql_name,
145
154
  item_value,
146
155
  deprecation_reason=deprecation_reason,
147
156
  directives=item_directives,
@@ -174,6 +183,7 @@ def enum(
174
183
  name: str | None = None,
175
184
  description: str | None = None,
176
185
  directives: Iterable[object] = (),
186
+ graphql_name_from: GraphqlEnumNameFrom = "key",
177
187
  ) -> EnumType: ...
178
188
 
179
189
 
@@ -184,6 +194,7 @@ def enum(
184
194
  name: str | None = None,
185
195
  description: str | None = None,
186
196
  directives: Iterable[object] = (),
197
+ graphql_name_from: GraphqlEnumNameFrom = "key",
187
198
  ) -> Callable[[EnumType], EnumType]: ...
188
199
 
189
200
 
@@ -193,18 +204,20 @@ def enum(
193
204
  name: str | None = None,
194
205
  description: str | None = None,
195
206
  directives: Iterable[object] = (),
207
+ graphql_name_from: GraphqlEnumNameFrom = "key",
196
208
  ) -> EnumType | Callable[[EnumType], EnumType]:
197
209
  """Annotates an Enum class a GraphQL enum.
198
210
 
199
211
  GraphQL enums only have names, while Python enums have names and values,
200
- Strawberry will use the names of the Python enum as the names of the
201
- GraphQL enum values.
212
+ Strawberry will by default use the names of the Python enum as the names of the
213
+ GraphQL enum values. You can use the values instead by using mode="value".
202
214
 
203
215
  Args:
204
216
  cls: The Enum class to be annotated.
205
217
  name: The name of the GraphQL enum.
206
218
  description: The description of the GraphQL enum.
207
219
  directives: The directives to attach to the GraphQL enum.
220
+ graphql_name_from: Whether to use the names (key) or values of the Python enums in GraphQL.
208
221
 
209
222
  Returns:
210
223
  The decorated Enum class.
@@ -235,7 +248,13 @@ def enum(
235
248
  """
236
249
 
237
250
  def wrap(cls: EnumType) -> EnumType:
238
- return _process_enum(cls, name, description, directives=directives)
251
+ return _process_enum(
252
+ cls,
253
+ name,
254
+ description,
255
+ directives=directives,
256
+ graphql_name_from=graphql_name_from,
257
+ )
239
258
 
240
259
  if not cls:
241
260
  return wrap
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: strawberry-graphql
3
- Version: 0.288.4
3
+ Version: 0.289.0
4
4
  Summary: A library for creating GraphQL APIs
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -210,7 +210,7 @@ strawberry/types/arguments.py,sha256=3Rt1WlVDMc0KKYmJOnMUckeTB0egJv96Uw7RMopM4DM
210
210
  strawberry/types/auto.py,sha256=4sDflIg-Kz-QpK7Qo7bCSFblw7VE-0752rgayukFznU,2954
211
211
  strawberry/types/base.py,sha256=Hubo7u6OsU1v1v-w-vBCwlNVTqHaNn4zPOJuX93WR5U,15699
212
212
  strawberry/types/cast.py,sha256=fx86MkLW77GIximBAwUk5vZxSGwDqUA6XicXvz8EXwQ,916
213
- strawberry/types/enum.py,sha256=Dc1emdlBi1m9pK9h6rdyq8VRaBLafQO7MQdKhVgYXvs,7286
213
+ strawberry/types/enum.py,sha256=sL7SMLx7W9h0-zxtKews6drfYtLUVZ1VorXuXp83mj4,8103
214
214
  strawberry/types/execution.py,sha256=z84AekAD_V_F4vGGHMPMGlAT0Htiw5JhEVvw1F9sGYQ,4019
215
215
  strawberry/types/field.py,sha256=-E5jfq7YXzI2CUepttfGzgmXFA1BTR2hmts44P-JpEk,20422
216
216
  strawberry/types/fields/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -238,8 +238,8 @@ strawberry/utils/logging.py,sha256=Dnivjd0ZhK_lAvjvuyCDkEWDhuURBoK9d3Kt_mIqbRg,7
238
238
  strawberry/utils/operation.py,sha256=Qs3ttbuC415xEVqmJ6YsWQpJNUo8CZJq9AoMB-yV65w,1215
239
239
  strawberry/utils/str_converters.py,sha256=-eH1Cl16IO_wrBlsGM-km4IY0IKsjhjnSNGRGOwQjVM,897
240
240
  strawberry/utils/typing.py,sha256=eE9NeMfASeXRstbjLnQFfOPymcSX8xwg3FGw_HCp95E,11828
241
- strawberry_graphql-0.288.4.dist-info/METADATA,sha256=LiS09hr9ulZqpMfHbqvHv4rVtV7Ej_WIgApx4CQU-C0,7627
242
- strawberry_graphql-0.288.4.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
243
- strawberry_graphql-0.288.4.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
244
- strawberry_graphql-0.288.4.dist-info/licenses/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
245
- strawberry_graphql-0.288.4.dist-info/RECORD,,
241
+ strawberry_graphql-0.289.0.dist-info/METADATA,sha256=kS72_h2N1ZRutWs8b46_u8-eKJHscc495FQ56I_qNSE,7627
242
+ strawberry_graphql-0.289.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
243
+ strawberry_graphql-0.289.0.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
244
+ strawberry_graphql-0.289.0.dist-info/licenses/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
245
+ strawberry_graphql-0.289.0.dist-info/RECORD,,