Added get_fields to options

This commit is contained in:
Syrus Akbary 2016-06-21 17:30:33 -07:00
parent b5458493ce
commit 1827099381
6 changed files with 33 additions and 22 deletions

View File

@ -54,6 +54,10 @@ class ConnectionMeta(ObjectTypeMeta):
) )
cls.Edge = type(edge.name, (ObjectType, ), {'Meta': type('Meta', (object,), {'graphql_type': edge})}) cls.Edge = type(edge.name, (ObjectType, ), {'Meta': type('Meta', (object,), {'graphql_type': edge})})
cls._meta.graphql_type = connection cls._meta.graphql_type = connection
fields = copy_fields(Field, options.graphql_type.get_fields(), parent=cls)
cls._meta.get_fields = lambda: fields
return cls return cls

View File

@ -52,6 +52,8 @@ class ClientIDMutationMeta(MutationMeta):
mutate_and_get_payload=cls.mutate_and_get_payload, mutate_and_get_payload=cls.mutate_and_get_payload,
) )
options.graphql_type = field.type options.graphql_type = field.type
options.get_fields = lambda: output_fields
cls.Field = partial(Field.copy_and_extend, field, type=field.type, _creation_counter=None) cls.Field = partial(Field.copy_and_extend, field, type=field.type, _creation_counter=None)
return cls return cls

View File

@ -10,6 +10,8 @@ from ..types.objecttype import ObjectType, ObjectTypeMeta, is_objecttype
from ..types.options import Options from ..types.options import Options
from .connection import Connection from .connection import Connection
from ..utils.copy_fields import copy_fields
# We inherit from ObjectTypeMeta as we want to allow # We inherit from ObjectTypeMeta as we want to allow
# inheriting from Node, and also ObjectType. # inheriting from Node, and also ObjectType.
@ -23,16 +25,17 @@ class NodeMeta(ObjectTypeMeta):
meta, meta,
) )
def __new__(cls, name, bases, attrs): @staticmethod
def _create_objecttype(cls, name, bases, attrs):
if is_objecttype(bases): # The interface provided by node_definitions is not an instance
cls = super(NodeMeta, cls).__new__(cls, name, bases, attrs) # of GrapheneInterfaceType, so it will have no graphql_type,
# The interface provided by node_definitions is not an instance # so will not trigger Node.implements
# of GrapheneInterfaceType, so it will have no graphql_type, cls = super(NodeMeta, cls)._create_objecttype(cls, name, bases, attrs)
# so will not trigger Node.implements cls.implements(cls)
cls.implements(cls) return cls
return cls
@staticmethod
def _create_interface(cls, name, bases, attrs):
options = cls._get_interface_options(attrs.pop('Meta', None)) options = cls._get_interface_options(attrs.pop('Meta', None))
cls = type.__new__(cls, name, bases, dict(attrs, _meta=options)) cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))
@ -45,6 +48,10 @@ class NodeMeta(ObjectTypeMeta):
type_resolver=cls.resolve_type, type_resolver=cls.resolve_type,
) )
options.graphql_type = node_interface options.graphql_type = node_interface
fields = copy_fields(Field, options.graphql_type.get_fields(), parent=cls)
options.get_fields = lambda: fields
cls.Field = partial( cls.Field = partial(
Field.copy_and_extend, Field.copy_and_extend,
node_field, node_field,

View File

@ -11,16 +11,14 @@ from .objecttype import ObjectType, ObjectTypeMeta
class MutationMeta(ObjectTypeMeta): class MutationMeta(ObjectTypeMeta):
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs):
super_new = super(MutationMeta, cls).__new__ # Also ensure initialization is only performed for subclasses of
# Mutation
# Also ensure initialization is only performed for subclasses of Model
# (excluding Model class itself).
if not is_base_type(bases, MutationMeta): if not is_base_type(bases, MutationMeta):
return type.__new__(cls, name, bases, attrs) return type.__new__(cls, name, bases, attrs)
Input = attrs.pop('Input', None) Input = attrs.pop('Input', None)
cls = super_new(cls, name, bases, attrs) cls = cls._create_objecttype(cls, name, bases, attrs)
field_args = props(Input) if Input else {} field_args = props(Input) if Input else {}
resolver = getattr(cls, 'mutate', None) resolver = getattr(cls, 'mutate', None)
assert resolver, 'All mutations must define a mutate method in it' assert resolver, 'All mutations must define a mutate method in it'

View File

@ -56,7 +56,7 @@ class ObjectTypeMeta(type):
super_new = type.__new__ super_new = type.__new__
# Also ensure initialization is only performed for subclasses of # Also ensure initialization is only performed for subclasses of
# ObjectType, Interfaces # ObjectType,or Interfaces
if not is_base_type(bases, ObjectTypeMeta): if not is_base_type(bases, ObjectTypeMeta):
return type.__new__(cls, name, bases, attrs) return type.__new__(cls, name, bases, attrs)
@ -100,7 +100,9 @@ class ObjectTypeMeta(type):
) )
else: else:
assert not fields, "Can't mount Fields in an Interface with a defined graphql_type" assert not fields, "Can't mount Fields in an Interface with a defined graphql_type"
fields = copy_fields(options.graphql_type.get_fields(), parent=cls) fields = copy_fields(Field, options.graphql_type.get_fields(), parent=cls)
options.get_fields = lambda: fields
for name, field in fields.items(): for name, field in fields.items():
setattr(cls, field.attname or name, field) setattr(cls, field.attname or name, field)
@ -138,6 +140,8 @@ class ObjectTypeMeta(type):
assert not fields, "Can't mount Fields in an ObjectType with a defined graphql_type" assert not fields, "Can't mount Fields in an ObjectType with a defined graphql_type"
fields = copy_fields(Field, options.graphql_type.get_fields(), parent=cls) fields = copy_fields(Field, options.graphql_type.get_fields(), parent=cls)
options.get_fields = lambda: fields
for name, field in fields.items(): for name, field in fields.items():
setattr(cls, field.attname or name, field) setattr(cls, field.attname or name, field)
@ -149,11 +153,7 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta)):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# GraphQL ObjectType acting as container # GraphQL ObjectType acting as container
args_len = len(args) args_len = len(args)
_fields = self._meta.graphql_type._fields fields = self._meta.get_fields().items()
if callable(_fields):
_fields = _fields()
fields = _fields.items()
for name, f in fields: for name, f in fields:
setattr(self, getattr(f, 'attname', name), None) setattr(self, getattr(f, 'attname', name), None)

View File

@ -21,7 +21,7 @@ def get_fields_from_bases_and_types(bases, types):
for _class in bases: for _class in bases:
if not is_graphene_type(_class): if not is_graphene_type(_class):
continue continue
_fields = get_graphql_type(_class)._fields _fields = _class._meta.get_fields()
if callable(_fields): if callable(_fields):
_fields = _fields() _fields = _fields()