diff --git a/graphene/tests/issues/test_1394.py b/graphene/tests/issues/test_1394.py index 39374381..103033a6 100644 --- a/graphene/tests/issues/test_1394.py +++ b/graphene/tests/issues/test_1394.py @@ -1,3 +1,5 @@ +from graphql import version_info as graphql_version_info + from ...types import ObjectType, Schema, String, NonNull @@ -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 diff --git a/graphene/types/definitions.py b/graphene/types/definitions.py index ac574bed..e9373ec6 100644 --- a/graphene/types/definitions.py +++ b/graphene/types/definitions.py @@ -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: @@ -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): diff --git a/graphene/types/schema.py b/graphene/types/schema.py index bceede6a..2d754747 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -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) diff --git a/graphene/types/tests/test_decimal.py b/graphene/types/tests/test_decimal.py index 1ba48bd1..aa91049f 100644 --- a/graphene/types/tests/test_decimal.py +++ b/graphene/types/tests/test_decimal.py @@ -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) @@ -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(): diff --git a/graphene/types/tests/test_json.py b/graphene/types/tests/test_json.py index 0d4ae7a6..c1ead72c 100644 --- a/graphene/types/tests/test_json.py +++ b/graphene/types/tests/test_json.py @@ -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 == [ @@ -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) diff --git a/graphene/types/tests/test_scalar.py b/graphene/types/tests/test_scalar.py index cbdfd8c5..dd841385 100644 --- a/graphene/types/tests/test_scalar.py +++ b/graphene/types/tests/test_scalar.py @@ -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." ) diff --git a/graphene/types/tests/test_uuid.py b/graphene/types/tests/test_uuid.py index d031387b..c3108329 100644 --- a/graphene/types/tests/test_uuid.py +++ b/graphene/types/tests/test_uuid.py @@ -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( @@ -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(): @@ -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 diff --git a/setup.py b/setup.py index 61c99ea4..70c91325 100644 --- a/setup.py +++ b/setup.py @@ -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", ],