def get_arg_serializer(arg_type): if isinstance(arg_type, GraphQLNonNull): return get_arg_serializer(arg_type.of_type) if isinstance(arg_type, GraphQLList): inner_serializer = get_arg_serializer(arg_type.of_type) return partial(serialize_list, inner_serializer) if isinstance(arg_type, GraphQLEnumType): return lambda value: ast.EnumValue(value=arg_type.serialize(value)) return arg_type.serialize
def test_converts_input_objects(): value = OrderedDict([('foo', 3), ('bar', 'HELLO')]) assert ast_from_value(value) == ast.ObjectValue(fields=[ ast.ObjectField(name=ast.Name('foo'), value=ast.IntValue('3')), ast.ObjectField(name=ast.Name('bar'), value=ast.StringValue('HELLO')) ]) input_obj = GraphQLInputObjectType( 'MyInputObj', { 'foo': GraphQLInputObjectField(GraphQLFloat), 'bar': GraphQLInputObjectField(my_enum) }) assert ast_from_value(value, input_obj) == ast.ObjectValue(fields=[ ast.ObjectField(name=ast.Name('foo'), value=ast.FloatValue('3.0')), ast.ObjectField(name=ast.Name('bar'), value=ast.EnumValue('HELLO')) ])
def test_converts_input_objects(): value = OrderedDict([("foo", 3), ("bar", "HELLO")]) assert ast_from_value(value) == ast.ObjectValue(fields=[ ast.ObjectField(name=ast.Name("foo"), value=ast.IntValue("3")), ast.ObjectField(name=ast.Name("bar"), value=ast.StringValue("HELLO")), ]) input_obj = GraphQLInputObjectType( "MyInputObj", { "foo": GraphQLInputObjectField(GraphQLFloat), "bar": GraphQLInputObjectField(my_enum), }, ) assert ast_from_value(value, input_obj) == ast.ObjectValue(fields=[ ast.ObjectField(name=ast.Name("foo"), value=ast.FloatValue("3.0")), ast.ObjectField(name=ast.Name("bar"), value=ast.EnumValue("HELLO")), ])
def get_arg_serializer(arg_type): if isinstance(arg_type, GraphQLNonNull): return get_arg_serializer(arg_type.of_type) if isinstance(arg_type, GraphQLInputObjectField): return get_arg_serializer(arg_type.type) if isinstance(arg_type, GraphQLInputObjectType): serializers = { k: get_arg_serializer(v) for k, v in arg_type.fields.items() } return lambda value: ast.ObjectValue(fields=[ ast.ObjectField(ast.Name(k), serializers[k](v)) for k, v in value.items() ]) if isinstance(arg_type, GraphQLList): inner_serializer = get_arg_serializer(arg_type.of_type) return partial(serialize_list, inner_serializer) if isinstance(arg_type, GraphQLEnumType): return lambda value: ast.EnumValue(value=arg_type.serialize(value)) return lambda value: ast_from_value(arg_type.serialize(value))
def test_converts_list_singletons(): assert ast_from_value("FOO", GraphQLList(my_enum)) == ast.EnumValue("FOO")
def test_converts_string_values_to_enum_asts_if_possible(): assert ast_from_value("hello", my_enum) == ast.EnumValue("hello") assert ast_from_value("HELLO", my_enum) == ast.EnumValue("HELLO") assert ast_from_value("VAL\nUE", my_enum) == ast.StringValue("VAL\\nUE") assert ast_from_value("123", my_enum) == ast.StringValue("123")
def test_converts_string_values_to_enum_asts_if_possible(): assert ast_from_value('hello', my_enum) == ast.EnumValue('hello') assert ast_from_value('HELLO', my_enum) == ast.EnumValue('HELLO') assert ast_from_value('VAL\nUE', my_enum) == ast.StringValue('VAL\\nUE') assert ast_from_value('123', my_enum) == ast.StringValue('123')