Skip to content

Commit 2d4791b

Browse files
committed
feat(interface): input type cannot inherit an interface type
fix #1236
1 parent a97c96f commit 2d4791b

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

strawberry/exceptions.py

+10
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,13 @@ def __init__(self, field_name: str, argument_name: str, argument_type: str):
176176
message = f'Argument "{argument_name}" on field "{field_name}" cannot be of type\
177177
"{argument_type}"'
178178
super().__init__(message)
179+
180+
181+
class InvalidSuperclassInterface(Exception):
182+
def __init__(self, input_name: str, interface_names=List[str]):
183+
interface_names = ", ".join(interface_names)
184+
message = (
185+
f"An Input class {input_name!r} cannot inherit "
186+
f"from an Interface(s) {interface_names!r}"
187+
)
188+
super().__init__(message)

strawberry/object_type.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dataclasses
22
from typing import List, Optional, Type, cast
33

4-
from .exceptions import MissingFieldAnnotationError, MissingReturnAnnotationError
4+
from .exceptions import InvalidSuperclassInterface, MissingFieldAnnotationError, MissingReturnAnnotationError
55
from .field import StrawberryField, field
66
from .types.type_resolver import _get_fields
77
from .types.types import FederationTypeParams, TypeDefinition
@@ -99,6 +99,14 @@ def _process_type(
9999
interfaces = _get_interfaces(cls)
100100
fields = _get_fields(cls)
101101

102+
if is_input and interfaces:
103+
interface_names = [interface.name for interface in interfaces]
104+
105+
raise InvalidSuperclassInterface(
106+
input_name=name,
107+
interface_names=interface_names
108+
)
109+
102110
cls._type_definition = TypeDefinition(
103111
name=name,
104112
is_input=is_input,

tests/schema/test_interface.py

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import strawberry
7+
from strawberry.exceptions import InvalidSuperclassInterface
78

89

910
def test_query_interface():
@@ -161,3 +162,27 @@ def anime(self) -> Anime:
161162

162163
assert not result.errors
163164
assert result.data == {"anime": {"name": "One Piece"}}
165+
166+
167+
def test_input_cannot_inherit_from_interface():
168+
@strawberry.interface
169+
class SomeInterface:
170+
some_arg: str
171+
172+
with pytest.raises(InvalidSuperclassInterface):
173+
174+
@strawberry.input
175+
class SomeInput(SomeInterface): # this should throw an error
176+
another_arg: str
177+
178+
@strawberry.interface
179+
class SomeOtherInterface:
180+
some_other_arg: str
181+
182+
with pytest.raises(InvalidSuperclassInterface):
183+
184+
@strawberry.input
185+
class SomeOtherInput(
186+
SomeInterface, SomeOtherInterface
187+
): # this should throw an error
188+
another_arg: str

0 commit comments

Comments
 (0)