Repository: mirumee/ariadne

Issue:
Raise ValueError when `field` or `source` decorator was called incorrectly
Currently there's no error when the developer forgets to follow the `field` or `source` decorator with `("name")`, tricking them into thinking that decorated function has been registered while in fact it wasn't.

We could update implementation for those functions to raise ValueError when `name` attr is not `str`.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=h1) Report
> Merging [#153](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=desc) into [master](https://codecov.io/gh/mirumee/ariadne/commit/aa3d994c27a740306edace41ce66842e6a8a02a3?src=pr&el=desc) will **increase** coverage by `0.1%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/mirumee/ariadne/pull/153/graphs/tree.svg?width=650&token=8GSRgVxHd7&height=150&src=pr)](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #153     +/-   ##
=========================================
+ Coverage   97.72%   97.82%   +0.1%     
=========================================
  Files          60       60             
  Lines        2018     2025      +7     
=========================================
+ Hits         1972     1981      +9     
+ Misses         46       44      -2
```


| [Impacted Files](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [ariadne/subscriptions.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS9zdWJzY3JpcHRpb25zLnB5) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_subscriptions.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9zdWJzY3JpcHRpb25zLnB5) | `100% <100%> (ø)` | :arrow_up: |
| [ariadne/objects.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS9vYmplY3RzLnB5) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_objects.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9vYmplY3RzLnB5) | `100% <100%> (ø)` | :arrow_up: |
| [ariadne/wsgi.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS93c2dpLnB5) | `100% <0%> (ø)` | :arrow_up: |
| [ariadne/scalars.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS9zY2FsYXJzLnB5) | `100% <0%> (ø)` | :arrow_up: |
| [ariadne/interfaces.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS9pbnRlcmZhY2VzLnB5) | `100% <0%> (ø)` | :arrow_up: |
| [ariadne/enums.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS9lbnVtcy5weQ==) | `100% <0%> (ø)` | :arrow_up: |
| [ariadne/types.py](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree#diff-YXJpYWRuZS90eXBlcy5weQ==) | `100% <0%> (ø)` | :arrow_up: |
| ... and [3 more](https://codecov.io/gh/mirumee/ariadne/pull/153/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=footer). Last update [aa3d994...bc9fbb3](https://codecov.io/gh/mirumee/ariadne/pull/153?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 140a270..befe10e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
 
 - Updated `graphql-core-next` to 1.0.3 which has feature parity with GraphQL.js 14.2.1 and better type annotations.
 - `ariadne.asgi.GraphQL` is now an ASGI3 application. ASGI3 is now handled by all ASGI servers.
+- `ObjectType.field` and `SubscriptionType.source` decorators now raise ValueError when used without name argument (eg. `@foo.field`).
 - Removed explicit `typing` dependency.
 - Added Flask integration example.
 
diff --git a/ariadne/objects.py b/ariadne/objects.py
index 80e1829..12c869b 100644
--- a/ariadne/objects.py
+++ b/ariadne/objects.py
@@ -14,6 +14,10 @@ class ObjectType(SchemaBindable):
         self._resolvers = {}
 
     def field(self, name: str) -> Callable[[Resolver], Resolver]:
+        if not isinstance(name, str):
+            raise ValueError(
+                'field decorator should be passed a field name: @foo.field("name")'
+            )
         return self.create_register_resolver(name)
 
     def create_register_resolver(self, name: str) -> Callable[[Resolver], Resolver]:
diff --git a/ariadne/subscriptions.py b/ariadne/subscriptions.py
index 8c371ff..8a180e3 100644
--- a/ariadne/subscriptions.py
+++ b/ariadne/subscriptions.py
@@ -14,6 +14,10 @@ class SubscriptionType(ObjectType):
         self._subscribers = {}
 
     def source(self, name: str) -> Callable[[Subscriber], Subscriber]:
+        if not isinstance(name, str):
+            raise ValueError(
+                'source decorator should be passed a field name: @foo.source("name")'
+            )
         return self.create_register_subscriber(name)
 
     def create_register_subscriber(


Test patch:
diff --git a/tests/test_objects.py b/tests/test_objects.py
index 231c5cc..eaf5b3e 100644
--- a/tests/test_objects.py
+++ b/tests/test_objects.py
@@ -46,6 +46,12 @@ def test_field_resolver_can_be_set_using_decorator(schema):
     assert result.data == {"hello": "World"}
 
 
+def test_value_error_is_raised_if_field_decorator_was_used_without_argument():
+    query = ObjectType("Query")
+    with pytest.raises(ValueError):
+        query.field(lambda *_: "World")
+
+
 def test_field_resolver_can_be_set_using_setter(schema):
     query = ObjectType("Query")
     query.set_field("hello", lambda *_: "World")
diff --git a/tests/test_subscriptions.py b/tests/test_subscriptions.py
index aede02b..cec70c7 100644
--- a/tests/test_subscriptions.py
+++ b/tests/test_subscriptions.py
@@ -23,9 +23,9 @@ def test_field_source_can_be_set_using_setter(schema):
     async def source(*_):
         yield "test"  # pragma: no cover
 
-    sub = SubscriptionType()
-    sub.set_source("message", source)
-    sub.bind_to_schema(schema)
+    subscription = SubscriptionType()
+    subscription.set_source("message", source)
+    subscription.bind_to_schema(schema)
     field = schema.type_map.get("Subscription").fields["message"]
     assert field.subscribe is source
 
@@ -34,18 +34,27 @@ def test_field_source_can_be_set_using_decorator(schema):
     async def source(*_):
         yield "test"  # pragma: no cover
 
-    sub = SubscriptionType()
-    sub.source("message")(source)
-    sub.bind_to_schema(schema)
+    subscription = SubscriptionType()
+    subscription.source("message")(source)
+    subscription.bind_to_schema(schema)
     field = schema.type_map.get("Subscription").fields["message"]
     assert field.subscribe is source
 
 
+def test_value_error_is_raised_if_source_decorator_was_used_without_argument():
+    async def source(*_):
+        yield "test"  # pragma: no cover
+
+    subscription = SubscriptionType()
+    with pytest.raises(ValueError):
+        subscription.source(source)
+
+
 def test_attempt_bind_subscription_to_undefined_field_raises_error(schema):
     async def source(*_):
         yield "test"  # pragma: no cover
 
-    sub_map = SubscriptionType()
-    sub_map.set_source("fake", source)
+    subscription = SubscriptionType()
+    subscription.set_source("fake", source)
     with pytest.raises(ValueError):
-        sub_map.bind_to_schema(schema)
+        subscription.bind_to_schema(schema)