Skip to content

Commit c96bd68

Browse files
Nta1ejkimbo
authored andcommitted
Add interfaces meta argument on Mutations (#1023)
1 parent 6e40589 commit c96bd68

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

graphene/types/mutation.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
from .field import Field
77
from .objecttype import ObjectType, ObjectTypeOptions
88
from .utils import yank_fields_from_attrs
9+
from .interface import Interface
910

1011
# For static type checking with Mypy
1112
MYPY = False
1213
if MYPY:
1314
from .argument import Argument # NOQA
14-
from typing import Dict, Type, Callable # NOQA
15+
from typing import Dict, Type, Callable, Iterable # NOQA
1516

1617

1718
class MutationOptions(ObjectTypeOptions):
1819
arguments = None # type: Dict[str, Argument]
1920
output = None # type: Type[ObjectType]
2021
resolver = None # type: Callable
22+
interfaces = () # type: Iterable[Type[Interface]]
2123

2224

2325
class Mutation(ObjectType):
@@ -58,22 +60,34 @@ class Mutation(ObjectType):
5860
name.
5961
description (str): Description of the GraphQL type in the schema. Defaults to class
6062
docstring.
61-
interfaces (Iterable[graphene.Interface]): NOT IMPLEMENTED (use ``output`` to define a
62-
payload implementing interfaces). GraphQL interfaces to extend with the payload
63+
interfaces (Iterable[graphene.Interface]): GraphQL interfaces to extend with the payload
6364
object. All fields from interface will be included in this object's schema.
6465
fields (Dict[str, graphene.Field]): Dictionary of field name to Field. Not recommended to
6566
use (prefer class attributes or ``Meta.output``).
6667
"""
6768

6869
@classmethod
6970
def __init_subclass_with_meta__(
70-
cls, resolver=None, output=None, arguments=None, _meta=None, **options
71+
cls,
72+
interfaces=(),
73+
resolver=None,
74+
output=None,
75+
arguments=None,
76+
_meta=None,
77+
**options
7178
):
7279
if not _meta:
7380
_meta = MutationOptions(cls)
7481

7582
output = output or getattr(cls, "Output", None)
7683
fields = {}
84+
85+
for interface in interfaces:
86+
assert issubclass(interface, Interface), (
87+
'All interfaces of {} must be a subclass of Interface. Received "{}".'
88+
).format(cls.__name__, interface)
89+
fields.update(interface._meta.fields)
90+
7791
if not output:
7892
# If output is defined, we don't need to get the fields
7993
fields = OrderedDict()
@@ -110,6 +124,7 @@ def __init_subclass_with_meta__(
110124
else:
111125
_meta.fields = fields
112126

127+
_meta.interfaces = interfaces
113128
_meta.output = output
114129
_meta.resolver = resolver
115130
_meta.arguments = arguments

graphene/types/objecttype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def __init_subclass_with_meta__(
121121
else:
122122
_meta.fields = fields
123123

124-
_meta.interfaces = interfaces
124+
if not _meta.interfaces:
125+
_meta.interfaces = interfaces
125126
_meta.possible_types = possible_types
126127
_meta.default_resolver = default_resolver
127128

graphene/types/tests/test_mutation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
from ..scalars import String
88
from ..schema import Schema
99
from ..structures import NonNull
10+
from ..interface import Interface
11+
12+
13+
class MyType(Interface):
14+
pass
1015

1116

1217
def test_generate_mutation_no_args():
@@ -28,12 +33,14 @@ class MyMutation(Mutation):
2833
class Meta:
2934
name = "MyOtherMutation"
3035
description = "Documentation"
36+
interfaces = (MyType,)
3137

3238
def mutate(self, info, **args):
3339
return args
3440

3541
assert MyMutation._meta.name == "MyOtherMutation"
3642
assert MyMutation._meta.description == "Documentation"
43+
assert MyMutation._meta.interfaces == (MyType,)
3744
resolved = MyMutation.Field().resolver(None, None, name="Peter")
3845
assert resolved == {"name": "Peter"}
3946

0 commit comments

Comments
 (0)