Added Mutation support

This commit is contained in:
Syrus Akbary 2016-06-13 20:26:26 -07:00
parent cf80f87dca
commit 6c7cf55b18
5 changed files with 22 additions and 9 deletions

View File

@ -5,6 +5,7 @@ from .objecttype import ObjectTypeMeta, ObjectType
from .field import Field from .field import Field
from ..utils.props import props from ..utils.props import props
from ..utils.is_base_type import is_base_type
class MutationMeta(ObjectTypeMeta): class MutationMeta(ObjectTypeMeta):
@ -16,15 +17,22 @@ class MutationMeta(ObjectTypeMeta):
assert resolver, 'All mutations must define a mutate method in it' assert resolver, 'All mutations must define a mutate method in it'
return partial(Field, cls, args=field_args, resolver=resolver) return partial(Field, cls, args=field_args, resolver=resolver)
def construct(cls, bases, attrs): def __new__(cls, name, bases, attrs):
super(MutationMeta, cls).construct(bases, attrs) super_new = super(MutationMeta, cls).__new__
if not cls._meta.abstract and cls._construct_field:
Input = attrs.pop('Input', None) # Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
if not is_base_type(bases, MutationMeta):
return type.__new__(cls, name, bases, attrs)
Input = attrs.pop('Input', None)
cls = super_new(cls, name, bases, attrs)
if cls._construct_field:
field_args = props(Input) if Input else {} field_args = props(Input) if Input else {}
cls.Field = cls.construct_field(field_args) cls.Field = cls.construct_field(field_args)
return cls return cls
class Mutation(six.with_metaclass(MutationMeta, ObjectType)): class Mutation(six.with_metaclass(MutationMeta, ObjectType)):
class Meta: pass
abstract = True

View File

@ -124,6 +124,9 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta)):
# GraphQL ObjectType acting as container # GraphQL ObjectType acting as container
args_len = len(args) args_len = len(args)
fields = self._meta.graphql_type.get_fields().values() fields = self._meta.graphql_type.get_fields().values()
for f in fields:
setattr(self, getattr(f, 'attname', f.name), None)
if args_len > len(fields): if args_len > len(fields):
# Daft, but matches old exception sans the err msg. # Daft, but matches old exception sans the err msg.
raise IndexError("Number of args exceeds number of fields") raise IndexError("Number of args exceeds number of fields")

View File

@ -1,6 +1,6 @@
from ..scalars import String from ..scalars import String
from ..field import Field from ..field import Field
from ..objecttype import ObjectType, implements from ..objecttype import ObjectType
from ..interface import Interface from ..interface import Interface
from ..structures import List from ..structures import List
from ..schema import Schema from ..schema import Schema

View File

@ -5,7 +5,7 @@ from ..types.field import Field, InputField
def copy_fields(like, fields, **extra): def copy_fields(like, fields, **extra):
_fields = [] _fields = []
for attname, field in fields.items(): for attname, field in fields.items():
field = like.copy_and_extend(field, attname=attname, **extra) field = like.copy_and_extend(field, attname=getattr(field, 'attname', None) or attname, **extra)
_fields.append(field) _fields.append(field)
return OrderedDict((f.name, f) for f in _fields) return OrderedDict((f.name, f) for f in _fields)

View File

@ -3,17 +3,19 @@ import inspect
def is_graphene_type(_type): def is_graphene_type(_type):
from ..types.objecttype import ObjectType from ..types.objecttype import ObjectType
from ..types.mutation import Mutation
from ..types.inputobjecttype import InputObjectType from ..types.inputobjecttype import InputObjectType
from ..types.interface import Interface from ..types.interface import Interface
from ..types.scalars import Scalar from ..types.scalars import Scalar
from ..types.enum import Enum from ..types.enum import Enum
if _type in [Interface, InputObjectType, ObjectType]: if _type in [Interface, InputObjectType, ObjectType, Mutation]:
return False return False
return inspect.isclass(_type) and issubclass(_type, ( return inspect.isclass(_type) and issubclass(_type, (
Interface, Interface,
ObjectType, ObjectType,
InputObjectType, InputObjectType,
Scalar, Scalar,
Mutation,
Enum Enum
)) ))