def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of # DjangoObjectType if not is_base_type(bases, DjangoObjectTypeMeta): return type.__new__(cls, name, bases, attrs) defaults = dict( name=name, description=attrs.pop('__doc__', None), model=None, local_fields=None, only_fields=(), exclude_fields=(), interfaces=(), registry=None ) if DJANGO_FILTER_INSTALLED: # In case Django filter is available, then # we allow more attributes in Meta defaults.update( filter_fields=(), filter_order_by=(), ) options = Options( attrs.pop('Meta', None), **defaults ) if not options.registry: options.registry = get_global_registry() assert isinstance(options.registry, Registry), ( 'The attribute registry in {}.Meta needs to be an instance of ' 'Registry, received "{}".' ).format(name, options.registry) assert is_valid_django_model(options.model), ( 'You need to pass a valid Django Model in {}.Meta, received "{}".' ).format(name, options.model) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) options.registry.register(cls) options.django_fields = yank_fields_from_attrs( construct_fields(options), _as=Field, ) options.fields = merge( options.interface_fields, options.django_fields, options.base_fields, options.local_fields ) return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of # DjangoObjectType if not is_base_type(bases, DjangoObjectTypeMeta): return type.__new__(cls, name, bases, attrs) defaults = dict( name=name, description=attrs.pop('__doc__', None), model=None, local_fields=None, only_fields=(), exclude_fields=(), interfaces=(), registry=None ) if DJANGO_FILTER_INSTALLED: # In case Django filter is available, then # we allow more attributes in Meta defaults.update( filter_fields=(), filter_order_by=(), ) options = Options( attrs.pop('Meta', None), **defaults ) if not options.registry: options.registry = get_global_registry() assert isinstance(options.registry, Registry), ( 'The attribute registry in {}.Meta needs to be an instance of ' 'Registry, received "{}".' ).format(name, options.registry) assert is_valid_django_model(options.model), ( 'You need to pass a valid Django Model in {}.Meta, received "{}".' ).format(name, options.model) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) options.registry.register(cls) options.django_fields = yank_fields_from_attrs( construct_fields(options), _as=Field, ) options.fields = merge( options.interface_fields, options.django_fields, options.base_fields, options.local_fields ) return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of # DjangoObjectType if not is_base_type(bases, SpecialObjectTypeMeta): return type.__new__(cls, name, bases, attrs) options = Options( attrs.pop('Meta', None), other_attr='default', ) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) assert cls._meta is options return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of Model # (excluding Model class itself). if not is_base_type(bases, SQLAlchemyObjectTypeMeta): return type.__new__(cls, name, bases, attrs) options = Options( attrs.pop('Meta', None), name=name, description=attrs.pop('__doc__', None), model=None, local_fields=None, only_fields=(), exclude_fields=(), id='id', interfaces=(), registry=None, abstract=False, ) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) if options.abstract: return cls if not options.registry: options.registry = get_global_registry() assert isinstance( options.registry, Registry), ('The attribute registry in {}.Meta needs to be an' ' instance of Registry, received "{}".').format( name, options.registry) assert is_mapped_class( options.model), ('You need to pass a valid SQLAlchemy Model in ' '{}.Meta, received "{}".').format( name, options.model) options.registry.register(cls) options.sqlalchemy_fields = yank_fields_from_attrs( construct_fields(options), _as=Field, ) options.fields = merge(options.interface_fields, options.sqlalchemy_fields, options.base_fields, options.local_fields) return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of Model # (excluding Model class itself). if not is_base_type(bases, SQLAlchemyObjectTypeMeta): return type.__new__(cls, name, bases, attrs) meta = attrs.pop('Meta', None) local_fields = OrderedDict( {k: v for k, v in attrs.items() if _is_graphql(v)}) tbl_doc = attrs.pop('__doc__', getattr(meta.model, '__doc__', None)) options = Options(meta, name=name, description=tbl_doc if not tbl_doc else (tbl_doc if isinstance(tbl_doc, unicode) else tbl_doc.decode('utf8')), model=None, local_fields=local_fields, exclude_fields=set(), interfaces=(), registry=None) if not options.registry: options.registry = get_global_registry() assert isinstance( options.registry, Registry), ('The attribute registry in {}.Meta needs to be an' ' instance of Registry, received "{}".').format( name, options.registry) assert is_mapped( options.model), ('You need to pass a valid SQLAlchemy Model in ' '{}.Meta, received "{}".').format( name, options.model) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) options.registry.register(cls) options.sqlalchemy_fields = yank_fields_from_attrs( construct_fields(options), _as=graphene.Field, ) options.fields = merge(options.interface_fields, options.sqlalchemy_fields, options.base_fields, options.local_fields) return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of # Mutation if not is_base_type(bases, DeleteMutationMeta): return type.__new__(cls, name, bases, attrs) cls = ObjectTypeMeta.__new__(cls, name, bases, attrs) input_class = getattr(cls, 'input_class', None)() field_args = props(input_class) if input_class else {} resolver = getattr(cls, 'mutate', None) assert resolver, 'All mutations must define a mutate method in it' resolver = get_unbound_function(resolver) cls.Field = partial(Field, cls, args=field_args, resolver=resolver) return cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of Model # (excluding Model class itself). if not is_base_type(bases, SQLAlchemyObjectTypeMeta): return type.__new__(cls, name, bases, attrs) meta = attrs.pop('Meta', None) local_fields = OrderedDict({k:v for k,v in attrs.items() if _is_graphql(v)}) options = Options( meta, name=name, description=attrs.pop('__doc__', getattr(meta.model, '__doc__', None)), model=None, local_fields=local_fields, exclude_fields=set(), interfaces=(), registry=None ) if not options.registry: options.registry = get_global_registry() assert isinstance(options.registry, Registry), ( 'The attribute registry in {}.Meta needs to be an' ' instance of Registry, received "{}".' ).format(name, options.registry) assert is_mapped(options.model), ( 'You need to pass a valid SQLAlchemy Model in ' '{}.Meta, received "{}".' ).format(name, options.model) cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) options.registry.register(cls) options.sqlalchemy_fields = yank_fields_from_attrs( construct_fields(options), _as = graphene.Field, ) options.fields = merge( options.interface_fields, options.sqlalchemy_fields, options.base_fields, options.local_fields ) return cls
def __new__(cls, name, bases, attrs): if not is_base_type(bases, SerializerMutationMeta): return type.__new__(cls, name, bases, attrs) options = Options( attrs.pop('Meta', None), name=name, description=attrs.pop('__doc__', None), serializer_class=None, local_fields=None, only_fields=(), exclude_fields=(), interfaces=(), registry=None ) if not options.serializer_class: raise Exception('Missing serializer_class') cls = ObjectTypeMeta.__new__( cls, name, bases, dict(attrs, _meta=options) ) serializer_fields = cls.fields_for_serializer(options) options.serializer_fields = yank_fields_from_attrs( serializer_fields, _as=Field, ) options.fields = merge( options.interface_fields, options.serializer_fields, options.base_fields, options.local_fields, {'errors': get_field_as(cls.errors, Field)} ) cls.Input = convert_serializer_to_input_type(options.serializer_class) cls.Field = partial( Field, cls, resolver=cls.mutate, input=Argument(cls.Input, required=True) ) return cls
def __new__(cls, name, bases, attrs): if not is_base_type(bases, SerializerMutationMeta): return type.__new__(cls, name, bases, attrs) options = Options(attrs.pop('Meta', None), name=name, description=attrs.pop('__doc__', None), serializer_class=None, local_fields=None, only_fields=(), exclude_fields=(), interfaces=(), registry=None) if not options.serializer_class: raise Exception('Missing serializer_class') cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) serializer_fields = cls.fields_for_serializer(options) options.serializer_fields = yank_fields_from_attrs( serializer_fields, _as=Field, ) options.fields = merge(options.interface_fields, options.serializer_fields, options.base_fields, options.local_fields, {'errors': get_field_as(cls.errors, Field)}) cls.Input = convert_serializer_to_input_type(options.serializer_class) cls.Field = partial(Field, cls, resolver=cls.mutate, input=Argument(cls.Input, required=True)) return cls
def __new__(mcs, name, bases, attrs): if not is_base_type(bases, NdbObjectTypeMeta): return type.__new__(mcs, name, bases, attrs) options = Options(attrs.pop('Meta', None), name=name, description=attrs.pop('__doc__', None), model=None, local_fields=None, only_fields=(), exclude_fields=(), interfaces=(), registry=None) if not options.model: raise Exception( 'NdbObjectType %s must have a model in the Meta class attr' % name) if not inspect.isclass(options.model) or not issubclass( options.model, ndb.Model): raise Exception('Provided model in %s is not an NDB model' % name) new_cls = ObjectTypeMeta.__new__(mcs, name, bases, dict(attrs, _meta=options)) mcs.register(new_cls) ndb_fields = mcs.fields_for_ndb_model(options) options.ndb_fields = yank_fields_from_attrs( ndb_fields, _as=Field, ) options.fields = merge(options.interface_fields, options.ndb_fields, options.base_fields, options.local_fields) return new_cls
def __new__(cls, name, bases, attrs): # Also ensure initialization is only performed for subclasses of # Mutation if not is_base_type(bases, ClientIDMutationMeta): return type.__new__(cls, name, bases, attrs) defaults = dict( name=name, description=attrs.pop('__doc__', None), interfaces=(), local_fields=None, permission_classes=(), throttle_classes=(), # for Django Model Permissions model=None, # for permissions method=None, # 'CREATE', 'UPDATE', 'DELETE' only this 3 accepted form_class=None, lookup_field='pk', lookup_kwarg=None ) options = Options( attrs.pop('Config', None), **defaults ) if options.model is not None: assert is_valid_django_model(options.model), ( 'You need to pass a valid Django Model in {}.Meta, received "{}".' ).format(name, options.model) input_class = attrs.pop('Input', None) base_name = re.sub('Payload$', '', name) if 'client_mutation_id' not in attrs: attrs['client_mutation_id'] = String(name='clientMutationId') cls = ObjectTypeMeta.__new__(cls, '{}Payload'.format(base_name), bases, dict(attrs, _meta=options)) mutate_and_get_payload = getattr(cls, 'mutate_and_get_payload', None) if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__: assert mutate_and_get_payload, ( "{}.mutate_and_get_payload method is required" " in a ClientIDMutation." ).format(name) input_attrs = {} bases = () if not input_class: input_attrs = {} elif not issubclass(input_class, AbstractType): input_attrs = props(input_class) else: bases += (input_class, ) input_attrs['client_mutation_id'] = String(name='clientMutationId') # If method is update and delete add id to input attrs if options.method is not None and options.method.upper() in ['UPDATE', 'DELETE']: input_attrs['id'] = String(name='id', required=True) cls.Input = type('{}Input'.format(base_name), bases + (InputObjectType,), input_attrs) cls.Field = partial(Field, cls, resolver=cls.mutate, input=Argument(cls.Input, required=True)) return cls