예제 #1
0
def test_simple_non_null_type():
    body = '''
type Hello {
  world: String!
}'''

    doc = parse(body)
    loc = create_loc_fn(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=[],
                                    type=ast.NonNullType(type=ast.NamedType(
                                        name=ast.Name(value='String',
                                                      loc=loc(23, 29)),
                                        loc=loc(23, 29)),
                                                         loc=loc(23, 30)),
                                    directives=[],
                                    loc=loc(16, 30))
            ],
            loc=loc(1, 32))
    ],
                            loc=loc(1, 32))
    assert doc == expected
예제 #2
0
def test_parses_simple_interface():
    # type: () -> None
    body = """
interface Hello {
  world: String
}
"""
    loc = create_loc_fn(body)
    doc = parse(body)
    expected = ast.Document(
        definitions=[
            ast.InterfaceTypeDefinition(
                name=ast.Name(value="Hello", loc=loc(11, 16)),
                directives=[],
                fields=[
                    ast.FieldDefinition(
                        name=ast.Name(value="world", loc=loc(21, 26)),
                        arguments=[],
                        type=ast.NamedType(
                            name=ast.Name(value="String", loc=loc(28, 34)),
                            loc=loc(28, 34),
                        ),
                        directives=[],
                        loc=loc(21, 34),
                    )
                ],
                loc=loc(1, 36),
            )
        ],
        loc=loc(1, 37),
    )

    assert doc == expected
예제 #3
0
def test_parses_simple_extension():
    body = '''
extend type Hello {
  world: String
}'''
    doc = parse(body)
    loc = create_loc_fn(body)

    expected = ast.Document(definitions=[
        ast.TypeExtensionDefinition(definition=ast.ObjectTypeDefinition(
            name=ast.Name(value='Hello', loc=loc(13, 18)),
            interfaces=[],
            directives=[],
            fields=[
                ast.FieldDefinition(name=ast.Name(value='world',
                                                  loc=loc(23, 28)),
                                    arguments=[],
                                    type=ast.NamedType(name=ast.Name(
                                        value='String', loc=loc(30, 36)),
                                                       loc=loc(30, 36)),
                                    directives=[],
                                    loc=loc(23, 36))
            ],
            loc=loc(8, 38)),
                                    loc=loc(1, 38))
    ],
                            loc=loc(1, 38))

    assert doc == expected
예제 #4
0
def test_parses_simple_interface():
    body = '''
interface Hello {
  world: String
}
'''
    loc = create_loc_fn(body)
    doc = parse(body)
    expected = ast.Document(definitions=[
        ast.InterfaceTypeDefinition(
            name=ast.Name(value='Hello', loc=loc(11, 16)),
            directives=[],
            fields=[
                ast.FieldDefinition(name=ast.Name(value='world',
                                                  loc=loc(21, 26)),
                                    arguments=[],
                                    type=ast.NamedType(name=ast.Name(
                                        value='String', loc=loc(28, 34)),
                                                       loc=loc(28, 34)),
                                    directives=[],
                                    loc=loc(21, 34))
            ],
            loc=loc(1, 36))
    ],
                            loc=loc(1, 37))

    assert doc == expected
예제 #5
0
def test_parses_simple_type():
    # type: () -> None
    body = """
type Hello {
  world: String
}"""

    doc = parse(body)
    loc = create_loc_fn(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=[],
                        type=ast.NamedType(
                            name=ast.Name(value="String", loc=loc(23, 29)),
                            loc=loc(23, 29),
                        ),
                        directives=[],
                        loc=loc(16, 29),
                    )
                ],
                loc=loc(1, 31),
            )
        ],
        loc=loc(1, 31),
    )
    assert doc == expected
예제 #6
0
def test_parses_simple_field_with_list_arg():
    body = '''
type Hello {
  world(things: [String]): 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='things', loc=loc(22, 28)),
                            type=ast.ListType(type=ast.NamedType(
                                name=ast.Name(value='String', loc=loc(31, 37)),
                                loc=loc(31, 37)),
                                              loc=loc(30, 38)),
                            default_value=None,
                            loc=loc(22, 38))
                    ],
                    type=ast.NamedType(name=ast.Name(value='String',
                                                     loc=loc(41, 47)),
                                       loc=loc(41, 47)),
                    loc=loc(16, 47))
            ],
            loc=loc(1, 49))
    ],
                            loc=loc(1, 49))
    assert doc == expected
예제 #7
0
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
예제 #8
0
def test_parses_simple_field_with_two_args():
    # type: () -> None
    body = """
type Hello {
  world(argOne: Boolean, argTwo: Int): 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="argOne", loc=loc(22, 28)),
                                type=ast.NamedType(
                                    name=ast.Name(value="Boolean",
                                                  loc=loc(30, 37)),
                                    loc=loc(30, 37),
                                ),
                                default_value=None,
                                directives=[],
                                loc=loc(22, 37),
                            ),
                            ast.InputValueDefinition(
                                name=ast.Name(value="argTwo", loc=loc(39, 45)),
                                type=ast.NamedType(
                                    name=ast.Name(value="Int", loc=loc(47,
                                                                       50)),
                                    loc=loc(47, 50),
                                ),
                                default_value=None,
                                directives=[],
                                loc=loc(39, 50),
                            ),
                        ],
                        type=ast.NamedType(
                            name=ast.Name(value="String", loc=loc(53, 59)),
                            loc=loc(53, 59),
                        ),
                        directives=[],
                        loc=loc(16, 59),
                    )
                ],
                loc=loc(1, 61),
            )
        ],
        loc=loc(1, 61),
    )
    assert doc == expected
예제 #9
0
def _add_edge_field(source_type_node, sink_type_name, source_field_name,
                    sink_field_name, edge_name, direction):
    """Add one direction of the specified edge as a field of the source type.

    Args:
        source_type_node: (Interface/Object)TypeDefinition, where a new field representing
                          one direction of the edge will be added. It is modified by this
                          function
        sink_type_name: str, name of the type that the edge leads to
        source_field_name: str, name of the source side field that will be stitched
        sink_field_name: str, name of the sink side field that will be stitched
        edge_name: str, name of the edge that will be used to name the new field
        direction: str, either OUTBOUND_EDGE_DIRECTION or INBOUND_EDGE_DIRECTION ('out'
                   or 'in')

    Raises:
        - SchemaNameConflictError if the new cross-schema edge name causes a name conflict with
          existing fields, or fields created by previous cross-schema edges
    """
    type_fields = source_type_node.fields

    if direction not in (OUTBOUND_EDGE_DIRECTION, INBOUND_EDGE_DIRECTION):
        raise AssertionError(
            u'Input "direction" must be either "{}" or "{}".'.format(
                OUTBOUND_EDGE_DIRECTION, INBOUND_EDGE_DIRECTION))
    new_edge_field_name = direction + '_' + edge_name

    # Error if new edge causes a field name clash
    if any(field.name.value == new_edge_field_name for field in type_fields):
        raise SchemaNameConflictError(
            u'New field "{}" under type "{}" created by the {}bound field of edge named '
            u'"{}" clashes with an existing field of the same name. Consider changing the '
            u'name of your edge to avoid name conflicts.'.format(
                new_edge_field_name, source_type_node.name.value, direction,
                edge_name))

    new_edge_field_node = ast_types.FieldDefinition(
        name=ast_types.Name(value=new_edge_field_name),
        arguments=[],
        type=ast_types.ListType(type=ast_types.NamedType(
            name=ast_types.Name(value=sink_type_name), ), ),
        directives=[
            _build_stitch_directive(source_field_name, sink_field_name),
        ],
    )

    type_fields.append(new_edge_field_node)
예제 #10
0
def test_parses_simple_field_with_list_arg():
    # type: () -> None
    body = """
type Hello {
  world(things: [String]): 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="things", loc=loc(22, 28)),
                                type=ast.ListType(
                                    type=ast.NamedType(
                                        name=ast.Name(value="String",
                                                      loc=loc(31, 37)),
                                        loc=loc(31, 37),
                                    ),
                                    loc=loc(30, 38),
                                ),
                                default_value=None,
                                directives=[],
                                loc=loc(22, 38),
                            )
                        ],
                        type=ast.NamedType(
                            name=ast.Name(value="String", loc=loc(41, 47)),
                            loc=loc(41, 47),
                        ),
                        directives=[],
                        loc=loc(16, 47),
                    )
                ],
                loc=loc(1, 49),
            )
        ],
        loc=loc(1, 49),
    )
    assert doc == expected
예제 #11
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
예제 #12
0
def test_parses_simple_field_with_two_args():
    body = '''
type Hello {
  world(argOne: Boolean, argTwo: Int): 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='argOne', loc=loc(22, 28)),
                            type=ast.NamedType(name=ast.Name(value='Boolean',
                                                             loc=loc(30, 37)),
                                               loc=loc(30, 37)),
                            default_value=None,
                            directives=[],
                            loc=loc(22, 37)),
                        ast.InputValueDefinition(
                            name=ast.Name(value='argTwo', loc=loc(39, 45)),
                            type=ast.NamedType(name=ast.Name(value='Int',
                                                             loc=loc(47, 50)),
                                               loc=loc(47, 50)),
                            default_value=None,
                            directives=[],
                            loc=loc(39, 50))
                    ],
                    type=ast.NamedType(name=ast.Name(value='String',
                                                     loc=loc(53, 59)),
                                       loc=loc(53, 59)),
                    directives=[],
                    loc=loc(16, 59))
            ],
            loc=loc(1, 61))
    ],
                            loc=loc(1, 61))
    assert doc == expected
예제 #13
0
def test_parses_simple_extension():
    # type: () -> None
    body = """
extend type Hello {
  world: String
}"""
    doc = parse(body)
    loc = create_loc_fn(body)

    expected = ast.Document(
        definitions=[
            ast.TypeExtensionDefinition(
                definition=ast.ObjectTypeDefinition(
                    name=ast.Name(value="Hello", loc=loc(13, 18)),
                    interfaces=[],
                    directives=[],
                    fields=[
                        ast.FieldDefinition(
                            name=ast.Name(value="world", loc=loc(23, 28)),
                            arguments=[],
                            type=ast.NamedType(
                                name=ast.Name(value="String", loc=loc(30, 36)),
                                loc=loc(30, 36),
                            ),
                            directives=[],
                            loc=loc(23, 36),
                        )
                    ],
                    loc=loc(8, 38),
                ),
                loc=loc(1, 38),
            )
        ],
        loc=loc(1, 38),
    )

    assert doc == expected