mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-30 01:39:56 +03:00
fallback
This commit is contained in:
parent
a212c8a422
commit
64b0828fe3
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
def get_version(version=None):
|
def get_version(version=None):
|
||||||
|
@ -45,13 +44,11 @@ def get_complete_version(version=None):
|
||||||
if version is None:
|
if version is None:
|
||||||
from graphene import VERSION as version
|
from graphene import VERSION as version
|
||||||
else:
|
else:
|
||||||
raise_assertion_if_not(
|
if len(version) is not 5:
|
||||||
condition=len(version) is 5, message="Wrong tuple provided"
|
raise AssertionError("Wrong tuple provided")
|
||||||
)
|
|
||||||
raise_assertion_if_not(
|
if not version[3] in ("alpha", "beta", "rc", "final"):
|
||||||
condition=version[3] in ("alpha", "beta", "rc", "final"),
|
raise AssertionError("Version not correct.")
|
||||||
message="Version not correct.",
|
|
||||||
)
|
|
||||||
|
|
||||||
return version
|
return version
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ from ..types.field import Field
|
||||||
from ..types.objecttype import ObjectType, ObjectTypeOptions
|
from ..types.objecttype import ObjectType, ObjectTypeOptions
|
||||||
from ..utils.thenables import maybe_thenable
|
from ..utils.thenables import maybe_thenable
|
||||||
from .node import is_node
|
from .node import is_node
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class PageInfo(ObjectType):
|
class PageInfo(ObjectType):
|
||||||
|
@ -53,18 +52,15 @@ class Connection(ObjectType):
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, node=None, name=None, **options):
|
def __init_subclass_with_meta__(cls, node=None, name=None, **options):
|
||||||
_meta = ConnectionOptions(cls)
|
_meta = ConnectionOptions(cls)
|
||||||
raise_assertion_if_not(
|
if not node:
|
||||||
condition=node,
|
raise AssertionError(
|
||||||
message="You have to provide a node in {}.Meta".format(cls.__name__),
|
"You have to provide a node in {}.Meta".format(cls.__name__)
|
||||||
)
|
)
|
||||||
raise_assertion_if_not(
|
|
||||||
condition=issubclass(
|
if not issubclass(node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)):
|
||||||
node, (Scalar, Enum, ObjectType, Interface, Union, NonNull)
|
raise AssertionError('Received incompatible node "{}" for Connection {}.'.format(
|
||||||
),
|
|
||||||
message=('Received incompatible node "{}" for Connection {}.').format(
|
|
||||||
node, cls.__name__
|
node, cls.__name__
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name
|
base_name = re.sub("Connection$", "", name or cls.__name__) or node._meta.name
|
||||||
if not name:
|
if not name:
|
||||||
|
@ -139,12 +135,11 @@ class IterableConnectionField(Field):
|
||||||
"Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections"
|
"Read more: https://github.com/graphql-python/graphene/blob/v2.0.0/UPGRADE-v2.0.md#node-connections"
|
||||||
)
|
)
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if not issubclass(connection_type, Connection):
|
||||||
condition=issubclass(connection_type, Connection),
|
raise AssertionError('{} type have to be a subclass of Connection. Received "{}".'.format(
|
||||||
message='{} type have to be a subclass of Connection. Received "{}".'.format(
|
|
||||||
self.__class__.__name__, connection_type
|
self.__class__.__name__, connection_type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
return type
|
return type
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -152,15 +147,13 @@ class IterableConnectionField(Field):
|
||||||
if isinstance(resolved, connection_type):
|
if isinstance(resolved, connection_type):
|
||||||
return resolved
|
return resolved
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if not isinstance(resolved, Iterable):
|
||||||
condition=isinstance(resolved, Iterable),
|
raise AssertionError("""
|
||||||
message="""
|
|
||||||
Resolved value from the connection field have to be iterable or instance of {}.
|
Resolved value from the connection field have to be iterable or instance of {}.
|
||||||
Received "{}"
|
Received "{}"
|
||||||
""".format(
|
""".format(
|
||||||
connection_type, resolved
|
connection_type, resolved
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
connection = connection_from_list(
|
connection = connection_from_list(
|
||||||
resolved,
|
resolved,
|
||||||
|
|
|
@ -17,11 +17,11 @@ class ClientIDMutation(Mutation):
|
||||||
input_class = getattr(cls, "Input", None)
|
input_class = getattr(cls, "Input", None)
|
||||||
base_name = re.sub("Payload$", "", name or cls.__name__)
|
base_name = re.sub("Payload$", "", name or cls.__name__)
|
||||||
|
|
||||||
raise_assertion_if_not(condition=not output, message="Can't specify any output")
|
if output:
|
||||||
|
raise AssertionError("Can't specify any output")
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if arguments:
|
||||||
condition=not arguments, message="Can't specify any arguments"
|
raise AssertionError("Can't specify any arguments")
|
||||||
)
|
|
||||||
|
|
||||||
bases = (InputObjectType,)
|
bases = (InputObjectType,)
|
||||||
if input_class:
|
if input_class:
|
||||||
|
@ -44,11 +44,9 @@ class ClientIDMutation(Mutation):
|
||||||
)
|
)
|
||||||
mutate_and_get_payload = getattr(cls, "mutate_and_get_payload", None)
|
mutate_and_get_payload = getattr(cls, "mutate_and_get_payload", None)
|
||||||
if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__:
|
if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__:
|
||||||
raise_assertion_if_not(
|
if not (mutate_and_get_payload):
|
||||||
condition=mutate_and_get_payload,
|
raise AssertionError("{name}.mutate_and_get_payload method is required"
|
||||||
message="{name}.mutate_and_get_payload method is required"
|
" in a ClientIDMutation.".format(name=name or cls.__name__))
|
||||||
" in a ClientIDMutation.".format(name=name or cls.__name__),
|
|
||||||
)
|
|
||||||
|
|
||||||
if not name:
|
if not name:
|
||||||
name = "{}Payload".format(base_name)
|
name = "{}Payload".format(base_name)
|
||||||
|
|
|
@ -49,7 +49,8 @@ class GlobalID(Field):
|
||||||
|
|
||||||
class NodeField(Field):
|
class NodeField(Field):
|
||||||
def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs):
|
def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs):
|
||||||
assert issubclass(node, Node), "NodeField can only operate in Nodes"
|
if not issubclass(node, Node):
|
||||||
|
raise AssertionError("NodeField can only operate in Nodes")
|
||||||
self.node_type = node
|
self.node_type = node
|
||||||
self.field_type = type
|
self.field_type = type
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ from .dynamic import Dynamic
|
||||||
from .mountedtype import MountedType
|
from .mountedtype import MountedType
|
||||||
from .structures import NonNull
|
from .structures import NonNull
|
||||||
from .utils import get_type
|
from .utils import get_type
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class Argument(MountedType):
|
class Argument(MountedType):
|
||||||
|
@ -74,10 +73,9 @@ def to_arguments(args, extra_args=None):
|
||||||
raise ValueError('Unknown argument "{}".'.format(default_name))
|
raise ValueError('Unknown argument "{}".'.format(default_name))
|
||||||
|
|
||||||
arg_name = default_name or arg.name
|
arg_name = default_name or arg.name
|
||||||
raise_assertion_if_not(
|
if arg_name in arguments:
|
||||||
condition=arg_name not in arguments,
|
raise AssertionError('More than one Argument have same name "{}".'.format(arg_name))
|
||||||
message='More than one Argument have same name "{}".'.format(arg_name),
|
|
||||||
)
|
|
||||||
arguments[arg_name] = arg
|
arguments[arg_name] = arg
|
||||||
|
|
||||||
return arguments
|
return arguments
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from ..utils.subclass_with_meta import SubclassWithMeta
|
from ..utils.subclass_with_meta import SubclassWithMeta
|
||||||
from ..utils.trim_docstring import trim_docstring
|
from ..utils.trim_docstring import trim_docstring
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class BaseOptions(object):
|
class BaseOptions(object):
|
||||||
|
@ -32,9 +31,9 @@ class BaseType(SubclassWithMeta):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, name=None, description=None, _meta=None):
|
def __init_subclass_with_meta__(cls, name=None, description=None, _meta=None):
|
||||||
raise_assertion_if_not(
|
if "_meta" in cls.__dict__:
|
||||||
condition="_meta" not in cls.__dict__, message="Can't assign directly meta"
|
raise AssertionError("Can't assign directly meta")
|
||||||
)
|
|
||||||
if not _meta:
|
if not _meta:
|
||||||
return
|
return
|
||||||
_meta.name = name or cls.__name__
|
_meta.name = name or cls.__name__
|
||||||
|
|
|
@ -6,7 +6,6 @@ from aniso8601 import parse_date, parse_datetime, parse_time
|
||||||
from graphql.language import ast
|
from graphql.language import ast
|
||||||
|
|
||||||
from .scalars import Scalar
|
from .scalars import Scalar
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class Date(Scalar):
|
class Date(Scalar):
|
||||||
|
@ -20,10 +19,9 @@ class Date(Scalar):
|
||||||
def serialize(date):
|
def serialize(date):
|
||||||
if isinstance(date, datetime.datetime):
|
if isinstance(date, datetime.datetime):
|
||||||
date = date.date()
|
date = date.date()
|
||||||
raise_assertion_if_not(
|
if not isinstance(date, datetime.date):
|
||||||
condition=isinstance(date, datetime.date),
|
raise AssertionError('Received not compatible date "{}"'.format(repr(date)))
|
||||||
message='Received not compatible date "{}"'.format(repr(date)),
|
|
||||||
)
|
|
||||||
return date.isoformat()
|
return date.isoformat()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -48,10 +46,8 @@ class DateTime(Scalar):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def serialize(dt):
|
def serialize(dt):
|
||||||
raise_assertion_if_not(
|
if not isinstance(dt, (datetime.datetime, datetime.date)):
|
||||||
condition=isinstance(dt, (datetime.datetime, datetime.date)),
|
raise AssertionError('Received not compatible datetime "{}"'.format(repr(dt)))
|
||||||
message='Received not compatible datetime "{}"'.format(repr(dt)),
|
|
||||||
)
|
|
||||||
return dt.isoformat()
|
return dt.isoformat()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -76,10 +72,8 @@ class Time(Scalar):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def serialize(time):
|
def serialize(time):
|
||||||
raise_assertion_if_not(
|
if not isinstance(time, datetime.time):
|
||||||
condition=isinstance(time, datetime.time),
|
raise AssertionError('Received not compatible time "{}"'.format(repr(time)))
|
||||||
message='Received not compatible time "{}"'.format(repr(time)),
|
|
||||||
)
|
|
||||||
return time.isoformat()
|
return time.isoformat()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -16,9 +16,8 @@ class Decimal(Scalar):
|
||||||
def serialize(dec):
|
def serialize(dec):
|
||||||
if isinstance(dec, str):
|
if isinstance(dec, str):
|
||||||
dec = _Decimal(dec)
|
dec = _Decimal(dec)
|
||||||
assert isinstance(dec, _Decimal), 'Received not compatible Decimal "{}"'.format(
|
if not isinstance(dec, _Decimal):
|
||||||
repr(dec)
|
raise AssertionError('Received not compatible Decimal "{}"'.format(repr(dec)))
|
||||||
)
|
|
||||||
return str(dec)
|
return str(dec)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -12,7 +12,8 @@ class Dynamic(MountedType):
|
||||||
|
|
||||||
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) or isinstance(type, partial)
|
if not inspect.isfunction(type) or isinstance(type, partial):
|
||||||
|
raise AssertionError
|
||||||
self.type = type
|
self.type = type
|
||||||
self.with_schema = with_schema
|
self.with_schema = with_schema
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ from .mountedtype import MountedType
|
||||||
from .structures import NonNull
|
from .structures import NonNull
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
from .utils import get_type
|
from .utils import get_type
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
base_type = type
|
base_type = type
|
||||||
|
|
||||||
|
@ -35,24 +34,18 @@ class Field(MountedType):
|
||||||
**extra_args
|
**extra_args
|
||||||
):
|
):
|
||||||
super(Field, self).__init__(_creation_counter=_creation_counter)
|
super(Field, self).__init__(_creation_counter=_creation_counter)
|
||||||
raise_assertion_if_not(
|
if not (not args or isinstance(args, Mapping)):
|
||||||
condition=not args or isinstance(args, Mapping),
|
raise AssertionError('Arguments in a field have to be a mapping, received "{}".'.format(
|
||||||
message='Arguments in a field have to be a mapping, received "{}".'.format(
|
|
||||||
args
|
args
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if source and resolver:
|
||||||
condition=not (source and resolver),
|
raise AssertionError("A Field cannot have a source and a resolver in at the same time.")
|
||||||
message="A Field cannot have a source and a resolver in at the same time.",
|
|
||||||
)
|
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if callable(default_value):
|
||||||
condition=not callable(default_value),
|
raise AssertionError('The default value can not be a function but received "{}".'.format(
|
||||||
message='The default value can not be a function but received "{}".'.format(
|
|
||||||
base_type(default_value)
|
base_type(default_value)
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
if required:
|
if required:
|
||||||
type = NonNull(type)
|
type = NonNull(type)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from ..utils.orderedtype import OrderedType
|
from ..utils.orderedtype import OrderedType
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class MountedType(OrderedType):
|
class MountedType(OrderedType):
|
||||||
|
@ -9,10 +8,8 @@ class MountedType(OrderedType):
|
||||||
"""
|
"""
|
||||||
Mount the UnmountedType instance
|
Mount the UnmountedType instance
|
||||||
"""
|
"""
|
||||||
raise_assertion_if_not(
|
if not isinstance(unmounted, UnmountedType):
|
||||||
condition=isinstance(unmounted, UnmountedType),
|
raise AssertionError("{} can't mount {}".format(cls.__name__, repr(unmounted)))
|
||||||
message="{} can't mount {}".format(cls.__name__, repr(unmounted)),
|
|
||||||
)
|
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
unmounted.get_type(),
|
unmounted.get_type(),
|
||||||
|
|
|
@ -6,7 +6,6 @@ from ..utils.props import props
|
||||||
from .field import Field
|
from .field import Field
|
||||||
from .objecttype import ObjectType, ObjectTypeOptions
|
from .objecttype import ObjectType, ObjectTypeOptions
|
||||||
from .utils import yank_fields_from_attrs
|
from .utils import yank_fields_from_attrs
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
# For static type checking with Mypy
|
# For static type checking with Mypy
|
||||||
MYPY = False
|
MYPY = False
|
||||||
|
@ -63,10 +62,8 @@ class Mutation(ObjectType):
|
||||||
|
|
||||||
if not resolver:
|
if not resolver:
|
||||||
mutate = getattr(cls, "mutate", None)
|
mutate = getattr(cls, "mutate", None)
|
||||||
raise_assertion_if_not(
|
if not mutate:
|
||||||
condition=mutate,
|
raise AssertionError("All mutations must define a mutate method in it")
|
||||||
message="All mutations must define a mutate method in it",
|
|
||||||
)
|
|
||||||
resolver = get_unbound_function(mutate)
|
resolver = get_unbound_function(mutate)
|
||||||
|
|
||||||
if _meta.fields:
|
if _meta.fields:
|
||||||
|
|
|
@ -4,7 +4,6 @@ from .base import BaseOptions, BaseType
|
||||||
from .field import Field
|
from .field import Field
|
||||||
from .interface import Interface
|
from .interface import Interface
|
||||||
from .utils import yank_fields_from_attrs
|
from .utils import yank_fields_from_attrs
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
# For static type checking with Mypy
|
# For static type checking with Mypy
|
||||||
MYPY = False
|
MYPY = False
|
||||||
|
@ -40,24 +39,18 @@ class ObjectType(BaseType):
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
|
|
||||||
for interface in interfaces:
|
for interface in interfaces:
|
||||||
raise_assertion_if_not(
|
if not issubclass(interface, Interface):
|
||||||
condition=issubclass(interface, Interface),
|
raise AssertionError('All interfaces of {} must be a subclass of Interface. Received "{}".'.format(
|
||||||
message='All interfaces of {} must be a subclass of Interface. Received "{}".'.format(
|
|
||||||
cls.__name__, interface
|
cls.__name__, interface
|
||||||
),
|
))
|
||||||
)
|
|
||||||
fields.update(interface._meta.fields)
|
fields.update(interface._meta.fields)
|
||||||
|
|
||||||
for base in reversed(cls.__mro__):
|
for base in reversed(cls.__mro__):
|
||||||
fields.update(yank_fields_from_attrs(base.__dict__, _as=Field))
|
fields.update(yank_fields_from_attrs(base.__dict__, _as=Field))
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if possible_types and cls.is_type_of:
|
||||||
condition=not (possible_types and cls.is_type_of),
|
raise AssertionError("{name}.Meta.possible_types will cause type collision with {name}.is_type_of. "
|
||||||
message=(
|
"Please use one or other.".format(name=cls.__name__))
|
||||||
"{name}.Meta.possible_types will cause type collision with {name}.is_type_of. "
|
|
||||||
"Please use one or other.".format(name=cls.__name__)
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
if _meta.fields:
|
if _meta.fields:
|
||||||
_meta.fields.update(fields)
|
_meta.fields.update(fields)
|
||||||
|
|
|
@ -13,7 +13,6 @@ from graphql.utils.schema_printer import print_schema
|
||||||
from .definitions import GrapheneGraphQLType
|
from .definitions import GrapheneGraphQLType
|
||||||
from .objecttype import ObjectType
|
from .objecttype import ObjectType
|
||||||
from .typemap import TypeMap, is_graphene_type
|
from .typemap import TypeMap, is_graphene_type
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
def assert_valid_root_type(_type):
|
def assert_valid_root_type(_type):
|
||||||
|
@ -21,11 +20,8 @@ def assert_valid_root_type(_type):
|
||||||
return
|
return
|
||||||
is_graphene_objecttype = inspect.isclass(_type) and issubclass(_type, ObjectType)
|
is_graphene_objecttype = inspect.isclass(_type) and issubclass(_type, ObjectType)
|
||||||
is_graphql_objecttype = isinstance(_type, GraphQLObjectType)
|
is_graphql_objecttype = isinstance(_type, GraphQLObjectType)
|
||||||
raise_assertion_if_not(
|
if not is_graphene_objecttype or is_graphql_objecttype:
|
||||||
condition=is_graphene_objecttype or is_graphql_objecttype,
|
raise AssertionError("Type {} is not a valid ObjectType.".format(_type))
|
||||||
message="Type {} is not a valid ObjectType.".format(_type),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Schema(GraphQLSchema):
|
class Schema(GraphQLSchema):
|
||||||
"""
|
"""
|
||||||
|
@ -55,12 +51,10 @@ class Schema(GraphQLSchema):
|
||||||
if directives is None:
|
if directives is None:
|
||||||
directives = [GraphQLIncludeDirective, GraphQLSkipDirective]
|
directives = [GraphQLIncludeDirective, GraphQLSkipDirective]
|
||||||
|
|
||||||
raise_assertion_if_not(
|
if not all(isinstance(d, GraphQLDirective) for d in directives):
|
||||||
condition=all(isinstance(d, GraphQLDirective) for d in directives),
|
raise AssertionError("Schema directives must be List[GraphQLDirective] if provided but got: {}.".format(
|
||||||
message="Schema directives must be List[GraphQLDirective] if provided but got: {}.".format(
|
|
||||||
directives
|
directives
|
||||||
),
|
))
|
||||||
)
|
|
||||||
self._directives = directives
|
self._directives = directives
|
||||||
self.build_typemap()
|
self.build_typemap()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
from .utils import get_type
|
from .utils import get_type
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class Structure(UnmountedType):
|
class Structure(UnmountedType):
|
||||||
|
@ -68,12 +67,10 @@ class NonNull(Structure):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(NonNull, self).__init__(*args, **kwargs)
|
super(NonNull, self).__init__(*args, **kwargs)
|
||||||
raise_assertion_if_not(
|
if isinstance(self._of_type, NonNull):
|
||||||
condition=not isinstance(self._of_type, NonNull),
|
raise AssertionError("Can only create NonNull of a Nullable GraphQLType but got: {}.".format(
|
||||||
message="Can only create NonNull of a Nullable GraphQLType but got: {}.".format(
|
|
||||||
self._of_type
|
self._of_type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{}!".format(self.of_type)
|
return "{}!".format(self.of_type)
|
||||||
|
|
|
@ -40,7 +40,6 @@ from .scalars import ID, Boolean, Float, Int, Scalar, String
|
||||||
from .structures import List, NonNull
|
from .structures import List, NonNull
|
||||||
from .union import Union
|
from .union import Union
|
||||||
from .utils import get_field_as
|
from .utils import get_field_as
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
def is_graphene_type(_type):
|
def is_graphene_type(_type):
|
||||||
|
@ -61,16 +60,13 @@ def resolve_type(resolve_type_func, map, type_name, root, info):
|
||||||
|
|
||||||
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)
|
||||||
raise_assertion_if_not(
|
if not graphql_type:
|
||||||
condition=graphql_type,
|
raise AssertionError("Can't find type {} in schema".format(_type._meta.name))
|
||||||
message="Can't find type {} in schema".format(_type._meta.name),
|
|
||||||
)
|
if not graphql_type.graphene_type is _type:
|
||||||
raise_assertion_if_not(
|
raise AssertionError("The type {} does not match with the associated graphene type {}.".format(
|
||||||
condition=graphql_type.graphene_type is _type,
|
|
||||||
message="The type {} does not match with the associated graphene type {}.".format(
|
|
||||||
_type, graphql_type.graphene_type
|
_type, graphql_type.graphene_type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
return graphql_type
|
return graphql_type
|
||||||
|
|
||||||
return _type
|
return _type
|
||||||
|
@ -101,12 +97,10 @@ class TypeMap(GraphQLTypeMap):
|
||||||
if type._meta.name in map:
|
if type._meta.name in map:
|
||||||
_type = map[type._meta.name]
|
_type = map[type._meta.name]
|
||||||
if isinstance(_type, GrapheneGraphQLType):
|
if isinstance(_type, GrapheneGraphQLType):
|
||||||
raise_assertion_if_not(
|
if not _type.graphene_type is type:
|
||||||
condition=_type.graphene_type is type,
|
raise AssertionError("Found different types with the same name in the schema: {}, {}.".format(
|
||||||
message="Found different types with the same name in the schema: {}, {}.".format(
|
|
||||||
_type.graphene_type, type
|
_type.graphene_type, type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
return map
|
return map
|
||||||
|
|
||||||
if issubclass(type, ObjectType):
|
if issubclass(type, ObjectType):
|
||||||
|
@ -183,12 +177,10 @@ class TypeMap(GraphQLTypeMap):
|
||||||
if type._meta.name in map:
|
if type._meta.name in map:
|
||||||
_type = map[type._meta.name]
|
_type = map[type._meta.name]
|
||||||
if isinstance(_type, GrapheneGraphQLType):
|
if isinstance(_type, GrapheneGraphQLType):
|
||||||
raise_assertion_if_not(
|
if not _type.graphene_type is type:
|
||||||
condition=_type.graphene_type is type,
|
raise AssertionError("Found different types with the same name in the schema: {}, {}.".format(
|
||||||
message="Found different types with the same name in the schema: {}, {}.".format(
|
|
||||||
_type.graphene_type, type
|
_type.graphene_type, type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
return _type
|
return _type
|
||||||
|
|
||||||
def interfaces():
|
def interfaces():
|
||||||
|
@ -196,9 +188,8 @@ class TypeMap(GraphQLTypeMap):
|
||||||
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]
|
||||||
raise_assertion_if_not(
|
if not internal_type.graphene_type is interface:
|
||||||
condition=internal_type.graphene_type is interface
|
raise AssertionError
|
||||||
)
|
|
||||||
interfaces.append(internal_type)
|
interfaces.append(internal_type)
|
||||||
return interfaces
|
return interfaces
|
||||||
|
|
||||||
|
@ -222,12 +213,10 @@ class TypeMap(GraphQLTypeMap):
|
||||||
if type._meta.name in map:
|
if type._meta.name in map:
|
||||||
_type = map[type._meta.name]
|
_type = map[type._meta.name]
|
||||||
if isinstance(_type, GrapheneInterfaceType):
|
if isinstance(_type, GrapheneInterfaceType):
|
||||||
raise_assertion_if_not(
|
if not _type.graphene_type is type:
|
||||||
condition=_type.graphene_type is type,
|
raise AssertionError("Found different types with the same name in the schema: {}, {}.".format(
|
||||||
message="Found different types with the same name in the schema: {}, {}.".format(
|
|
||||||
_type.graphene_type, type
|
_type.graphene_type, type
|
||||||
),
|
))
|
||||||
)
|
|
||||||
return _type
|
return _type
|
||||||
|
|
||||||
_resolve_type = None
|
_resolve_type = None
|
||||||
|
@ -266,9 +255,8 @@ class TypeMap(GraphQLTypeMap):
|
||||||
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]
|
||||||
raise_assertion_if_not(
|
if not internal_type.graphene_type is objecttype:
|
||||||
condition=internal_type.graphene_type is objecttype
|
raise AssertionError
|
||||||
)
|
|
||||||
union_types.append(internal_type)
|
union_types.append(internal_type)
|
||||||
return union_types
|
return union_types
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from .base import BaseOptions, BaseType
|
from .base import BaseOptions, BaseType
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
# For static type checking with Mypy
|
# For static type checking with Mypy
|
||||||
MYPY = False
|
MYPY = False
|
||||||
|
@ -24,10 +23,8 @@ class Union(UnmountedType, BaseType):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, types=None, **options):
|
def __init_subclass_with_meta__(cls, types=None, **options):
|
||||||
raise_assertion_if_not(
|
if not isinstance(types, (list, tuple)) and len(types) > 0:
|
||||||
condition=isinstance(types, (list, tuple)) and len(types) > 0,
|
raise AssertionError("Must provide types for Union {name}.".format(name=cls.__name__))
|
||||||
message="Must provide types for Union {name}.".format(name=cls.__name__),
|
|
||||||
)
|
|
||||||
_meta = UnionOptions(cls)
|
_meta = UnionOptions(cls)
|
||||||
_meta.types = types
|
_meta.types = types
|
||||||
super(Union, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
super(Union, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
||||||
|
|
|
@ -5,7 +5,6 @@ from uuid import UUID as _UUID
|
||||||
from graphql.language import ast
|
from graphql.language import ast
|
||||||
|
|
||||||
from .scalars import Scalar
|
from .scalars import Scalar
|
||||||
from ..utils.comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class UUID(Scalar):
|
class UUID(Scalar):
|
||||||
|
@ -15,10 +14,8 @@ class UUID(Scalar):
|
||||||
def serialize(uuid):
|
def serialize(uuid):
|
||||||
if isinstance(uuid, str):
|
if isinstance(uuid, str):
|
||||||
uuid = _UUID(uuid)
|
uuid = _UUID(uuid)
|
||||||
raise_assertion_if_not(
|
if not isinstance(uuid, _UUID):
|
||||||
condition=isinstance(uuid, _UUID),
|
raise AssertionError("Expected UUID instance, received {}".format(uuid))
|
||||||
message="Expected UUID instance, received {}".format(uuid),
|
|
||||||
)
|
|
||||||
return str(uuid)
|
return str(uuid)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -2,7 +2,6 @@ import six
|
||||||
|
|
||||||
from ..pyutils.compat import func_name, signature
|
from ..pyutils.compat import func_name, signature
|
||||||
from .deprecated import warn_deprecation
|
from .deprecated import warn_deprecation
|
||||||
from .comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
def annotate(_func=None, _trigger_warning=True, **annotations):
|
def annotate(_func=None, _trigger_warning=True, **annotations):
|
||||||
|
@ -23,12 +22,10 @@ def annotate(_func=None, _trigger_warning=True, **annotations):
|
||||||
|
|
||||||
# We make sure the annotations are valid
|
# We make sure the annotations are valid
|
||||||
for key, value in annotations.items():
|
for key, value in annotations.items():
|
||||||
raise_assertion_if_not(
|
if not key in func_signature.parameters:
|
||||||
condition=key in func_signature.parameters,
|
raise AssertionError('The key {key} is not a function parameter in the function "{func_name}".'.format(
|
||||||
message='The key {key} is not a function parameter in the function "{func_name}".'.format(
|
|
||||||
key=key, func_name=func_name(_func)
|
key=key, func_name=func_name(_func)
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
func_annotations = getattr(_func, "__annotations__", None)
|
func_annotations = getattr(_func, "__annotations__", None)
|
||||||
if func_annotations is None:
|
if func_annotations is None:
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
def raise_assertion_if_not(condition=None, message=None):
|
|
||||||
if not condition:
|
|
||||||
raise AssertionError(message)
|
|
|
@ -4,7 +4,6 @@ import six
|
||||||
|
|
||||||
from ..pyutils.init_subclass import InitSubclassMeta
|
from ..pyutils.init_subclass import InitSubclassMeta
|
||||||
from .props import props
|
from .props import props
|
||||||
from .comparison_helper import raise_assertion_if_not
|
|
||||||
|
|
||||||
|
|
||||||
class SubclassWithMeta_Meta(InitSubclassMeta):
|
class SubclassWithMeta_Meta(InitSubclassMeta):
|
||||||
|
@ -43,13 +42,11 @@ class SubclassWithMeta(six.with_metaclass(SubclassWithMeta_Meta)):
|
||||||
|
|
||||||
abstract = options.pop("abstract", False)
|
abstract = options.pop("abstract", False)
|
||||||
if abstract:
|
if abstract:
|
||||||
raise_assertion_if_not(
|
if options:
|
||||||
condition=not options,
|
raise AssertionError("Abstract types can only contain the abstract attribute. "
|
||||||
message="Abstract types can only contain the abstract attribute. "
|
|
||||||
"Received: abstract, {option_keys}".format(
|
"Received: abstract, {option_keys}".format(
|
||||||
option_keys=", ".join(options.keys())
|
option_keys=", ".join(options.keys())
|
||||||
),
|
))
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
super_class = super(cls, cls)
|
super_class = super(cls, cls)
|
||||||
if hasattr(super_class, "__init_subclass_with_meta__"):
|
if hasattr(super_class, "__init_subclass_with_meta__"):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user