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_parses_null_value_in_list(): result = parse('{ fieldWithObjectInput(input: {b: ["A", null, "C"], c: "C"}) }') assert result == ast.Document( definitions=[ ast.OperationDefinition( operation='query', name=None, variable_definitions=None, directives=[], selection_set=ast.SelectionSet( selections=[ ast.Field( alias=None, name=ast.Name(value=u'fieldWithObjectInput'), directives=[], selection_set=None, arguments=[ ast.Argument( name=ast.Name(value=u'input'), value=ast.ObjectValue( fields=[ ast.ObjectField( name=ast.Name(value=u'b'), value=ast.ListValue( values=[ ast.StringValue(value=u'A'), ast.NullValue(), ast.StringValue(value=u'C'), ], ), ), ast.ObjectField( name=ast.Name(value=u'c'), value=ast.StringValue(value=u'C'), ), ] ), ), ], ), ], ), ), ], )
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 value_node_from_pyvalue(val: object): if isinstance(val, str): val = val.replace('\\', '\\\\') value = eql_quote.quote_literal(val) return gql_ast.StringValue(value=value[1:-1]) elif isinstance(val, bool): return gql_ast.BooleanValue(value=bool(val)) elif isinstance(val, int): return gql_ast.IntValue(value=str(val)) elif isinstance(val, float): return gql_ast.FloatValue(value=str(val)) elif isinstance(val, list): return gql_ast.ListValue( values=[value_node_from_pyvalue(v) for v in val]) elif isinstance(val, dict): return gql_ast.ObjectValue(fields=[ gql_ast.ObjectField(name=n, value=value_node_from_pyvalue(v)) for n, v in val.items() ]) else: raise ValueError(f'unexpected constant type: {type(val)!r}')