nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: strawberry-graphql/strawberry
Pull request #45 (2019-04-13 10:45:12)

Allow strawberry.field to be used as a function


Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 676606f4..b1e77df2 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -3,6 +3,7 @@ repos:
     rev: stable
     hooks:
       - id: black
+        language_version: python3.6
 
   - repo: https://github.com/pre-commit/pre-commit-hooks
     rev: v1.2.3
diff --git a/strawberry/field.py b/strawberry/field.py
index 8107da34..3d9beb14 100644
--- a/strawberry/field.py
+++ b/strawberry/field.py
@@ -1,5 +1,6 @@
 from typing import get_type_hints
 
+import dataclasses
 from graphql import GraphQLField
 
 from .constants import IS_STRAWBERRY_FIELD, IS_STRAWBERRY_INPUT
@@ -49,7 +50,13 @@ def convert_args(args, annotations):
     return converted_args
 
 
-def field(wrap, *, is_subscription=False):
+def field(wrap=None, *, is_subscription=False):
+    if not wrap:
+        # when used as a function we return a dataclass field
+        # in the future we can use the `metadata` argument of `field` to pass
+        # extra arguments like the description
+        return dataclasses.field()
+
     setattr(wrap, IS_STRAWBERRY_FIELD, True)
     annotations = get_type_hints(wrap)
 
diff --git a/strawberry/type.py b/strawberry/type.py
index b8a39661..24c0c417 100644
--- a/strawberry/type.py
+++ b/strawberry/type.py
@@ -1,6 +1,7 @@
 import typing
 from functools import partial
 
+import dataclasses
 from dataclasses import dataclass
 from graphql import (
     GraphQLField,
@@ -46,11 +47,13 @@ def type(cls, *, is_input=False):
             FieldClass = GraphQLInputField if is_input else GraphQLField
 
             fields = {
-                to_camel_case(key): FieldClass(
-                    get_graphql_type_for_annotation(value, key),
-                    **({} if is_input else {"resolve": _get_resolver(cls, key)})
+                to_camel_case(field.name): FieldClass(
+                    get_graphql_type_for_annotation(
+                        annotations[field.name], field.name
+                    ),
+                    **({} if is_input else {"resolve": _get_resolver(cls, field.name)})
                 )
-                for key, value in annotations.items()
+                for field in dataclasses.fields(cls)
             }
 
             fields.update(


Test patch:
diff --git a/tests/test_field.py b/tests/test_field.py
index 017d957d..d62ee2d4 100644
--- a/tests/test_field.py
+++ b/tests/test_field.py
@@ -1,5 +1,6 @@
 import pytest
 
+import dataclasses
 import strawberry
 from graphql import GraphQLField, GraphQLNonNull
 from strawberry.exceptions import (
@@ -8,6 +9,13 @@ from strawberry.exceptions import (
 )
 
 
+def test_field_as_function():
+    field: str = strawberry.field()
+
+    assert type(field) == dataclasses.Field
+    assert field.metadata == {}
+
+
 def test_field_arguments():
     @strawberry.field
     def hello(self, info, id: int) -> str: