We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add support for renaming fields
We should be able to specify a custom name for a field when needed, like this:

```python
@strawberry.type
class Query:
    example_field: str = strawberry.field(name="example")
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `strawberry/field.py`
Content:
```
1 import typing
2 
3 import dataclasses
4 from graphql import GraphQLField
5 
6 from .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT
7 from .exceptions import MissingArgumentsAnnotationsError, MissingReturnAnnotationError
8 from .type_converter import REGISTRY, get_graphql_type_for_annotation
9 from .utils.dict_to_type import dict_to_type
10 from .utils.inspect import get_func_args
11 from .utils.lazy_property import lazy_property
12 from .utils.str_converters import to_camel_case, to_snake_case
13 from .utils.typing import (
14     get_list_annotation,
15     get_optional_annotation,
16     is_list,
17     is_optional,
18 )
19 
20 
21 class LazyFieldWrapper:
22     """A lazy wrapper for a strawberry field.
23     This allows to use cyclic dependencies in a strawberry fields:
24 
25     >>> @strawberry.type
26     >>> class TypeA:
27     >>>     @strawberry.field
28     >>>     def type_b(self, info) -> "TypeB":
29     >>>         from .type_b import TypeB
30     >>>         return TypeB()
31     """
32 
33     def __init__(self, obj, is_subscription, **kwargs):
34         self._wrapped_obj = obj
35         self.is_subscription = is_subscription
36         self.kwargs = kwargs
37 
38         if callable(self._wrapped_obj):
39             self._check_has_annotations()
40 
41     def _check_has_annotations(self):
42         # using annotations without passing from typing.get_type_hints
43         # as we don't the actually types for the annotations
44         annotations = self._wrapped_obj.__annotations__
45         name = self._wrapped_obj.__name__
46 
47         if "return" not in annotations:
48             raise MissingReturnAnnotationError(name)
49 
50         function_arguments = set(get_func_args(self._wrapped_obj)) - {"self", "info"}
51 
52         arguments_annotations = {
53             key: value
54             for key, value in annotations.items()
55             if key not in ["info", "return"]
56         }
57 
58         annotated_function_arguments = set(arguments_annotations.keys())
59         arguments_missing_annotations = (
60             function_arguments - annotated_function_arguments
61         )
62 
63         if len(arguments_missing_annotations) > 0:
64             raise MissingArgumentsAnnotationsError(name, arguments_missing_annotations)
65 
66     def __getattr__(self, attr):
67         if attr in self.__dict__:
68             return getattr(self, attr)
69 
70         return getattr(self._wrapped_obj, attr)
71 
72     def __call__(self, *args, **kwargs):
73         return self._wrapped_obj(self, *args, **kwargs)
74 
75     @lazy_property
76     def field(self):
77         return _get_field(
78             self._wrapped_obj, is_subscription=self.is_subscription, **self.kwargs
79         )
80 
81 
82 class strawberry_field:
83     """A small wrapper for a field in strawberry.
84 
85     You shouldn't be using this directly as this is used internally
86     when using `strawberry.field`.
87 
88     This allows to use the following two syntaxes when using the type
89     decorator:
90 
91     >>> class X:
92     >>>     field_abc: str = strawberry.field(description="ABC")
93 
94     >>> class X:
95     >>>     @strawberry.field(description="ABC")
96     >>>     def field_a(self, info) -> str:
97     >>>         return "abc"
98 
99     When calling this class as strawberry_field it creates a field
100     that stores metadata (such as field description). In addition
101     to that it also acts as decorator when called as a function,
102     allowing us to us both syntaxes.
103     """
104 
105     def __init__(self, *, is_subscription=False, **kwargs):
106         self.field = dataclasses.field()
107         self.is_subscription = is_subscription
108         self.description = kwargs.get("description", None)
109         self.kwargs = kwargs
110 
111     def __call__(self, wrap):
112         setattr(wrap, IS_STRAWBERRY_FIELD, True)
113 
114         self.kwargs["description"] = self.description or wrap.__doc__
115 
116         return LazyFieldWrapper(wrap, self.is_subscription, **self.kwargs)
117 
118 
119 def convert_args(args, annotations):
120     """Converts a nested dictionary to a dictionary of strawberry input types."""
121 
122     converted_args = {}
123 
124     for key, value in args.items():
125         key = to_snake_case(key)
126         annotation = annotations[key]
127 
128         # we don't need to check about unions here since they are not
129         # yet supported for arguments.
130         # see https://github.com/graphql/graphql-spec/issues/488
131 
132         is_list_of_args = False
133 
134         if is_optional(annotation):
135             annotation = get_optional_annotation(annotation)
136 
137         if is_list(annotation):
138             annotation = get_list_annotation(annotation)
139             is_list_of_args = True
140 
141         if getattr(annotation, IS_STRAWBERRY_INPUT, False):
142             if is_list_of_args:
143                 converted_args[key] = [dict_to_type(x, annotation) for x in value]
144             else:
145                 converted_args[key] = dict_to_type(value, annotation)
146         else:
147             converted_args[key] = value
148 
149     return converted_args
150 
151 
152 def _get_field(wrap, *, is_subscription=False, **kwargs):
153     annotations = typing.get_type_hints(wrap, None, REGISTRY)
154 
155     name = wrap.__name__
156 
157     field_type = get_graphql_type_for_annotation(annotations["return"], name)
158 
159     arguments_annotations = {
160         key: value
161         for key, value in annotations.items()
162         if key not in ["info", "return"]
163     }
164 
165     arguments = {
166         to_camel_case(name): get_graphql_type_for_annotation(annotation, name)
167         for name, annotation in arguments_annotations.items()
168     }
169 
170     def resolver(source, info, **args):
171         args = convert_args(args, arguments_annotations)
172 
173         return wrap(source, info, **args)
174 
175     if is_subscription:
176 
177         def _resolve(event, info):
178             return event
179 
180         kwargs.update({"subscribe": resolver, "resolve": _resolve})
181     else:
182         kwargs.update({"resolve": resolver})
183 
184     kwargs["description"] = kwargs.get("description", wrap.__doc__)
185 
186     return GraphQLField(field_type, args=arguments, **kwargs)
187 
188 
189 def field(wrap=None, *, is_subscription=False, description=None):
190     """Annotates a method or property as a GraphQL field.
191 
192     This is normally used inside a type declaration:
193 
194     >>> @strawberry.type:
195     >>> class X:
196     >>>     field_abc: str = strawberry.field(description="ABC")
197 
198     >>>     @strawberry.field(description="ABC")
199     >>>     def field_with_resolver(self, info) -> str:
200     >>>         return "abc"
201 
202     it can be used both as decorator and as a normal function.
203     """
204 
205     field = strawberry_field(description=description, is_subscription=is_subscription)
206 
207     # when calling this with parens we are going to return a strawberry_field
208     # instance, so it can be used as both decorator and function.
209 
210     if wrap is None:
211         return field
212 
213     # otherwise we run the decorator directly,
214     # when called as @strawberry.field, without parens.
215 
216     return field(wrap)
217
```

Path: `strawberry/type.py`
Content:
```
1 import typing
2 from functools import partial
3 
4 from dataclasses import dataclass
5 from graphql import (
6     GraphQLField,
7     GraphQLInputField,
8     GraphQLInputObjectType,
9     GraphQLInterfaceType,
10     GraphQLObjectType,
11 )
12 from graphql.utilities.schema_printer import print_type
13 
14 from .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT, IS_STRAWBERRY_INTERFACE
15 from .type_converter import REGISTRY, get_graphql_type_for_annotation
16 from .utils.str_converters import to_camel_case
17 
18 
19 def _get_resolver(cls, field_name):
20     def _resolver(obj, info):
21         # TODO: can we make this nicer?
22         # does it work in all the cases?
23 
24         field_resolver = getattr(cls(**(obj.__dict__ if obj else {})), field_name)
25 
26         if getattr(field_resolver, IS_STRAWBERRY_FIELD, False):
27             return field_resolver(obj, info)
28 
29         return field_resolver
30 
31     return _resolver
32 
33 
34 def _convert_annotations_fields(cls, *, is_input=False):
35     FieldClass = GraphQLInputField if is_input else GraphQLField
36     annotations = typing.get_type_hints(cls, None, REGISTRY)
37 
38     fields = {}
39 
40     for key, annotation in annotations.items():
41         field_name = to_camel_case(key)
42         class_field = getattr(cls, key, None)
43 
44         description = getattr(class_field, "description", None)
45 
46         fields[field_name] = FieldClass(
47             get_graphql_type_for_annotation(annotation, key),
48             description=description,
49             **({} if is_input else {"resolve": _get_resolver(cls, key)})
50         )
51 
52     return fields
53 
54 
55 def _process_type(cls, *, is_input=False, is_interface=False, description=None):
56     name = cls.__name__
57     REGISTRY[name] = cls
58 
59     def repr_(self):
60         return print_type(self.field)
61 
62     setattr(cls, "__repr__", repr_)
63 
64     def _get_fields():
65         fields = _convert_annotations_fields(cls, is_input=is_input)
66 
67         fields.update(
68             {
69                 to_camel_case(key): value.field
70                 for key, value in cls.__dict__.items()
71                 if getattr(value, IS_STRAWBERRY_FIELD, False)
72             }
73         )
74 
75         return fields
76 
77     if is_input:
78         setattr(cls, IS_STRAWBERRY_INPUT, True)
79     elif is_interface:
80         setattr(cls, IS_STRAWBERRY_INTERFACE, True)
81 
82     extra_kwargs = {"description": description or cls.__doc__}
83 
84     if is_input:
85         TypeClass = GraphQLInputObjectType
86     elif is_interface:
87         TypeClass = GraphQLInterfaceType
88     else:
89         TypeClass = GraphQLObjectType
90 
91         extra_kwargs["interfaces"] = [
92             klass.field
93             for klass in cls.__bases__
94             if hasattr(klass, IS_STRAWBERRY_INTERFACE)
95         ]
96 
97     cls.field = TypeClass(name, lambda: _get_fields(), **extra_kwargs)
98 
99     return dataclass(cls, repr=False)
100 
101 
102 def type(cls=None, *, is_input=False, is_interface=False, description=None):
103     """Annotates a class as a GraphQL type.
104 
105     Example usage:
106 
107     >>> @strawberry.type:
108     >>> class X:
109     >>>     field_abc: str = "ABC"
110     """
111 
112     def wrap(cls):
113         return _process_type(
114             cls, is_input=is_input, is_interface=is_interface, description=description
115         )
116 
117     if cls is None:
118         return wrap
119 
120     return wrap(cls)
121 
122 
123 input = partial(type, is_input=True)
124 interface = partial(type, is_interface=True)
125
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/strawberry/field.py b/strawberry/field.py
--- a/strawberry/field.py
+++ b/strawberry/field.py
@@ -30,9 +30,10 @@
     >>>         return TypeB()
     """
 
-    def __init__(self, obj, is_subscription, **kwargs):
+    def __init__(self, obj, is_subscription, name=None, **kwargs):
         self._wrapped_obj = obj
         self.is_subscription = is_subscription
+        self.name = name
         self.kwargs = kwargs
 
         if callable(self._wrapped_obj):
@@ -106,6 +107,7 @@
         self.field = dataclasses.field()
         self.is_subscription = is_subscription
         self.description = kwargs.get("description", None)
+        self.name = kwargs.pop("name", None)
         self.kwargs = kwargs
 
     def __call__(self, wrap):
@@ -113,7 +115,7 @@
 
         self.kwargs["description"] = self.description or wrap.__doc__
 
-        return LazyFieldWrapper(wrap, self.is_subscription, **self.kwargs)
+        return LazyFieldWrapper(wrap, self.is_subscription, self.name, **self.kwargs)
 
 
 def convert_args(args, annotations):
@@ -186,7 +188,7 @@
     return GraphQLField(field_type, args=arguments, **kwargs)
 
 
-def field(wrap=None, *, is_subscription=False, description=None):
+def field(wrap=None, *, is_subscription=False, name=None, description=None):
     """Annotates a method or property as a GraphQL field.
 
     This is normally used inside a type declaration:
@@ -202,7 +204,9 @@
     it can be used both as decorator and as a normal function.
     """
 
-    field = strawberry_field(description=description, is_subscription=is_subscription)
+    field = strawberry_field(
+        name=name, description=description, is_subscription=is_subscription
+    )
 
     # when calling this with parens we are going to return a strawberry_field
     # instance, so it can be used as both decorator and function.
diff --git a/strawberry/type.py b/strawberry/type.py
--- a/strawberry/type.py
+++ b/strawberry/type.py
@@ -12,6 +12,7 @@
 from graphql.utilities.schema_printer import print_type
 
 from .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT, IS_STRAWBERRY_INTERFACE
+from .field import strawberry_field
 from .type_converter import REGISTRY, get_graphql_type_for_annotation
 from .utils.str_converters import to_camel_case
 
@@ -26,6 +27,10 @@
         if getattr(field_resolver, IS_STRAWBERRY_FIELD, False):
             return field_resolver(obj, info)
 
+        elif field_resolver.__class__ is strawberry_field:
+            # TODO: support default values
+            return None
+
         return field_resolver
 
     return _resolver
@@ -38,10 +43,12 @@
     fields = {}
 
     for key, annotation in annotations.items():
-        field_name = to_camel_case(key)
         class_field = getattr(cls, key, None)
 
         description = getattr(class_field, "description", None)
+        name = getattr(class_field, "name", None)
+
+        field_name = name or to_camel_case(key)
 
         fields[field_name] = FieldClass(
             get_graphql_type_for_annotation(annotation, key),
@@ -64,13 +71,16 @@
     def _get_fields():
         fields = _convert_annotations_fields(cls, is_input=is_input)
 
-        fields.update(
-            {
-                to_camel_case(key): value.field
-                for key, value in cls.__dict__.items()
-                if getattr(value, IS_STRAWBERRY_FIELD, False)
-            }
-        )
+        strawberry_fields = {
+            key: value
+            for key, value in cls.__dict__.items()
+            if getattr(value, IS_STRAWBERRY_FIELD, False)
+        }
+
+        for key, value in strawberry_fields.items():
+            name = getattr(value, "name", None) or to_camel_case(key)
+
+            fields[name] = value.field
 
         return fields

{"golden_diff": "diff --git a/strawberry/field.py b/strawberry/field.py\n--- a/strawberry/field.py\n+++ b/strawberry/field.py\n@@ -30,9 +30,10 @@\n     >>>         return TypeB()\n     \"\"\"\n \n-    def __init__(self, obj, is_subscription, **kwargs):\n+    def __init__(self, obj, is_subscription, name=None, **kwargs):\n         self._wrapped_obj = obj\n         self.is_subscription = is_subscription\n+        self.name = name\n         self.kwargs = kwargs\n \n         if callable(self._wrapped_obj):\n@@ -106,6 +107,7 @@\n         self.field = dataclasses.field()\n         self.is_subscription = is_subscription\n         self.description = kwargs.get(\"description\", None)\n+        self.name = kwargs.pop(\"name\", None)\n         self.kwargs = kwargs\n \n     def __call__(self, wrap):\n@@ -113,7 +115,7 @@\n \n         self.kwargs[\"description\"] = self.description or wrap.__doc__\n \n-        return LazyFieldWrapper(wrap, self.is_subscription, **self.kwargs)\n+        return LazyFieldWrapper(wrap, self.is_subscription, self.name, **self.kwargs)\n \n \n def convert_args(args, annotations):\n@@ -186,7 +188,7 @@\n     return GraphQLField(field_type, args=arguments, **kwargs)\n \n \n-def field(wrap=None, *, is_subscription=False, description=None):\n+def field(wrap=None, *, is_subscription=False, name=None, description=None):\n     \"\"\"Annotates a method or property as a GraphQL field.\n \n     This is normally used inside a type declaration:\n@@ -202,7 +204,9 @@\n     it can be used both as decorator and as a normal function.\n     \"\"\"\n \n-    field = strawberry_field(description=description, is_subscription=is_subscription)\n+    field = strawberry_field(\n+        name=name, description=description, is_subscription=is_subscription\n+    )\n \n     # when calling this with parens we are going to return a strawberry_field\n     # instance, so it can be used as both decorator and function.\ndiff --git a/strawberry/type.py b/strawberry/type.py\n--- a/strawberry/type.py\n+++ b/strawberry/type.py\n@@ -12,6 +12,7 @@\n from graphql.utilities.schema_printer import print_type\n \n from .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT, IS_STRAWBERRY_INTERFACE\n+from .field import strawberry_field\n from .type_converter import REGISTRY, get_graphql_type_for_annotation\n from .utils.str_converters import to_camel_case\n \n@@ -26,6 +27,10 @@\n         if getattr(field_resolver, IS_STRAWBERRY_FIELD, False):\n             return field_resolver(obj, info)\n \n+        elif field_resolver.__class__ is strawberry_field:\n+            # TODO: support default values\n+            return None\n+\n         return field_resolver\n \n     return _resolver\n@@ -38,10 +43,12 @@\n     fields = {}\n \n     for key, annotation in annotations.items():\n-        field_name = to_camel_case(key)\n         class_field = getattr(cls, key, None)\n \n         description = getattr(class_field, \"description\", None)\n+        name = getattr(class_field, \"name\", None)\n+\n+        field_name = name or to_camel_case(key)\n \n         fields[field_name] = FieldClass(\n             get_graphql_type_for_annotation(annotation, key),\n@@ -64,13 +71,16 @@\n     def _get_fields():\n         fields = _convert_annotations_fields(cls, is_input=is_input)\n \n-        fields.update(\n-            {\n-                to_camel_case(key): value.field\n-                for key, value in cls.__dict__.items()\n-                if getattr(value, IS_STRAWBERRY_FIELD, False)\n-            }\n-        )\n+        strawberry_fields = {\n+            key: value\n+            for key, value in cls.__dict__.items()\n+            if getattr(value, IS_STRAWBERRY_FIELD, False)\n+        }\n+\n+        for key, value in strawberry_fields.items():\n+            name = getattr(value, \"name\", None) or to_camel_case(key)\n+\n+            fields[name] = value.field\n \n         return fields\n", "issue": "Add support for renaming fields\nWe should be able to specify a custom name for a field when needed, like this:\r\n\r\n```python\r\n@strawberry.type\r\nclass Query:\r\n    example_field: str = strawberry.field(name=\"example\")\r\n```\n", "before_files": [{"content": "import typing\n\nimport dataclasses\nfrom graphql import GraphQLField\n\nfrom .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT\nfrom .exceptions import MissingArgumentsAnnotationsError, MissingReturnAnnotationError\nfrom .type_converter import REGISTRY, get_graphql_type_for_annotation\nfrom .utils.dict_to_type import dict_to_type\nfrom .utils.inspect import get_func_args\nfrom .utils.lazy_property import lazy_property\nfrom .utils.str_converters import to_camel_case, to_snake_case\nfrom .utils.typing import (\n    get_list_annotation,\n    get_optional_annotation,\n    is_list,\n    is_optional,\n)\n\n\nclass LazyFieldWrapper:\n    \"\"\"A lazy wrapper for a strawberry field.\n    This allows to use cyclic dependencies in a strawberry fields:\n\n    >>> @strawberry.type\n    >>> class TypeA:\n    >>>     @strawberry.field\n    >>>     def type_b(self, info) -> \"TypeB\":\n    >>>         from .type_b import TypeB\n    >>>         return TypeB()\n    \"\"\"\n\n    def __init__(self, obj, is_subscription, **kwargs):\n        self._wrapped_obj = obj\n        self.is_subscription = is_subscription\n        self.kwargs = kwargs\n\n        if callable(self._wrapped_obj):\n            self._check_has_annotations()\n\n    def _check_has_annotations(self):\n        # using annotations without passing from typing.get_type_hints\n        # as we don't the actually types for the annotations\n        annotations = self._wrapped_obj.__annotations__\n        name = self._wrapped_obj.__name__\n\n        if \"return\" not in annotations:\n            raise MissingReturnAnnotationError(name)\n\n        function_arguments = set(get_func_args(self._wrapped_obj)) - {\"self\", \"info\"}\n\n        arguments_annotations = {\n            key: value\n            for key, value in annotations.items()\n            if key not in [\"info\", \"return\"]\n        }\n\n        annotated_function_arguments = set(arguments_annotations.keys())\n        arguments_missing_annotations = (\n            function_arguments - annotated_function_arguments\n        )\n\n        if len(arguments_missing_annotations) > 0:\n            raise MissingArgumentsAnnotationsError(name, arguments_missing_annotations)\n\n    def __getattr__(self, attr):\n        if attr in self.__dict__:\n            return getattr(self, attr)\n\n        return getattr(self._wrapped_obj, attr)\n\n    def __call__(self, *args, **kwargs):\n        return self._wrapped_obj(self, *args, **kwargs)\n\n    @lazy_property\n    def field(self):\n        return _get_field(\n            self._wrapped_obj, is_subscription=self.is_subscription, **self.kwargs\n        )\n\n\nclass strawberry_field:\n    \"\"\"A small wrapper for a field in strawberry.\n\n    You shouldn't be using this directly as this is used internally\n    when using `strawberry.field`.\n\n    This allows to use the following two syntaxes when using the type\n    decorator:\n\n    >>> class X:\n    >>>     field_abc: str = strawberry.field(description=\"ABC\")\n\n    >>> class X:\n    >>>     @strawberry.field(description=\"ABC\")\n    >>>     def field_a(self, info) -> str:\n    >>>         return \"abc\"\n\n    When calling this class as strawberry_field it creates a field\n    that stores metadata (such as field description). In addition\n    to that it also acts as decorator when called as a function,\n    allowing us to us both syntaxes.\n    \"\"\"\n\n    def __init__(self, *, is_subscription=False, **kwargs):\n        self.field = dataclasses.field()\n        self.is_subscription = is_subscription\n        self.description = kwargs.get(\"description\", None)\n        self.kwargs = kwargs\n\n    def __call__(self, wrap):\n        setattr(wrap, IS_STRAWBERRY_FIELD, True)\n\n        self.kwargs[\"description\"] = self.description or wrap.__doc__\n\n        return LazyFieldWrapper(wrap, self.is_subscription, **self.kwargs)\n\n\ndef convert_args(args, annotations):\n    \"\"\"Converts a nested dictionary to a dictionary of strawberry input types.\"\"\"\n\n    converted_args = {}\n\n    for key, value in args.items():\n        key = to_snake_case(key)\n        annotation = annotations[key]\n\n        # we don't need to check about unions here since they are not\n        # yet supported for arguments.\n        # see https://github.com/graphql/graphql-spec/issues/488\n\n        is_list_of_args = False\n\n        if is_optional(annotation):\n            annotation = get_optional_annotation(annotation)\n\n        if is_list(annotation):\n            annotation = get_list_annotation(annotation)\n            is_list_of_args = True\n\n        if getattr(annotation, IS_STRAWBERRY_INPUT, False):\n            if is_list_of_args:\n                converted_args[key] = [dict_to_type(x, annotation) for x in value]\n            else:\n                converted_args[key] = dict_to_type(value, annotation)\n        else:\n            converted_args[key] = value\n\n    return converted_args\n\n\ndef _get_field(wrap, *, is_subscription=False, **kwargs):\n    annotations = typing.get_type_hints(wrap, None, REGISTRY)\n\n    name = wrap.__name__\n\n    field_type = get_graphql_type_for_annotation(annotations[\"return\"], name)\n\n    arguments_annotations = {\n        key: value\n        for key, value in annotations.items()\n        if key not in [\"info\", \"return\"]\n    }\n\n    arguments = {\n        to_camel_case(name): get_graphql_type_for_annotation(annotation, name)\n        for name, annotation in arguments_annotations.items()\n    }\n\n    def resolver(source, info, **args):\n        args = convert_args(args, arguments_annotations)\n\n        return wrap(source, info, **args)\n\n    if is_subscription:\n\n        def _resolve(event, info):\n            return event\n\n        kwargs.update({\"subscribe\": resolver, \"resolve\": _resolve})\n    else:\n        kwargs.update({\"resolve\": resolver})\n\n    kwargs[\"description\"] = kwargs.get(\"description\", wrap.__doc__)\n\n    return GraphQLField(field_type, args=arguments, **kwargs)\n\n\ndef field(wrap=None, *, is_subscription=False, description=None):\n    \"\"\"Annotates a method or property as a GraphQL field.\n\n    This is normally used inside a type declaration:\n\n    >>> @strawberry.type:\n    >>> class X:\n    >>>     field_abc: str = strawberry.field(description=\"ABC\")\n\n    >>>     @strawberry.field(description=\"ABC\")\n    >>>     def field_with_resolver(self, info) -> str:\n    >>>         return \"abc\"\n\n    it can be used both as decorator and as a normal function.\n    \"\"\"\n\n    field = strawberry_field(description=description, is_subscription=is_subscription)\n\n    # when calling this with parens we are going to return a strawberry_field\n    # instance, so it can be used as both decorator and function.\n\n    if wrap is None:\n        return field\n\n    # otherwise we run the decorator directly,\n    # when called as @strawberry.field, without parens.\n\n    return field(wrap)\n", "path": "strawberry/field.py"}, {"content": "import typing\nfrom functools import partial\n\nfrom dataclasses import dataclass\nfrom graphql import (\n    GraphQLField,\n    GraphQLInputField,\n    GraphQLInputObjectType,\n    GraphQLInterfaceType,\n    GraphQLObjectType,\n)\nfrom graphql.utilities.schema_printer import print_type\n\nfrom .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT, IS_STRAWBERRY_INTERFACE\nfrom .type_converter import REGISTRY, get_graphql_type_for_annotation\nfrom .utils.str_converters import to_camel_case\n\n\ndef _get_resolver(cls, field_name):\n    def _resolver(obj, info):\n        # TODO: can we make this nicer?\n        # does it work in all the cases?\n\n        field_resolver = getattr(cls(**(obj.__dict__ if obj else {})), field_name)\n\n        if getattr(field_resolver, IS_STRAWBERRY_FIELD, False):\n            return field_resolver(obj, info)\n\n        return field_resolver\n\n    return _resolver\n\n\ndef _convert_annotations_fields(cls, *, is_input=False):\n    FieldClass = GraphQLInputField if is_input else GraphQLField\n    annotations = typing.get_type_hints(cls, None, REGISTRY)\n\n    fields = {}\n\n    for key, annotation in annotations.items():\n        field_name = to_camel_case(key)\n        class_field = getattr(cls, key, None)\n\n        description = getattr(class_field, \"description\", None)\n\n        fields[field_name] = FieldClass(\n            get_graphql_type_for_annotation(annotation, key),\n            description=description,\n            **({} if is_input else {\"resolve\": _get_resolver(cls, key)})\n        )\n\n    return fields\n\n\ndef _process_type(cls, *, is_input=False, is_interface=False, description=None):\n    name = cls.__name__\n    REGISTRY[name] = cls\n\n    def repr_(self):\n        return print_type(self.field)\n\n    setattr(cls, \"__repr__\", repr_)\n\n    def _get_fields():\n        fields = _convert_annotations_fields(cls, is_input=is_input)\n\n        fields.update(\n            {\n                to_camel_case(key): value.field\n                for key, value in cls.__dict__.items()\n                if getattr(value, IS_STRAWBERRY_FIELD, False)\n            }\n        )\n\n        return fields\n\n    if is_input:\n        setattr(cls, IS_STRAWBERRY_INPUT, True)\n    elif is_interface:\n        setattr(cls, IS_STRAWBERRY_INTERFACE, True)\n\n    extra_kwargs = {\"description\": description or cls.__doc__}\n\n    if is_input:\n        TypeClass = GraphQLInputObjectType\n    elif is_interface:\n        TypeClass = GraphQLInterfaceType\n    else:\n        TypeClass = GraphQLObjectType\n\n        extra_kwargs[\"interfaces\"] = [\n            klass.field\n            for klass in cls.__bases__\n            if hasattr(klass, IS_STRAWBERRY_INTERFACE)\n        ]\n\n    cls.field = TypeClass(name, lambda: _get_fields(), **extra_kwargs)\n\n    return dataclass(cls, repr=False)\n\n\ndef type(cls=None, *, is_input=False, is_interface=False, description=None):\n    \"\"\"Annotates a class as a GraphQL type.\n\n    Example usage:\n\n    >>> @strawberry.type:\n    >>> class X:\n    >>>     field_abc: str = \"ABC\"\n    \"\"\"\n\n    def wrap(cls):\n        return _process_type(\n            cls, is_input=is_input, is_interface=is_interface, description=description\n        )\n\n    if cls is None:\n        return wrap\n\n    return wrap(cls)\n\n\ninput = partial(type, is_input=True)\ninterface = partial(type, is_interface=True)\n", "path": "strawberry/type.py"}], "after_files": [{"content": "import typing\n\nimport dataclasses\nfrom graphql import GraphQLField\n\nfrom .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT\nfrom .exceptions import MissingArgumentsAnnotationsError, MissingReturnAnnotationError\nfrom .type_converter import REGISTRY, get_graphql_type_for_annotation\nfrom .utils.dict_to_type import dict_to_type\nfrom .utils.inspect import get_func_args\nfrom .utils.lazy_property import lazy_property\nfrom .utils.str_converters import to_camel_case, to_snake_case\nfrom .utils.typing import (\n    get_list_annotation,\n    get_optional_annotation,\n    is_list,\n    is_optional,\n)\n\n\nclass LazyFieldWrapper:\n    \"\"\"A lazy wrapper for a strawberry field.\n    This allows to use cyclic dependencies in a strawberry fields:\n\n    >>> @strawberry.type\n    >>> class TypeA:\n    >>>     @strawberry.field\n    >>>     def type_b(self, info) -> \"TypeB\":\n    >>>         from .type_b import TypeB\n    >>>         return TypeB()\n    \"\"\"\n\n    def __init__(self, obj, is_subscription, name=None, **kwargs):\n        self._wrapped_obj = obj\n        self.is_subscription = is_subscription\n        self.name = name\n        self.kwargs = kwargs\n\n        if callable(self._wrapped_obj):\n            self._check_has_annotations()\n\n    def _check_has_annotations(self):\n        # using annotations without passing from typing.get_type_hints\n        # as we don't the actually types for the annotations\n        annotations = self._wrapped_obj.__annotations__\n        name = self._wrapped_obj.__name__\n\n        if \"return\" not in annotations:\n            raise MissingReturnAnnotationError(name)\n\n        function_arguments = set(get_func_args(self._wrapped_obj)) - {\"self\", \"info\"}\n\n        arguments_annotations = {\n            key: value\n            for key, value in annotations.items()\n            if key not in [\"info\", \"return\"]\n        }\n\n        annotated_function_arguments = set(arguments_annotations.keys())\n        arguments_missing_annotations = (\n            function_arguments - annotated_function_arguments\n        )\n\n        if len(arguments_missing_annotations) > 0:\n            raise MissingArgumentsAnnotationsError(name, arguments_missing_annotations)\n\n    def __getattr__(self, attr):\n        if attr in self.__dict__:\n            return getattr(self, attr)\n\n        return getattr(self._wrapped_obj, attr)\n\n    def __call__(self, *args, **kwargs):\n        return self._wrapped_obj(self, *args, **kwargs)\n\n    @lazy_property\n    def field(self):\n        return _get_field(\n            self._wrapped_obj, is_subscription=self.is_subscription, **self.kwargs\n        )\n\n\nclass strawberry_field:\n    \"\"\"A small wrapper for a field in strawberry.\n\n    You shouldn't be using this directly as this is used internally\n    when using `strawberry.field`.\n\n    This allows to use the following two syntaxes when using the type\n    decorator:\n\n    >>> class X:\n    >>>     field_abc: str = strawberry.field(description=\"ABC\")\n\n    >>> class X:\n    >>>     @strawberry.field(description=\"ABC\")\n    >>>     def field_a(self, info) -> str:\n    >>>         return \"abc\"\n\n    When calling this class as strawberry_field it creates a field\n    that stores metadata (such as field description). In addition\n    to that it also acts as decorator when called as a function,\n    allowing us to us both syntaxes.\n    \"\"\"\n\n    def __init__(self, *, is_subscription=False, **kwargs):\n        self.field = dataclasses.field()\n        self.is_subscription = is_subscription\n        self.description = kwargs.get(\"description\", None)\n        self.name = kwargs.pop(\"name\", None)\n        self.kwargs = kwargs\n\n    def __call__(self, wrap):\n        setattr(wrap, IS_STRAWBERRY_FIELD, True)\n\n        self.kwargs[\"description\"] = self.description or wrap.__doc__\n\n        return LazyFieldWrapper(wrap, self.is_subscription, self.name, **self.kwargs)\n\n\ndef convert_args(args, annotations):\n    \"\"\"Converts a nested dictionary to a dictionary of strawberry input types.\"\"\"\n\n    converted_args = {}\n\n    for key, value in args.items():\n        key = to_snake_case(key)\n        annotation = annotations[key]\n\n        # we don't need to check about unions here since they are not\n        # yet supported for arguments.\n        # see https://github.com/graphql/graphql-spec/issues/488\n\n        is_list_of_args = False\n\n        if is_optional(annotation):\n            annotation = get_optional_annotation(annotation)\n\n        if is_list(annotation):\n            annotation = get_list_annotation(annotation)\n            is_list_of_args = True\n\n        if getattr(annotation, IS_STRAWBERRY_INPUT, False):\n            if is_list_of_args:\n                converted_args[key] = [dict_to_type(x, annotation) for x in value]\n            else:\n                converted_args[key] = dict_to_type(value, annotation)\n        else:\n            converted_args[key] = value\n\n    return converted_args\n\n\ndef _get_field(wrap, *, is_subscription=False, **kwargs):\n    annotations = typing.get_type_hints(wrap, None, REGISTRY)\n\n    name = wrap.__name__\n\n    field_type = get_graphql_type_for_annotation(annotations[\"return\"], name)\n\n    arguments_annotations = {\n        key: value\n        for key, value in annotations.items()\n        if key not in [\"info\", \"return\"]\n    }\n\n    arguments = {\n        to_camel_case(name): get_graphql_type_for_annotation(annotation, name)\n        for name, annotation in arguments_annotations.items()\n    }\n\n    def resolver(source, info, **args):\n        args = convert_args(args, arguments_annotations)\n\n        return wrap(source, info, **args)\n\n    if is_subscription:\n\n        def _resolve(event, info):\n            return event\n\n        kwargs.update({\"subscribe\": resolver, \"resolve\": _resolve})\n    else:\n        kwargs.update({\"resolve\": resolver})\n\n    kwargs[\"description\"] = kwargs.get(\"description\", wrap.__doc__)\n\n    return GraphQLField(field_type, args=arguments, **kwargs)\n\n\ndef field(wrap=None, *, is_subscription=False, name=None, description=None):\n    \"\"\"Annotates a method or property as a GraphQL field.\n\n    This is normally used inside a type declaration:\n\n    >>> @strawberry.type:\n    >>> class X:\n    >>>     field_abc: str = strawberry.field(description=\"ABC\")\n\n    >>>     @strawberry.field(description=\"ABC\")\n    >>>     def field_with_resolver(self, info) -> str:\n    >>>         return \"abc\"\n\n    it can be used both as decorator and as a normal function.\n    \"\"\"\n\n    field = strawberry_field(\n        name=name, description=description, is_subscription=is_subscription\n    )\n\n    # when calling this with parens we are going to return a strawberry_field\n    # instance, so it can be used as both decorator and function.\n\n    if wrap is None:\n        return field\n\n    # otherwise we run the decorator directly,\n    # when called as @strawberry.field, without parens.\n\n    return field(wrap)\n", "path": "strawberry/field.py"}, {"content": "import typing\nfrom functools import partial\n\nfrom dataclasses import dataclass\nfrom graphql import (\n    GraphQLField,\n    GraphQLInputField,\n    GraphQLInputObjectType,\n    GraphQLInterfaceType,\n    GraphQLObjectType,\n)\nfrom graphql.utilities.schema_printer import print_type\n\nfrom .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT, IS_STRAWBERRY_INTERFACE\nfrom .field import strawberry_field\nfrom .type_converter import REGISTRY, get_graphql_type_for_annotation\nfrom .utils.str_converters import to_camel_case\n\n\ndef _get_resolver(cls, field_name):\n    def _resolver(obj, info):\n        # TODO: can we make this nicer?\n        # does it work in all the cases?\n\n        field_resolver = getattr(cls(**(obj.__dict__ if obj else {})), field_name)\n\n        if getattr(field_resolver, IS_STRAWBERRY_FIELD, False):\n            return field_resolver(obj, info)\n\n        elif field_resolver.__class__ is strawberry_field:\n            # TODO: support default values\n            return None\n\n        return field_resolver\n\n    return _resolver\n\n\ndef _convert_annotations_fields(cls, *, is_input=False):\n    FieldClass = GraphQLInputField if is_input else GraphQLField\n    annotations = typing.get_type_hints(cls, None, REGISTRY)\n\n    fields = {}\n\n    for key, annotation in annotations.items():\n        class_field = getattr(cls, key, None)\n\n        description = getattr(class_field, \"description\", None)\n        name = getattr(class_field, \"name\", None)\n\n        field_name = name or to_camel_case(key)\n\n        fields[field_name] = FieldClass(\n            get_graphql_type_for_annotation(annotation, key),\n            description=description,\n            **({} if is_input else {\"resolve\": _get_resolver(cls, key)})\n        )\n\n    return fields\n\n\ndef _process_type(cls, *, is_input=False, is_interface=False, description=None):\n    name = cls.__name__\n    REGISTRY[name] = cls\n\n    def repr_(self):\n        return print_type(self.field)\n\n    setattr(cls, \"__repr__\", repr_)\n\n    def _get_fields():\n        fields = _convert_annotations_fields(cls, is_input=is_input)\n\n        strawberry_fields = {\n            key: value\n            for key, value in cls.__dict__.items()\n            if getattr(value, IS_STRAWBERRY_FIELD, False)\n        }\n\n        for key, value in strawberry_fields.items():\n            name = getattr(value, \"name\", None) or to_camel_case(key)\n\n            fields[name] = value.field\n\n        return fields\n\n    if is_input:\n        setattr(cls, IS_STRAWBERRY_INPUT, True)\n    elif is_interface:\n        setattr(cls, IS_STRAWBERRY_INTERFACE, True)\n\n    extra_kwargs = {\"description\": description or cls.__doc__}\n\n    if is_input:\n        TypeClass = GraphQLInputObjectType\n    elif is_interface:\n        TypeClass = GraphQLInterfaceType\n    else:\n        TypeClass = GraphQLObjectType\n\n        extra_kwargs[\"interfaces\"] = [\n            klass.field\n            for klass in cls.__bases__\n            if hasattr(klass, IS_STRAWBERRY_INTERFACE)\n        ]\n\n    cls.field = TypeClass(name, lambda: _get_fields(), **extra_kwargs)\n\n    return dataclass(cls, repr=False)\n\n\ndef type(cls=None, *, is_input=False, is_interface=False, description=None):\n    \"\"\"Annotates a class as a GraphQL type.\n\n    Example usage:\n\n    >>> @strawberry.type:\n    >>> class X:\n    >>>     field_abc: str = \"ABC\"\n    \"\"\"\n\n    def wrap(cls):\n        return _process_type(\n            cls, is_input=is_input, is_interface=is_interface, description=description\n        )\n\n    if cls is None:\n        return wrap\n\n    return wrap(cls)\n\n\ninput = partial(type, is_input=True)\ninterface = partial(type, is_interface=True)\n", "path": "strawberry/type.py"}]}