is a builtin in python; switch some arguments called to be

This commit is contained in:
Daniel Gallagher 2018-05-25 00:13:16 -07:00
parent 28f6353212
commit 01735b1a15
8 changed files with 153 additions and 152 deletions

View File

@ -207,7 +207,7 @@ Before:
```python ```python
class SomeMutation(Mutation): class SomeMutation(Mutation):
... ...
@classmethod @classmethod
def mutate(cls, instance, args, context, info): def mutate(cls, instance, args, context, info):
... ...
@ -218,7 +218,7 @@ With 2.0:
```python ```python
class SomeMutation(Mutation): class SomeMutation(Mutation):
... ...
def mutate(self, info, **args): def mutate(self, info, **args):
... ...
``` ```
@ -231,7 +231,7 @@ class SomeMutation(Mutation):
first_name = String(required=True) first_name = String(required=True)
last_name = String(required=True) last_name = String(required=True)
... ...
def mutate(self, info, first_name, last_name): def mutate(self, info, first_name, last_name):
... ...
``` ```
@ -250,7 +250,7 @@ If you are using Middelwares, you need to some adjustments:
Before: Before:
```python ```python
class MyGrapheneMiddleware(object): class MyGrapheneMiddleware(object):
def resolve(self, next_mw, root, args, context, info): def resolve(self, next_mw, root, args, context, info):
## Middleware code ## Middleware code
@ -261,7 +261,7 @@ class MyGrapheneMiddleware(object):
With 2.0: With 2.0:
```python ```python
class MyGrapheneMiddleware(object): class MyGrapheneMiddleware(object):
def resolve(self, next_mw, root, info, **args): def resolve(self, next_mw, root, info, **args):
context = info.context context = info.context

View File

@ -9,14 +9,14 @@ from .utils import get_type
class Argument(MountedType): class Argument(MountedType):
def __init__(self, type, default_value=None, description=None, name=None, required=False, _creation_counter=None): def __init__(self, type_, default_value=None, description=None, name=None, required=False, _creation_counter=None):
super(Argument, self).__init__(_creation_counter=_creation_counter) super(Argument, self).__init__(_creation_counter=_creation_counter)
if required: if required:
type = NonNull(type) type_ = NonNull(type_)
self.name = name self.name = name
self._type = type self._type = type_
self.default_value = default_value self.default_value = default_value
self.description = description self.description = description

View File

@ -9,10 +9,10 @@ class Dynamic(MountedType):
the schema. So we can have lazy fields. the schema. So we can have lazy fields.
''' '''
def __init__(self, type, with_schema=False, _creation_counter=None): def __init__(self, type_, with_schema=False, _creation_counter=None):
super(Dynamic, self).__init__(_creation_counter=_creation_counter) super(Dynamic, self).__init__(_creation_counter=_creation_counter)
assert inspect.isfunction(type) assert inspect.isfunction(type_)
self.type = type self.type = type_
self.with_schema = with_schema self.with_schema = with_schema
def get_type(self, schema=None): def get_type(self, schema=None):

View File

@ -20,7 +20,7 @@ def source_resolver(source, root, info, **args):
class Field(MountedType): class Field(MountedType):
def __init__(self, type, args=None, resolver=None, source=None, def __init__(self, type_, args=None, resolver=None, source=None,
deprecation_reason=None, name=None, description=None, deprecation_reason=None, name=None, description=None,
required=False, _creation_counter=None, default_value=None, required=False, _creation_counter=None, default_value=None,
**extra_args): **extra_args):
@ -36,7 +36,7 @@ class Field(MountedType):
).format(base_type(default_value)) ).format(base_type(default_value))
if required: if required:
type = NonNull(type) type_ = NonNull(type_)
# Check if name is actually an argument of the field # Check if name is actually an argument of the field
if isinstance(name, (Argument, UnmountedType)): if isinstance(name, (Argument, UnmountedType)):
@ -49,7 +49,7 @@ class Field(MountedType):
source = None source = None
self.name = name self.name = name
self._type = type self._type = type_
self.args = to_arguments(args or OrderedDict(), extra_args) self.args = to_arguments(args or OrderedDict(), extra_args)
if source: if source:
resolver = partial(source_resolver, source) resolver = partial(source_resolver, source)

View File

@ -5,14 +5,14 @@ from .utils import get_type
class InputField(MountedType): class InputField(MountedType):
def __init__(self, type, name=None, default_value=None, def __init__(self, type_, name=None, default_value=None,
deprecation_reason=None, description=None, deprecation_reason=None, description=None,
required=False, _creation_counter=None, **extra_args): required=False, _creation_counter=None, **extra_args):
super(InputField, self).__init__(_creation_counter=_creation_counter) super(InputField, self).__init__(_creation_counter=_creation_counter)
self.name = name self.name = name
if required: if required:
type = NonNull(type) type_ = NonNull(type_)
self._type = type self._type = type_
self.deprecation_reason = deprecation_reason self.deprecation_reason = deprecation_reason
self.default_value = default_value self.default_value = default_value
self.description = description self.description = description

View File

@ -12,15 +12,15 @@ from .objecttype import ObjectType
from .typemap import TypeMap, is_graphene_type from .typemap import TypeMap, is_graphene_type
def assert_valid_root_type(_type): def assert_valid_root_type(type_):
if _type is None: if type_ is None:
return return
is_graphene_objecttype = inspect.isclass( is_graphene_objecttype = inspect.isclass(
_type) and issubclass(_type, ObjectType) type_) and issubclass(type_, ObjectType)
is_graphql_objecttype = isinstance(_type, GraphQLObjectType) is_graphql_objecttype = isinstance(type_, GraphQLObjectType)
assert is_graphene_objecttype or is_graphql_objecttype, ( assert is_graphene_objecttype or is_graphql_objecttype, (
"Type {} is not a valid ObjectType." "Type {} is not a valid ObjectType."
).format(_type) ).format(type_)
class Schema(GraphQLSchema): class Schema(GraphQLSchema):
@ -72,26 +72,26 @@ class Schema(GraphQLSchema):
Example: using schema.Query for accessing the "Query" type in the Schema Example: using schema.Query for accessing the "Query" type in the Schema
''' '''
_type = super(Schema, self).get_type(type_name) type_ = super(Schema, self).get_type(type_name)
if _type is None: if type_ is None:
raise AttributeError( raise AttributeError(
'Type "{}" not found in the Schema'.format(type_name)) 'Type "{}" not found in the Schema'.format(type_name))
if isinstance(_type, GrapheneGraphQLType): if isinstance(type_, GrapheneGraphQLType):
return _type.graphene_type return type_.graphene_type
return _type return type_
def get_graphql_type(self, _type): def get_graphql_type(self, type_):
if not _type: if not type_:
return _type return type_
if is_type(_type): if is_type(type_):
return _type return type_
if is_graphene_type(_type): if is_graphene_type(type_):
graphql_type = self.get_type(_type._meta.name) graphql_type = self.get_type(type_._meta.name)
assert graphql_type, "Type {} not found in this schema.".format( assert graphql_type, "Type {} not found in this schema.".format(
_type._meta.name) type_._meta.name)
assert graphql_type.graphene_type == _type assert graphql_type.graphene_type == type_
return graphql_type return graphql_type
raise Exception("{} is not a valid GraphQL type.".format(_type)) raise Exception("{} is not a valid GraphQL type.".format(type_))
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
return graphql(self, *args, **kwargs) return graphql(self, *args, **kwargs)
@ -105,8 +105,8 @@ class Schema(GraphQLSchema):
def __str__(self): def __str__(self):
return print_schema(self) return print_schema(self)
def lazy(self, _type): def lazy(self, type_):
return lambda: self.get_type(_type) return lambda: self.get_type(type_)
def build_typemap(self): def build_typemap(self):
initial_types = [ initial_types = [

View File

@ -28,33 +28,33 @@ from .union import Union
from .utils import get_field_as from .utils import get_field_as
def is_graphene_type(_type): def is_graphene_type(type_):
if isinstance(_type, (List, NonNull)): if isinstance(type_, (List, NonNull)):
return True return True
if inspect.isclass(_type) and issubclass(_type, if inspect.isclass(type_) and issubclass(type_,
(ObjectType, InputObjectType, (ObjectType, InputObjectType,
Scalar, Interface, Union, Enum)): Scalar, Interface, Union, Enum)):
return True return True
def resolve_type(resolve_type_func, map, type_name, root, info): def resolve_type(resolve_type_func, map, type_name, root, info):
_type = resolve_type_func(root, info) type_ = resolve_type_func(root, info)
if not _type: if not type_:
return_type = map[type_name] return_type = map[type_name]
return get_default_resolve_type_fn(root, info, return_type) return get_default_resolve_type_fn(root, info, return_type)
if inspect.isclass(_type) and issubclass(_type, ObjectType): if inspect.isclass(type_) and issubclass(type_, ObjectType):
graphql_type = map.get(_type._meta.name) graphql_type = map.get(type_._meta.name)
assert graphql_type, "Can't find type {} in schema".format( assert graphql_type, "Can't find type {} in schema".format(
_type._meta.name type_._meta.name
) )
assert graphql_type.graphene_type == _type, ( assert graphql_type.graphene_type == type_, (
'The type {} does not match with the associated graphene type {}.' 'The type {} does not match with the associated graphene type {}.'
).format(_type, graphql_type.graphene_type) ).format(type_, graphql_type.graphene_type)
return graphql_type return graphql_type
return _type return type_
def is_type_of_from_possible_types(possible_types, root, info): def is_type_of_from_possible_types(possible_types, root, info):
@ -68,45 +68,45 @@ class TypeMap(GraphQLTypeMap):
self.schema = schema self.schema = schema
super(TypeMap, self).__init__(types) super(TypeMap, self).__init__(types)
def reducer(self, map, type): def reducer(self, map, type_):
if not type: if not type_:
return map return map
if inspect.isfunction(type): if inspect.isfunction(type_):
type = type() type_ = type_()
if is_graphene_type(type): if is_graphene_type(type_):
return self.graphene_reducer(map, type) return self.graphene_reducer(map, type_)
return GraphQLTypeMap.reducer(map, type) return GraphQLTypeMap.reducer(map, type_)
def graphene_reducer(self, map, type): def graphene_reducer(self, map, type_):
if isinstance(type, (List, NonNull)): if isinstance(type_, (List, NonNull)):
return self.reducer(map, type.of_type) return self.reducer(map, type_.of_type)
if type._meta.name in map: if type_._meta.name in map:
_type = map[type._meta.name] type_from_map = map[type_._meta.name]
if isinstance(_type, GrapheneGraphQLType): if isinstance(type_from_map, GrapheneGraphQLType):
assert _type.graphene_type == type, ( assert type_from_map.graphene_type == type_, (
'Found different types with the same name in the schema: {}, {}.' 'Found different types with the same name in the schema: {}, {}.'
).format(_type.graphene_type, type) ).format(type_from_map.graphene_type, type_)
return map return map
if issubclass(type, ObjectType): if issubclass(type_, ObjectType):
internal_type = self.construct_objecttype(map, type) internal_type = self.construct_objecttype(map, type_)
elif issubclass(type, InputObjectType): elif issubclass(type_, InputObjectType):
internal_type = self.construct_inputobjecttype(map, type) internal_type = self.construct_inputobjecttype(map, type_)
elif issubclass(type, Interface): elif issubclass(type_, Interface):
internal_type = self.construct_interface(map, type) internal_type = self.construct_interface(map, type_)
elif issubclass(type, Scalar): elif issubclass(type_, Scalar):
internal_type = self.construct_scalar(map, type) internal_type = self.construct_scalar(map, type_)
elif issubclass(type, Enum): elif issubclass(type_, Enum):
internal_type = self.construct_enum(map, type) internal_type = self.construct_enum(map, type_)
elif issubclass(type, Union): elif issubclass(type_, Union):
internal_type = self.construct_union(map, type) internal_type = self.construct_union(map, type_)
else: else:
raise Exception( raise Exception(
"Expected Graphene type, but received: {}.".format(type)) "Expected Graphene type, but received: {}.".format(type_))
return GraphQLTypeMap.reducer(map, internal_type) return GraphQLTypeMap.reducer(map, internal_type)
def construct_scalar(self, map, type): def construct_scalar(self, map, type_):
# We have a mapping to the original GraphQL types # We have a mapping to the original GraphQL types
# so there are no collisions. # so there are no collisions.
_scalars = { _scalars = {
@ -116,27 +116,27 @@ class TypeMap(GraphQLTypeMap):
Boolean: GraphQLBoolean, Boolean: GraphQLBoolean,
ID: GraphQLID ID: GraphQLID
} }
if type in _scalars: if type_ in _scalars:
return _scalars[type] return _scalars[type_]
return GrapheneScalarType( return GrapheneScalarType(
graphene_type=type, graphene_type=type_,
name=type._meta.name, name=type_._meta.name,
description=type._meta.description, description=type_._meta.description,
serialize=getattr(type, 'serialize', None), serialize=getattr(type_, 'serialize', None),
parse_value=getattr(type, 'parse_value', None), parse_value=getattr(type_, 'parse_value', None),
parse_literal=getattr(type, 'parse_literal', None), ) parse_literal=getattr(type_, 'parse_literal', None), )
def construct_enum(self, map, type): def construct_enum(self, map, type_):
values = OrderedDict() values = OrderedDict()
for name, value in type._meta.enum.__members__.items(): for name, value in type_._meta.enum.__members__.items():
description = getattr(value, 'description', None) description = getattr(value, 'description', None)
deprecation_reason = getattr(value, 'deprecation_reason', None) deprecation_reason = getattr(value, 'deprecation_reason', None)
if not description and callable(type._meta.description): if not description and callable(type_._meta.description):
description = type._meta.description(value) description = type_._meta.description(value)
if not deprecation_reason and callable(type._meta.deprecation_reason): if not deprecation_reason and callable(type_._meta.deprecation_reason):
deprecation_reason = type._meta.deprecation_reason(value) deprecation_reason = type_._meta.deprecation_reason(value)
values[name] = GraphQLEnumValue( values[name] = GraphQLEnumValue(
name=name, name=name,
@ -144,85 +144,85 @@ class TypeMap(GraphQLTypeMap):
description=description, description=description,
deprecation_reason=deprecation_reason) deprecation_reason=deprecation_reason)
type_description = type._meta.description(None) if callable(type._meta.description) else type._meta.description type_description = type_._meta.description(None) if callable(type_._meta.description) else type_._meta.description
return GrapheneEnumType( return GrapheneEnumType(
graphene_type=type, graphene_type=type_,
values=values, values=values,
name=type._meta.name, name=type_._meta.name,
description=type_description, ) description=type_description, )
def construct_objecttype(self, map, type): def construct_objecttype(self, map, type_):
if type._meta.name in map: if type_._meta.name in map:
_type = map[type._meta.name] type_from_map = map[type_._meta.name]
if isinstance(_type, GrapheneGraphQLType): if isinstance(type_from_map, GrapheneGraphQLType):
assert _type.graphene_type == type, ( assert type_from_map.graphene_type == type_, (
'Found different types with the same name in the schema: {}, {}.' 'Found different types with the same name in the schema: {}, {}.'
).format(_type.graphene_type, type) ).format(type_from_map.graphene_type, type_)
return _type return type_from_map
def interfaces(): def interfaces():
interfaces = [] interfaces = []
for interface in type._meta.interfaces: for interface in type_._meta.interfaces:
self.graphene_reducer(map, interface) self.graphene_reducer(map, interface)
internal_type = map[interface._meta.name] internal_type = map[interface._meta.name]
assert internal_type.graphene_type == interface assert internal_type.graphene_type == interface
interfaces.append(internal_type) interfaces.append(internal_type)
return interfaces return interfaces
if type._meta.possible_types: if type_._meta.possible_types:
is_type_of = partial(is_type_of_from_possible_types, is_type_of = partial(is_type_of_from_possible_types,
type._meta.possible_types) type_._meta.possible_types)
else: else:
is_type_of = type.is_type_of is_type_of = type_.is_type_of
return GrapheneObjectType( return GrapheneObjectType(
graphene_type=type, graphene_type=type_,
name=type._meta.name, name=type_._meta.name,
description=type._meta.description, description=type_._meta.description,
fields=partial(self.construct_fields_for_type, map, type), fields=partial(self.construct_fields_for_type, map, type_),
is_type_of=is_type_of, is_type_of=is_type_of,
interfaces=interfaces) interfaces=interfaces)
def construct_interface(self, map, type): def construct_interface(self, map, type_):
if type._meta.name in map: if type_._meta.name in map:
_type = map[type._meta.name] type_from_map = map[type_._meta.name]
if isinstance(_type, GrapheneInterfaceType): if isinstance(type_from_map, GrapheneInterfaceType):
assert _type.graphene_type == type, ( assert type_from_map.graphene_type == type_, (
'Found different types with the same name in the schema: {}, {}.' 'Found different types with the same name in the schema: {}, {}.'
).format(_type.graphene_type, type) ).format(type_from_map.graphene_type, type_)
return _type return type_from_map
_resolve_type = None _resolve_type = None
if type.resolve_type: if type_.resolve_type:
_resolve_type = partial(resolve_type, type.resolve_type, map, _resolve_type = partial(resolve_type, type_.resolve_type, map,
type._meta.name) type_._meta.name)
return GrapheneInterfaceType( return GrapheneInterfaceType(
graphene_type=type, graphene_type=type_,
name=type._meta.name, name=type_._meta.name,
description=type._meta.description, description=type_._meta.description,
fields=partial(self.construct_fields_for_type, map, type), fields=partial(self.construct_fields_for_type, map, type_),
resolve_type=_resolve_type, ) resolve_type=_resolve_type, )
def construct_inputobjecttype(self, map, type): def construct_inputobjecttype(self, map, type_):
return GrapheneInputObjectType( return GrapheneInputObjectType(
graphene_type=type, graphene_type=type_,
name=type._meta.name, name=type_._meta.name,
description=type._meta.description, description=type_._meta.description,
container_type=type._meta.container, container_type=type_._meta.container,
fields=partial( fields=partial(
self.construct_fields_for_type, map, type, is_input_type=True), self.construct_fields_for_type, map, type_, is_input_type=True),
) )
def construct_union(self, map, type): def construct_union(self, map, type_):
_resolve_type = None _resolve_type = None
if type.resolve_type: if type_.resolve_type:
_resolve_type = partial(resolve_type, type.resolve_type, map, _resolve_type = partial(resolve_type, type_.resolve_type, map,
type._meta.name) type_._meta.name)
def types(): def types():
union_types = [] union_types = []
for objecttype in type._meta.types: for objecttype in type_._meta.types:
self.graphene_reducer(map, objecttype) self.graphene_reducer(map, objecttype)
internal_type = map[objecttype._meta.name] internal_type = map[objecttype._meta.name]
assert internal_type.graphene_type == objecttype assert internal_type.graphene_type == objecttype
@ -230,8 +230,8 @@ class TypeMap(GraphQLTypeMap):
return union_types return union_types
return GrapheneUnionType( return GrapheneUnionType(
graphene_type=type, graphene_type=type_,
name=type._meta.name, name=type_._meta.name,
types=types, types=types,
resolve_type=_resolve_type, ) resolve_type=_resolve_type, )
@ -240,9 +240,9 @@ class TypeMap(GraphQLTypeMap):
return to_camel_case(name) return to_camel_case(name)
return name return name
def construct_fields_for_type(self, map, type, is_input_type=False): def construct_fields_for_type(self, map, type_, is_input_type=False):
fields = OrderedDict() fields = OrderedDict()
for name, field in type._meta.fields.items(): for name, field in type_._meta.fields.items():
if isinstance(field, Dynamic): if isinstance(field, Dynamic):
field = get_field_as(field.get_type(self.schema), _as=Field) field = get_field_as(field.get_type(self.schema), _as=Field)
if not field: if not field:
@ -271,7 +271,7 @@ class TypeMap(GraphQLTypeMap):
args=args, args=args,
resolver=field.get_resolver( resolver=field.get_resolver(
self.get_resolver_for_type( self.get_resolver_for_type(
type, type_,
name, name,
field.default_value field.default_value
) )
@ -282,15 +282,15 @@ class TypeMap(GraphQLTypeMap):
fields[field_name] = _field fields[field_name] = _field
return fields return fields
def get_resolver_for_type(self, type, name, default_value): def get_resolver_for_type(self, type_, name, default_value):
if not issubclass(type, ObjectType): if not issubclass(type_, ObjectType):
return return
resolver = getattr(type, 'resolve_{}'.format(name), None) resolver = getattr(type_, 'resolve_{}'.format(name), None)
if not resolver: if not resolver:
# If we don't find the resolver in the ObjectType class, then try to # If we don't find the resolver in the ObjectType class, then try to
# find it in each of the interfaces # find it in each of the interfaces
interface_resolver = None interface_resolver = None
for interface in type._meta.interfaces: for interface in type_._meta.interfaces:
if name not in interface._meta.fields: if name not in interface._meta.fields:
continue continue
interface_resolver = getattr(interface, interface_resolver = getattr(interface,
@ -303,13 +303,13 @@ class TypeMap(GraphQLTypeMap):
if resolver: if resolver:
return get_unbound_function(resolver) return get_unbound_function(resolver)
default_resolver = type._meta.default_resolver or get_default_resolver( default_resolver = type_._meta.default_resolver or get_default_resolver(
) )
return partial(default_resolver, name, default_value) return partial(default_resolver, name, default_value)
def get_field_type(self, map, type): def get_field_type(self, map, type_):
if isinstance(type, List): if isinstance(type_, List):
return GraphQLList(self.get_field_type(map, type.of_type)) return GraphQLList(self.get_field_type(map, type_.of_type))
if isinstance(type, NonNull): if isinstance(type_, NonNull):
return GraphQLNonNull(self.get_field_type(map, type.of_type)) return GraphQLNonNull(self.get_field_type(map, type_.of_type))
return map.get(type._meta.name) return map.get(type_._meta.name)

View File

@ -15,6 +15,7 @@ deps=
pytz pytz
iso8601 iso8601
pytest-benchmark pytest-benchmark
aniso8601
setenv = setenv =
PYTHONPATH = .:{envdir} PYTHONPATH = .:{envdir}
commands= commands=