Skip to content

Add interfaces meta argument on Mutations #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions graphene/types/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
from .field import Field
from .objecttype import ObjectType, ObjectTypeOptions
from .utils import yank_fields_from_attrs
from .interface import Interface

# For static type checking with Mypy
MYPY = False
if MYPY:
from .argument import Argument # NOQA
from typing import Dict, Type, Callable # NOQA
from typing import Dict, Type, Callable, Iterable # NOQA


class MutationOptions(ObjectTypeOptions):
arguments = None # type: Dict[str, Argument]
output = None # type: Type[ObjectType]
resolver = None # type: Callable
interfaces = () # type: Iterable[Type[Interface]]


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

@classmethod
def __init_subclass_with_meta__(
cls, resolver=None, output=None, arguments=None, _meta=None, **options
cls,
interfaces=(),
resolver=None,
output=None,
arguments=None,
_meta=None,
**options
):
if not _meta:
_meta = MutationOptions(cls)

output = output or getattr(cls, "Output", None)
fields = {}

for interface in interfaces:
assert issubclass(interface, Interface), (
'All interfaces of {} must be a subclass of Interface. Received "{}".'
).format(cls.__name__, interface)
fields.update(interface._meta.fields)

if not output:
# If output is defined, we don't need to get the fields
fields = OrderedDict()
Expand Down Expand Up @@ -110,6 +124,7 @@ def __init_subclass_with_meta__(
else:
_meta.fields = fields

_meta.interfaces = interfaces
_meta.output = output
_meta.resolver = resolver
_meta.arguments = arguments
Expand Down
3 changes: 2 additions & 1 deletion graphene/types/objecttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def __init_subclass_with_meta__(
else:
_meta.fields = fields

_meta.interfaces = interfaces
if not _meta.interfaces:
_meta.interfaces = interfaces
_meta.possible_types = possible_types
_meta.default_resolver = default_resolver

Expand Down
7 changes: 7 additions & 0 deletions graphene/types/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
from ..scalars import String
from ..schema import Schema
from ..structures import NonNull
from ..interface import Interface


class MyType(Interface):
pass


def test_generate_mutation_no_args():
Expand All @@ -28,12 +33,14 @@ class MyMutation(Mutation):
class Meta:
name = "MyOtherMutation"
description = "Documentation"
interfaces = (MyType,)

def mutate(self, info, **args):
return args

assert MyMutation._meta.name == "MyOtherMutation"
assert MyMutation._meta.description == "Documentation"
assert MyMutation._meta.interfaces == (MyType,)
resolved = MyMutation.Field().resolver(None, None, name="Peter")
assert resolved == {"name": "Peter"}

Expand Down