mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-27 08:19:45 +03:00
is a builtin in python; switch some arguments called to be
This commit is contained in:
parent
28f6353212
commit
01735b1a15
|
@ -207,7 +207,7 @@ Before:
|
|||
```python
|
||||
class SomeMutation(Mutation):
|
||||
...
|
||||
|
||||
|
||||
@classmethod
|
||||
def mutate(cls, instance, args, context, info):
|
||||
...
|
||||
|
@ -218,7 +218,7 @@ With 2.0:
|
|||
```python
|
||||
class SomeMutation(Mutation):
|
||||
...
|
||||
|
||||
|
||||
def mutate(self, info, **args):
|
||||
...
|
||||
```
|
||||
|
@ -231,7 +231,7 @@ class SomeMutation(Mutation):
|
|||
first_name = String(required=True)
|
||||
last_name = String(required=True)
|
||||
...
|
||||
|
||||
|
||||
def mutate(self, info, first_name, last_name):
|
||||
...
|
||||
```
|
||||
|
@ -250,7 +250,7 @@ If you are using Middelwares, you need to some adjustments:
|
|||
Before:
|
||||
|
||||
```python
|
||||
class MyGrapheneMiddleware(object):
|
||||
class MyGrapheneMiddleware(object):
|
||||
def resolve(self, next_mw, root, args, context, info):
|
||||
|
||||
## Middleware code
|
||||
|
@ -261,7 +261,7 @@ class MyGrapheneMiddleware(object):
|
|||
With 2.0:
|
||||
|
||||
```python
|
||||
class MyGrapheneMiddleware(object):
|
||||
class MyGrapheneMiddleware(object):
|
||||
def resolve(self, next_mw, root, info, **args):
|
||||
context = info.context
|
||||
|
||||
|
|
|
@ -9,14 +9,14 @@ from .utils import get_type
|
|||
|
||||
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)
|
||||
|
||||
if required:
|
||||
type = NonNull(type)
|
||||
type_ = NonNull(type_)
|
||||
|
||||
self.name = name
|
||||
self._type = type
|
||||
self._type = type_
|
||||
self.default_value = default_value
|
||||
self.description = description
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@ class Dynamic(MountedType):
|
|||
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)
|
||||
assert inspect.isfunction(type)
|
||||
self.type = type
|
||||
assert inspect.isfunction(type_)
|
||||
self.type = type_
|
||||
self.with_schema = with_schema
|
||||
|
||||
def get_type(self, schema=None):
|
||||
|
|
|
@ -20,7 +20,7 @@ def source_resolver(source, root, info, **args):
|
|||
|
||||
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,
|
||||
required=False, _creation_counter=None, default_value=None,
|
||||
**extra_args):
|
||||
|
@ -36,7 +36,7 @@ class Field(MountedType):
|
|||
).format(base_type(default_value))
|
||||
|
||||
if required:
|
||||
type = NonNull(type)
|
||||
type_ = NonNull(type_)
|
||||
|
||||
# Check if name is actually an argument of the field
|
||||
if isinstance(name, (Argument, UnmountedType)):
|
||||
|
@ -49,7 +49,7 @@ class Field(MountedType):
|
|||
source = None
|
||||
|
||||
self.name = name
|
||||
self._type = type
|
||||
self._type = type_
|
||||
self.args = to_arguments(args or OrderedDict(), extra_args)
|
||||
if source:
|
||||
resolver = partial(source_resolver, source)
|
||||
|
|
|
@ -5,14 +5,14 @@ from .utils import get_type
|
|||
|
||||
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,
|
||||
required=False, _creation_counter=None, **extra_args):
|
||||
super(InputField, self).__init__(_creation_counter=_creation_counter)
|
||||
self.name = name
|
||||
if required:
|
||||
type = NonNull(type)
|
||||
self._type = type
|
||||
type_ = NonNull(type_)
|
||||
self._type = type_
|
||||
self.deprecation_reason = deprecation_reason
|
||||
self.default_value = default_value
|
||||
self.description = description
|
||||
|
|
|
@ -12,15 +12,15 @@ from .objecttype import ObjectType
|
|||
from .typemap import TypeMap, is_graphene_type
|
||||
|
||||
|
||||
def assert_valid_root_type(_type):
|
||||
if _type is None:
|
||||
def assert_valid_root_type(type_):
|
||||
if type_ is None:
|
||||
return
|
||||
is_graphene_objecttype = inspect.isclass(
|
||||
_type) and issubclass(_type, ObjectType)
|
||||
is_graphql_objecttype = isinstance(_type, GraphQLObjectType)
|
||||
type_) and issubclass(type_, ObjectType)
|
||||
is_graphql_objecttype = isinstance(type_, GraphQLObjectType)
|
||||
assert is_graphene_objecttype or is_graphql_objecttype, (
|
||||
"Type {} is not a valid ObjectType."
|
||||
).format(_type)
|
||||
).format(type_)
|
||||
|
||||
|
||||
class Schema(GraphQLSchema):
|
||||
|
@ -72,26 +72,26 @@ class Schema(GraphQLSchema):
|
|||
|
||||
Example: using schema.Query for accessing the "Query" type in the Schema
|
||||
'''
|
||||
_type = super(Schema, self).get_type(type_name)
|
||||
if _type is None:
|
||||
type_ = super(Schema, self).get_type(type_name)
|
||||
if type_ is None:
|
||||
raise AttributeError(
|
||||
'Type "{}" not found in the Schema'.format(type_name))
|
||||
if isinstance(_type, GrapheneGraphQLType):
|
||||
return _type.graphene_type
|
||||
return _type
|
||||
if isinstance(type_, GrapheneGraphQLType):
|
||||
return type_.graphene_type
|
||||
return type_
|
||||
|
||||
def get_graphql_type(self, _type):
|
||||
if not _type:
|
||||
return _type
|
||||
if is_type(_type):
|
||||
return _type
|
||||
if is_graphene_type(_type):
|
||||
graphql_type = self.get_type(_type._meta.name)
|
||||
def get_graphql_type(self, type_):
|
||||
if not type_:
|
||||
return type_
|
||||
if is_type(type_):
|
||||
return type_
|
||||
if is_graphene_type(type_):
|
||||
graphql_type = self.get_type(type_._meta.name)
|
||||
assert graphql_type, "Type {} not found in this schema.".format(
|
||||
_type._meta.name)
|
||||
assert graphql_type.graphene_type == _type
|
||||
type_._meta.name)
|
||||
assert graphql_type.graphene_type == 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):
|
||||
return graphql(self, *args, **kwargs)
|
||||
|
@ -105,8 +105,8 @@ class Schema(GraphQLSchema):
|
|||
def __str__(self):
|
||||
return print_schema(self)
|
||||
|
||||
def lazy(self, _type):
|
||||
return lambda: self.get_type(_type)
|
||||
def lazy(self, type_):
|
||||
return lambda: self.get_type(type_)
|
||||
|
||||
def build_typemap(self):
|
||||
initial_types = [
|
||||
|
|
|
@ -28,33 +28,33 @@ from .union import Union
|
|||
from .utils import get_field_as
|
||||
|
||||
|
||||
def is_graphene_type(_type):
|
||||
if isinstance(_type, (List, NonNull)):
|
||||
def is_graphene_type(type_):
|
||||
if isinstance(type_, (List, NonNull)):
|
||||
return True
|
||||
if inspect.isclass(_type) and issubclass(_type,
|
||||
if inspect.isclass(type_) and issubclass(type_,
|
||||
(ObjectType, InputObjectType,
|
||||
Scalar, Interface, Union, Enum)):
|
||||
return True
|
||||
|
||||
|
||||
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 get_default_resolve_type_fn(root, info, return_type)
|
||||
|
||||
if inspect.isclass(_type) and issubclass(_type, ObjectType):
|
||||
graphql_type = map.get(_type._meta.name)
|
||||
if inspect.isclass(type_) and issubclass(type_, ObjectType):
|
||||
graphql_type = map.get(type_._meta.name)
|
||||
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 {}.'
|
||||
).format(_type, graphql_type.graphene_type)
|
||||
).format(type_, graphql_type.graphene_type)
|
||||
return graphql_type
|
||||
|
||||
return _type
|
||||
return type_
|
||||
|
||||
|
||||
def is_type_of_from_possible_types(possible_types, root, info):
|
||||
|
@ -68,45 +68,45 @@ class TypeMap(GraphQLTypeMap):
|
|||
self.schema = schema
|
||||
super(TypeMap, self).__init__(types)
|
||||
|
||||
def reducer(self, map, type):
|
||||
if not type:
|
||||
def reducer(self, map, type_):
|
||||
if not type_:
|
||||
return map
|
||||
if inspect.isfunction(type):
|
||||
type = type()
|
||||
if is_graphene_type(type):
|
||||
return self.graphene_reducer(map, type)
|
||||
return GraphQLTypeMap.reducer(map, type)
|
||||
if inspect.isfunction(type_):
|
||||
type_ = type_()
|
||||
if is_graphene_type(type_):
|
||||
return self.graphene_reducer(map, type_)
|
||||
return GraphQLTypeMap.reducer(map, type_)
|
||||
|
||||
def graphene_reducer(self, map, type):
|
||||
if isinstance(type, (List, NonNull)):
|
||||
return self.reducer(map, type.of_type)
|
||||
if type._meta.name in map:
|
||||
_type = map[type._meta.name]
|
||||
if isinstance(_type, GrapheneGraphQLType):
|
||||
assert _type.graphene_type == type, (
|
||||
def graphene_reducer(self, map, type_):
|
||||
if isinstance(type_, (List, NonNull)):
|
||||
return self.reducer(map, type_.of_type)
|
||||
if type_._meta.name in map:
|
||||
type_from_map = map[type_._meta.name]
|
||||
if isinstance(type_from_map, GrapheneGraphQLType):
|
||||
assert type_from_map.graphene_type == type_, (
|
||||
'Found different types with the same name in the schema: {}, {}.'
|
||||
).format(_type.graphene_type, type)
|
||||
).format(type_from_map.graphene_type, type_)
|
||||
return map
|
||||
|
||||
if issubclass(type, ObjectType):
|
||||
internal_type = self.construct_objecttype(map, type)
|
||||
elif issubclass(type, InputObjectType):
|
||||
internal_type = self.construct_inputobjecttype(map, type)
|
||||
elif issubclass(type, Interface):
|
||||
internal_type = self.construct_interface(map, type)
|
||||
elif issubclass(type, Scalar):
|
||||
internal_type = self.construct_scalar(map, type)
|
||||
elif issubclass(type, Enum):
|
||||
internal_type = self.construct_enum(map, type)
|
||||
elif issubclass(type, Union):
|
||||
internal_type = self.construct_union(map, type)
|
||||
if issubclass(type_, ObjectType):
|
||||
internal_type = self.construct_objecttype(map, type_)
|
||||
elif issubclass(type_, InputObjectType):
|
||||
internal_type = self.construct_inputobjecttype(map, type_)
|
||||
elif issubclass(type_, Interface):
|
||||
internal_type = self.construct_interface(map, type_)
|
||||
elif issubclass(type_, Scalar):
|
||||
internal_type = self.construct_scalar(map, type_)
|
||||
elif issubclass(type_, Enum):
|
||||
internal_type = self.construct_enum(map, type_)
|
||||
elif issubclass(type_, Union):
|
||||
internal_type = self.construct_union(map, type_)
|
||||
else:
|
||||
raise Exception(
|
||||
"Expected Graphene type, but received: {}.".format(type))
|
||||
"Expected Graphene type, but received: {}.".format(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
|
||||
# so there are no collisions.
|
||||
_scalars = {
|
||||
|
@ -116,27 +116,27 @@ class TypeMap(GraphQLTypeMap):
|
|||
Boolean: GraphQLBoolean,
|
||||
ID: GraphQLID
|
||||
}
|
||||
if type in _scalars:
|
||||
return _scalars[type]
|
||||
if type_ in _scalars:
|
||||
return _scalars[type_]
|
||||
|
||||
return GrapheneScalarType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
serialize=getattr(type, 'serialize', None),
|
||||
parse_value=getattr(type, 'parse_value', None),
|
||||
parse_literal=getattr(type, 'parse_literal', None), )
|
||||
graphene_type=type_,
|
||||
name=type_._meta.name,
|
||||
description=type_._meta.description,
|
||||
serialize=getattr(type_, 'serialize', None),
|
||||
parse_value=getattr(type_, 'parse_value', None),
|
||||
parse_literal=getattr(type_, 'parse_literal', None), )
|
||||
|
||||
def construct_enum(self, map, type):
|
||||
def construct_enum(self, map, type_):
|
||||
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)
|
||||
deprecation_reason = getattr(value, 'deprecation_reason', None)
|
||||
if not description and callable(type._meta.description):
|
||||
description = type._meta.description(value)
|
||||
if not description and callable(type_._meta.description):
|
||||
description = type_._meta.description(value)
|
||||
|
||||
if not deprecation_reason and callable(type._meta.deprecation_reason):
|
||||
deprecation_reason = type._meta.deprecation_reason(value)
|
||||
if not deprecation_reason and callable(type_._meta.deprecation_reason):
|
||||
deprecation_reason = type_._meta.deprecation_reason(value)
|
||||
|
||||
values[name] = GraphQLEnumValue(
|
||||
name=name,
|
||||
|
@ -144,85 +144,85 @@ class TypeMap(GraphQLTypeMap):
|
|||
description=description,
|
||||
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(
|
||||
graphene_type=type,
|
||||
graphene_type=type_,
|
||||
values=values,
|
||||
name=type._meta.name,
|
||||
name=type_._meta.name,
|
||||
description=type_description, )
|
||||
|
||||
def construct_objecttype(self, map, type):
|
||||
if type._meta.name in map:
|
||||
_type = map[type._meta.name]
|
||||
if isinstance(_type, GrapheneGraphQLType):
|
||||
assert _type.graphene_type == type, (
|
||||
def construct_objecttype(self, map, type_):
|
||||
if type_._meta.name in map:
|
||||
type_from_map = map[type_._meta.name]
|
||||
if isinstance(type_from_map, GrapheneGraphQLType):
|
||||
assert type_from_map.graphene_type == type_, (
|
||||
'Found different types with the same name in the schema: {}, {}.'
|
||||
).format(_type.graphene_type, type)
|
||||
return _type
|
||||
).format(type_from_map.graphene_type, type_)
|
||||
return type_from_map
|
||||
|
||||
def interfaces():
|
||||
interfaces = []
|
||||
for interface in type._meta.interfaces:
|
||||
for interface in type_._meta.interfaces:
|
||||
self.graphene_reducer(map, interface)
|
||||
internal_type = map[interface._meta.name]
|
||||
assert internal_type.graphene_type == interface
|
||||
interfaces.append(internal_type)
|
||||
return interfaces
|
||||
|
||||
if type._meta.possible_types:
|
||||
if type_._meta.possible_types:
|
||||
is_type_of = partial(is_type_of_from_possible_types,
|
||||
type._meta.possible_types)
|
||||
type_._meta.possible_types)
|
||||
else:
|
||||
is_type_of = type.is_type_of
|
||||
is_type_of = type_.is_type_of
|
||||
|
||||
return GrapheneObjectType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
fields=partial(self.construct_fields_for_type, map, type),
|
||||
graphene_type=type_,
|
||||
name=type_._meta.name,
|
||||
description=type_._meta.description,
|
||||
fields=partial(self.construct_fields_for_type, map, type_),
|
||||
is_type_of=is_type_of,
|
||||
interfaces=interfaces)
|
||||
|
||||
def construct_interface(self, map, type):
|
||||
if type._meta.name in map:
|
||||
_type = map[type._meta.name]
|
||||
if isinstance(_type, GrapheneInterfaceType):
|
||||
assert _type.graphene_type == type, (
|
||||
def construct_interface(self, map, type_):
|
||||
if type_._meta.name in map:
|
||||
type_from_map = map[type_._meta.name]
|
||||
if isinstance(type_from_map, GrapheneInterfaceType):
|
||||
assert type_from_map.graphene_type == type_, (
|
||||
'Found different types with the same name in the schema: {}, {}.'
|
||||
).format(_type.graphene_type, type)
|
||||
return _type
|
||||
).format(type_from_map.graphene_type, type_)
|
||||
return type_from_map
|
||||
|
||||
_resolve_type = None
|
||||
if type.resolve_type:
|
||||
_resolve_type = partial(resolve_type, type.resolve_type, map,
|
||||
type._meta.name)
|
||||
if type_.resolve_type:
|
||||
_resolve_type = partial(resolve_type, type_.resolve_type, map,
|
||||
type_._meta.name)
|
||||
return GrapheneInterfaceType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
fields=partial(self.construct_fields_for_type, map, type),
|
||||
graphene_type=type_,
|
||||
name=type_._meta.name,
|
||||
description=type_._meta.description,
|
||||
fields=partial(self.construct_fields_for_type, map, type_),
|
||||
resolve_type=_resolve_type, )
|
||||
|
||||
def construct_inputobjecttype(self, map, type):
|
||||
def construct_inputobjecttype(self, map, type_):
|
||||
return GrapheneInputObjectType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
container_type=type._meta.container,
|
||||
graphene_type=type_,
|
||||
name=type_._meta.name,
|
||||
description=type_._meta.description,
|
||||
container_type=type_._meta.container,
|
||||
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
|
||||
if type.resolve_type:
|
||||
_resolve_type = partial(resolve_type, type.resolve_type, map,
|
||||
type._meta.name)
|
||||
if type_.resolve_type:
|
||||
_resolve_type = partial(resolve_type, type_.resolve_type, map,
|
||||
type_._meta.name)
|
||||
|
||||
def types():
|
||||
union_types = []
|
||||
for objecttype in type._meta.types:
|
||||
for objecttype in type_._meta.types:
|
||||
self.graphene_reducer(map, objecttype)
|
||||
internal_type = map[objecttype._meta.name]
|
||||
assert internal_type.graphene_type == objecttype
|
||||
|
@ -230,8 +230,8 @@ class TypeMap(GraphQLTypeMap):
|
|||
return union_types
|
||||
|
||||
return GrapheneUnionType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
graphene_type=type_,
|
||||
name=type_._meta.name,
|
||||
types=types,
|
||||
resolve_type=_resolve_type, )
|
||||
|
||||
|
@ -240,9 +240,9 @@ class TypeMap(GraphQLTypeMap):
|
|||
return to_camel_case(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()
|
||||
for name, field in type._meta.fields.items():
|
||||
for name, field in type_._meta.fields.items():
|
||||
if isinstance(field, Dynamic):
|
||||
field = get_field_as(field.get_type(self.schema), _as=Field)
|
||||
if not field:
|
||||
|
@ -271,7 +271,7 @@ class TypeMap(GraphQLTypeMap):
|
|||
args=args,
|
||||
resolver=field.get_resolver(
|
||||
self.get_resolver_for_type(
|
||||
type,
|
||||
type_,
|
||||
name,
|
||||
field.default_value
|
||||
)
|
||||
|
@ -282,15 +282,15 @@ class TypeMap(GraphQLTypeMap):
|
|||
fields[field_name] = _field
|
||||
return fields
|
||||
|
||||
def get_resolver_for_type(self, type, name, default_value):
|
||||
if not issubclass(type, ObjectType):
|
||||
def get_resolver_for_type(self, type_, name, default_value):
|
||||
if not issubclass(type_, ObjectType):
|
||||
return
|
||||
resolver = getattr(type, 'resolve_{}'.format(name), None)
|
||||
resolver = getattr(type_, 'resolve_{}'.format(name), None)
|
||||
if not resolver:
|
||||
# If we don't find the resolver in the ObjectType class, then try to
|
||||
# find it in each of the interfaces
|
||||
interface_resolver = None
|
||||
for interface in type._meta.interfaces:
|
||||
for interface in type_._meta.interfaces:
|
||||
if name not in interface._meta.fields:
|
||||
continue
|
||||
interface_resolver = getattr(interface,
|
||||
|
@ -303,13 +303,13 @@ class TypeMap(GraphQLTypeMap):
|
|||
if 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)
|
||||
|
||||
def get_field_type(self, map, type):
|
||||
if isinstance(type, List):
|
||||
return GraphQLList(self.get_field_type(map, type.of_type))
|
||||
if isinstance(type, NonNull):
|
||||
return GraphQLNonNull(self.get_field_type(map, type.of_type))
|
||||
return map.get(type._meta.name)
|
||||
def get_field_type(self, map, type_):
|
||||
if isinstance(type_, List):
|
||||
return GraphQLList(self.get_field_type(map, type_.of_type))
|
||||
if isinstance(type_, NonNull):
|
||||
return GraphQLNonNull(self.get_field_type(map, type_.of_type))
|
||||
return map.get(type_._meta.name)
|
||||
|
|
Loading…
Reference in New Issue
Block a user