Beispiel #1
0
def test_parse_creates_ast():
    source = Source("""{
  node(id: 4) {
    id,
    name
  }
}
""")
    result = parse(source)

    assert result == \
           ast.Document(
               loc={'start': 0, 'end': 41, 'source': source},
               definitions=
               [ast.OperationDefinition(
                   loc={'start': 0, 'end': 40, 'source': source},
                   operation='query',
                   name=None,
                   variable_definitions=None,
                   directives=[],
                   selection_set=ast.SelectionSet(
                       loc={'start': 0, 'end': 40, 'source': source},
                       selections=
                       [ast.Field(
                           loc={'start': 4, 'end': 38, 'source': source},
                           alias=None,
                           name=ast.Name(
                               loc={'start': 4, 'end': 8, 'source': source},
                               value='node'),
                           arguments=[ast.Argument(
                               name=ast.Name(loc={'start': 9, 'end': 11, 'source': source},
                                             value='id'),
                               value=ast.IntValue(
                                   loc={'start': 13, 'end': 14, 'source': source},
                                   value='4'),
                               loc={'start': 9, 'end': 14, 'source': source})],
                           directives=[],
                           selection_set=ast.SelectionSet(
                               loc={'start': 16, 'end': 38, 'source': source},
                               selections=
                               [ast.Field(
                                   loc={'start': 22, 'end': 24, 'source': source},
                                   alias=None,
                                   name=ast.Name(
                                       loc={'start': 22, 'end': 24, 'source': source},
                                       value='id'),
                                   arguments=[],
                                   directives=[],
                                   selection_set=None),
                                ast.Field(
                                    loc={'start': 30, 'end': 34, 'source': source},
                                    alias=None,
                                    name=ast.Name(
                                        loc={'start': 30, 'end': 34, 'source': source},
                                        value='name'),
                                    arguments=[],
                                    directives=[],
                                    selection_set=None)]))]))])
Beispiel #2
0
def test_parse_creates_ast():
    source = Source("""{
  node(id: 4) {
    id,
    name
  }
}
""")
    result = parse(source)

    assert result == \
           ast.Document(
               loc=Loc(start=0, end=41, source=source),
               definitions=
               [ast.OperationDefinition(
                   loc=Loc(start=0, end=40, source=source),
                   operation='query',
                   name=None,
                   variable_definitions=None,
                   directives=[],
                   selection_set=ast.SelectionSet(
                       loc=Loc(start=0, end=40, source=source),
                       selections=
                       [ast.Field(
                           loc=Loc(start=4, end=38, source=source),
                           alias=None,
                           name=ast.Name(
                               loc=Loc(start=4, end=8, source=source),
                               value='node'),
                           arguments=[ast.Argument(
                               name=ast.Name(loc=Loc(start=9, end=11, source=source),
                                             value='id'),
                               value=ast.IntValue(
                                   loc=Loc(start=13, end=14, source=source),
                                   value='4'),
                               loc=Loc(start=9, end=14, source=source))],
                           directives=[],
                           selection_set=ast.SelectionSet(
                               loc=Loc(start=16, end=38, source=source),
                               selections=
                               [ast.Field(
                                   loc=Loc(start=22, end=24, source=source),
                                   alias=None,
                                   name=ast.Name(
                                       loc=Loc(start=22, end=24, source=source),
                                       value='id'),
                                   arguments=[],
                                   directives=[],
                                   selection_set=None),
                                   ast.Field(
                                       loc=Loc(start=30, end=34, source=source),
                                       alias=None,
                                       name=ast.Name(
                                           loc=Loc(start=30, end=34, source=source),
                                           value='name'),
                                       arguments=[],
                                       directives=[],
                                       selection_set=None)]))]))])
Beispiel #3
0
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'))
    ])
Beispiel #4
0
def test_converts_numeric_values_to_asts():
    assert ast_from_value(123) == ast.IntValue('123')
    assert ast_from_value(123.0) == ast.IntValue('123')
    assert ast_from_value(123.5) == ast.FloatValue('123.5')
    assert ast_from_value(1e4) == ast.IntValue('10000')
    assert ast_from_value(1e40) == ast.FloatValue('1e+40')