Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions graphene/tests/issues/test_1394.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from graphql import version_info as graphql_version_info

from ...types import ObjectType, Schema, String, NonNull


Expand Down Expand Up @@ -30,7 +32,9 @@ def test_required_input_missing():
result = schema.execute("{ hello }")
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Field 'hello' argument 'input' of type 'String!' is required, but it was not provided."
expected_message = (
"Argument 'Query.hello(input:)' of type 'String!' is required, but it was not provided."
if graphql_version_info >= (3, 3)
else "Field 'hello' argument 'input' of type 'String!' is required, but it was not provided."
)
assert result.errors[0].message == expected_message
12 changes: 10 additions & 2 deletions graphene/types/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):


class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
def serialize(self, value):
def _coerce_enum_value(self, value):
if not isinstance(value, PyEnum):
enum = self.graphene_type._meta.enum
try:
Expand All @@ -55,7 +55,15 @@ def serialize(self, value):
value = enum[value]
except KeyError:
pass
return super(GrapheneEnumType, self).serialize(value)
return value

def serialize(self, value):
return super(GrapheneEnumType, self).serialize(self._coerce_enum_value(value))

def coerce_output_value(self, output_value):
return super(GrapheneEnumType, self).coerce_output_value(
self._coerce_enum_value(output_value)
)


class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType):
Expand Down
5 changes: 4 additions & 1 deletion graphene/types/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ async def subscribe(self, query, *args, **kwargs):

# Execute the query
kwargs = normalize_execute_kwargs(kwargs)
return await subscribe(self.graphql_schema, document, *args, **kwargs)
result = subscribe(self.graphql_schema, document, *args, **kwargs)
if inspect.isawaitable(result):
return await result
return result

def introspect(self):
introspection = self.execute(introspection_query)
Expand Down
13 changes: 10 additions & 3 deletions graphene/types/tests/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def test_decimal_string_query_variable():


def test_bad_decimal_query():
from graphql import version_info as graphql_version_info

sep = "found:" if graphql_version_info >= (3, 3) else "found"
not_a_decimal = "Nobody expects the Spanish Inquisition!"

result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal)
Expand All @@ -44,20 +47,24 @@ def test_bad_decimal_query():
assert result.data is None
assert (
result.errors[0].message
== "Expected value of type 'Decimal', found \"Nobody expects the Spanish Inquisition!\"."
== f"Expected value of type 'Decimal', {sep} \"Nobody expects the Spanish Inquisition!\"."
)

result = schema.execute("{ decimal(input: true) }")
assert result.errors
assert len(result.errors) == 1
assert result.data is None
assert result.errors[0].message == "Expected value of type 'Decimal', found true."
assert (
result.errors[0].message == f"Expected value of type 'Decimal', {sep} true."
)

result = schema.execute("{ decimal(input: 1.2) }")
assert result.errors
assert len(result.errors) == 1
assert result.data is None
assert result.errors[0].message == "Expected value of type 'Decimal', found 1.2."
assert (
result.errors[0].message == f"Expected value of type 'Decimal', {sep} 1.2."
)


def test_decimal_string_query_integer():
Expand Down
38 changes: 26 additions & 12 deletions graphene/types/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,34 @@ def test_jsonstring_invalid_query():
"""
Test that if an invalid type is provided we get an error
"""
from graphql import version_info as graphql_version_info

is_v33 = graphql_version_info >= (3, 3)
sep = "found:" if is_v33 else "found"

result = schema.execute("{ json(input: 1) }")
assert result.errors == [
{"message": "Expected value of type 'JSONString', found 1."},
{"message": f"Expected value of type 'JSONString', {sep} 1."},
]

result = schema.execute("{ json(input: {}) }")
assert result.errors == [
{"message": "Expected value of type 'JSONString', found {}."},
]
expected_obj_msg = (
"Expected value of type 'JSONString', found: { }."
if is_v33
else "Expected value of type 'JSONString', found {}."
)
assert result.errors == [{"message": expected_obj_msg}]

result = schema.execute('{ json(input: "a") }')
assert result.errors == [
{
"message": "Expected value of type 'JSONString', found \"a\"; "
"Badly formed JSONString: Expecting value: line 1 column 1 (char 0)",
},
]
expected_bad_json_msg = (
"Expected value of type 'JSONString', but encountered error "
"'Badly formed JSONString: Expecting value: line 1 column 1 (char 0)'; "
'found: "a".'
if is_v33
else "Expected value of type 'JSONString', found \"a\"; "
"Badly formed JSONString: Expecting value: line 1 column 1 (char 0)"
)
assert result.errors == [{"message": expected_bad_json_msg}]

result = schema.execute("""{ json(input: "{\\'key\\': 0}") }""")
assert result.errors == [
Expand All @@ -75,6 +86,9 @@ def test_jsonstring_invalid_query():

result = schema.execute("""{ json(input: "{\\"key\\": 0,}") }""")
assert len(result.errors) == 1
assert result.errors[0].message.startswith(
'Expected value of type \'JSONString\', found "{\\"key\\": 0,}"; Badly formed JSONString:'
expected_prefix = (
"Expected value of type 'JSONString', but encountered error 'Badly formed JSONString:"
if is_v33
else 'Expected value of type \'JSONString\', found "{\\"key\\": 0,}"; Badly formed JSONString:'
)
assert result.errors[0].message.startswith(expected_prefix)
13 changes: 10 additions & 3 deletions graphene/types/tests/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,32 @@ def test_invalid_input(self):
"""
Test that if an invalid type is provided we get an error
"""
from graphql import version_info as graphql_version_info

sep = "found:" if graphql_version_info >= (3, 3) else "found"

result = schema.execute('{ optional { bigInt(input: "20") } }')
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message == "Expected value of type 'BigInt', found \"20\"."
result.errors[0].message
== f"Expected value of type 'BigInt', {sep} \"20\"."
)

result = schema.execute('{ optional { bigInt(input: "a") } }')
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message == "Expected value of type 'BigInt', found \"a\"."
result.errors[0].message
== f"Expected value of type 'BigInt', {sep} \"a\"."
)

result = schema.execute("{ optional { bigInt(input: true) } }")
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message == "Expected value of type 'BigInt', found true."
result.errors[0].message
== f"Expected value of type 'BigInt', {sep} true."
)


Expand Down
33 changes: 25 additions & 8 deletions graphene/types/tests/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def test_uuidstring_query_variable():


def test_uuidstring_invalid_argument():
from graphql import version_info as graphql_version_info

uuid_value = {"not": "a string"}

result = schema.execute(
Expand All @@ -45,10 +47,12 @@ def test_uuidstring_invalid_argument():
)
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Variable '$uuid' got invalid value {'not': 'a string'}; UUID cannot represent value: {'not': 'a string'}"
expected = (
"Variable '$uuid' has invalid value: UUID cannot represent value: {'not': 'a string'}"
if graphql_version_info >= (3, 3)
else "Variable '$uuid' got invalid value {'not': 'a string'}; UUID cannot represent value: {'not': 'a string'}"
)
assert result.errors[0].message == expected


def test_uuidstring_optional_uuid_input():
Expand All @@ -64,20 +68,33 @@ def test_uuidstring_invalid_query():
"""
Test that if an invalid type is provided we get an error
"""
from graphql import version_info as graphql_version_info

is_v33 = graphql_version_info >= (3, 3)
sep = "found:" if is_v33 else "found"

result = schema.execute("{ uuid(input: 1) }")
assert result.errors
assert len(result.errors) == 1
assert result.errors[0].message == "Expected value of type 'UUID', found 1."
assert result.errors[0].message == f"Expected value of type 'UUID', {sep} 1."

result = schema.execute('{ uuid(input: "a") }')
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Expected value of type 'UUID', found \"a\"; badly formed hexadecimal UUID string"
expected_bad_uuid = (
"Expected value of type 'UUID', but encountered error "
"'badly formed hexadecimal UUID string'; found: \"a\"."
if is_v33
else "Expected value of type 'UUID', found \"a\"; badly formed hexadecimal UUID string"
)
assert result.errors[0].message == expected_bad_uuid

result = schema.execute("{ requiredUuid(input: null) }")
assert result.errors
assert len(result.errors) == 1
assert result.errors[0].message == "Expected value of type 'UUID!', found null."
expected_null_msg = (
"Expected value of non-null type 'UUID!' not to be None."
if is_v33
else "Expected value of type 'UUID!', found null."
)
assert result.errors[0].message == expected_null_msg
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def run_tests(self):
keywords="api graphql protocol rest relay graphene",
packages=find_packages(exclude=["examples*"]),
install_requires=[
"graphql-core>=3.1,<3.3",
"graphql-relay>=3.1,<3.3",
"graphql-core>=3.1,<3.4",
"graphql-relay>=3.1,<3.4",
"python-dateutil>=2.7.0,<3",
"typing-extensions>=4.7.1,<5",
],
Expand Down