def test_when_input_object_variable_is_missing_field_then_error_is_raised(): Input = g.InputObjectType( "Input", fields=lambda: ( g.input_field("field", type=g.Int), ), ) Root = g.ObjectType( "Root", fields=( g.field("one", type=g.Int, params=[ g.param("arg", type=Input), ]), ), ) graphql_query = """ query ($var: Input!) { one(arg: $var) } """ variables = {"var": {}} error = pytest.raises( GraphQLError, lambda: _document_text_to_graph_query(graphql_query, query_type=Root, variables=variables), ) assert_that(error.value.message, equal_to("Variable '$var' got invalid value {}; Field value.field of required type Int! was not provided."))
def test_input_object_type_field_names_are_converted_from_snake_case_to_camel_case(): graph_type = g.InputObjectType("Obj", fields=( g.input_field("field_name", type=g.String), )) assert_that(to_graphql_input_type(graph_type), is_graphql_non_null( is_graphql_input_object_type( fields=is_mapping({ "fieldName": anything, }), ), ))
def test_when_input_field_has_default_then_input_field_type_is_nullable(): graph_type = g.InputObjectType("Obj", fields=( g.input_field("value", type=g.String, default=""), )) assert_that(to_graphql_input_type(graph_type), is_graphql_non_null( is_graphql_input_object_type( name="Obj", fields=is_mapping({ "value": is_graphql_input_field(type=is_graphql_string), }), ), ))
def test_input_object_type_is_converted_to_non_null_graphql_input_object_type(): graph_type = g.InputObjectType("Obj", fields=( g.input_field("value", type=g.String), )) assert_that(to_graphql_input_type(graph_type), is_graphql_non_null( is_graphql_input_object_type( name="Obj", fields=is_mapping({ "value": is_graphql_input_field(type=is_graphql_non_null(is_graphql_string)), }), ), ))
# TODO: deduplicate arg/variable tests @pytest.mark.parametrize("arg_type, arg_string, arg_value", [ (g.Boolean, "true", True), (g.Float, "4.2", 4.2), (g.Int, "42", 42), (g.String, '"value"', "value"), (g.EnumType(Season), 'WINTER', Season.winter), (g.NullableType(g.Int), "42", 42), (g.NullableType(g.Int), "null", None), (g.ListType(g.Int), "[]", []), (g.ListType(g.Int), "[1, 2, 3]", [1, 2, 3]), ( g.InputObjectType("User", fields=( g.input_field("id", type=g.Int), g.input_field("name", type=g.String), )), '{id: 42, name: "Bob"}', lambda input_type: input_type(id=42, name="Bob"), ), ( g.InputObjectType("Casing", fields=( g.input_field("field_zero", type=g.Int), )), '{fieldZero: 1}', lambda input_type: input_type(field_zero=1), ), ]) def test_literal_graphql_arg_values_are_converted(arg_type, arg_string, arg_value): if callable(arg_value): arg_value = arg_value(arg_type)
@pytest.mark.parametrize( "arg_type, arg_string, arg_value", [ (g.Boolean, "true", True), (g.Float, "4.2", 4.2), (g.Int, "42", 42), (g.String, '"value"', "value"), (g.EnumType(Season), '"WINTER"', Season.winter), (g.NullableType(g.Int), "42", 42), #~ (g.NullableType(g.Int), "null", None), (g.ListType(g.Int), "[]", []), (g.ListType(g.Int), "[1, 2, 3]", [1, 2, 3]), ( g.InputObjectType("User", fields=( g.input_field("id", type=g.Int), g.input_field("name", type=g.String), )), '{id: 42, name: "Bob"}', g.Object({ "id": 42, "name": "Bob" }), ), ]) def test_graphql_arg_values_are_converted(arg_type, arg_string, arg_value): Root = g.ObjectType( "Root", fields=(g.field("one", type=g.Int, params=[