def test_parses_simple_field_with_arg_with_default_value():
    body = '''
type Hello {
  world(flag: Boolean = true): String
}'''
    loc = create_loc_fn(body)
    doc = parse(body)
    expected = ast.Document(definitions=[
        ast.ObjectTypeDefinition(
            name=ast.Name(value='Hello', loc=loc(6, 11)),
            interfaces=[],
            fields=[
                ast.FieldDefinition(
                    name=ast.Name(value='world', loc=loc(16, 21)),
                    arguments=[
                        ast.InputValueDefinition(
                            name=ast.Name(value='flag', loc=loc(22, 26)),
                            type=ast.NamedType(name=ast.Name(value='Boolean',
                                                             loc=loc(28, 35)),
                                               loc=loc(28, 35)),
                            default_value=ast.BooleanValue(value=True,
                                                           loc=loc(38, 42)),
                            loc=loc(22, 42))
                    ],
                    type=ast.NamedType(name=ast.Name(value='String',
                                                     loc=loc(45, 51)),
                                       loc=loc(45, 51)),
                    loc=loc(16, 51))
            ],
            loc=loc(1, 53))
    ],
                            loc=loc(1, 53))

    assert doc == expected
Esempio n. 2
0
def get_ast_value(value):
    if isinstance(value, ast.Node):
        return value
    if isinstance(value, six.string_types):
        return ast.StringValue(value=value)
    elif isinstance(value, bool):
        return ast.BooleanValue(value=value)
    elif isinstance(value, (float, decimal.Decimal)):
        return ast.FloatValue(value=value)
    elif isinstance(value, int):
        return ast.IntValue(value=value)
    return None
Esempio n. 3
0
def test_parses_simple_field_with_arg_with_default_value():
    # type: () -> None
    body = """
type Hello {
  world(flag: Boolean = true): String
}"""
    loc = create_loc_fn(body)
    doc = parse(body)
    expected = ast.Document(
        definitions=[
            ast.ObjectTypeDefinition(
                name=ast.Name(value="Hello", loc=loc(6, 11)),
                interfaces=[],
                directives=[],
                fields=[
                    ast.FieldDefinition(
                        name=ast.Name(value="world", loc=loc(16, 21)),
                        arguments=[
                            ast.InputValueDefinition(
                                name=ast.Name(value="flag", loc=loc(22, 26)),
                                type=ast.NamedType(
                                    name=ast.Name(value="Boolean",
                                                  loc=loc(28, 35)),
                                    loc=loc(28, 35),
                                ),
                                default_value=ast.BooleanValue(value=True,
                                                               loc=loc(38,
                                                                       42)),
                                directives=[],
                                loc=loc(22, 42),
                            )
                        ],
                        type=ast.NamedType(
                            name=ast.Name(value="String", loc=loc(45, 51)),
                            loc=loc(45, 51),
                        ),
                        directives=[],
                        loc=loc(16, 51),
                    )
                ],
                loc=loc(1, 53),
            )
        ],
        loc=loc(1, 53),
    )

    assert doc == expected
Esempio n. 4
0
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}')
Esempio n. 5
0
def test_converts_boolean_values_to_asts():
    assert ast_from_value(True) == ast.BooleanValue(True)
    assert ast_from_value(False) == ast.BooleanValue(False)