def test_when_type_is_interface_then_typename_field_is_unresolved(self):
        User = g.InterfaceType("User",
                               fields=(g.field("name", type=g.String), ))

        object_builder = g.create_object_builder(
            User(g.key("type", schema.typename_field()), ))

        error = pytest.raises(g.GraphError, lambda: object_builder({}))
        assert_that(str(error.value),
                    equal_to("Resolver missing for field type_name"))
def test_interface_type_is_converted_to_non_null_graphql_interface_type():
    graph_type = g.InterfaceType("Obj", fields=(
        g.field("value", type=g.String),
    ))

    assert_that(to_graphql_type(graph_type), is_graphql_non_null(
        is_graphql_interface_type(
            name="Obj",
            fields=is_mapping({
                "value": is_graphql_field(type=is_graphql_non_null(is_graphql_string)),
            }),
        ),
    ))
def test_fragments_can_be_on_more_specific_type():
    Animal = g.InterfaceType(
        "Animal",
        fields=(
            g.field("name", type=g.String),
        ),
    )

    Cat = g.ObjectType(
        "Cat",
        fields=(
            g.field("name", type=g.String),
            g.field("whisker_count", type=g.Int),
        ),
        interfaces=(Animal, ),
    )

    Root = g.ObjectType(
        "Root",
        (
            g.field("animal", type=Animal),
        ),
    )

    graphql_query = """
        query {
            animal {
                name
                ... on Cat {
                    whiskerCount
                }
            }
        }
    """

    object_query = _document_text_to_graph_query(graphql_query, query_type=Root, types=(Cat, ))

    assert_that(object_query, is_query(
        Root(
            g.key("animal", Root.fields.animal(
                g.key("name", Animal.fields.name()),
                g.key("whiskerCount", Cat.fields.whisker_count()),
            )),
        ),
    ))
def test_fragment_fields_are_updated_to_fields_for_element_type():
    Person = g.InterfaceType(
        "Person",
        fields=lambda: (
            g.field("name", type=g.String),
        ),
    )

    User = g.ObjectType(
        "User",
        fields=lambda: (
            g.field("name", type=g.String),
        ),
        interfaces=lambda: (Person, ),
    )

    Root = g.ObjectType(
        "Root",
        fields=lambda: (
            g.field("users", type=g.ListType(g.NullableType(User))),
        ),
    )

    graphql_query = """
        query {
            users {
                ... on Person {
                    name
                }
            }
        }
    """

    object_query = _document_text_to_graph_query(graphql_query, query_type=Root)

    assert_that(object_query, is_query(
        Root(
            g.key("users", Root.fields.users(
                g.key("name", User.fields.name()),
            )),
        ),
    ))
def test_object_type_interfaces_are_converted_to_graphql_interfaces():
    graph_interface_type = g.InterfaceType("Interface", fields=(
        g.field("value", type=g.String),
    ))

    graph_object_type = g.ObjectType(
        "Obj",
        fields=(
            g.field("value", type=g.String),
        ),
        interfaces=(graph_interface_type, ),
    )

    assert_that(to_graphql_type(graph_object_type), is_graphql_non_null(
        is_graphql_object_type(
            interfaces=contains_exactly(
                is_graphql_interface_type(name="Interface"),
            ),
        ),
    ))
Beispiel #6
0
def test_can_select_unions():
    Person = g.InterfaceType(
        "Person",
        fields=lambda: (g.field("name", type=g.String), ),
    )

    Author = g.ObjectType(
        "Author",
        fields=lambda: (
            g.field("name", type=g.String),
            g.field("books_written", type=g.Int),
        ),
        interfaces=lambda: (Person, ),
    )

    Reader = g.ObjectType(
        "Reader",
        fields=lambda: (
            g.field("name", type=g.String),
            g.field("books_read", type=g.Int),
        ),
        interfaces=lambda: (Person, ),
    )

    @g.resolver(g.ListType(Author))
    def resolve_author(graph, query):
        assert_that(
            query,
            is_query(
                g.ListType(Author)(
                    g.key("name", Author.fields.name()),
                    g.key("books_written", Author.fields.books_written()),
                )))
        return [
            g.Object(dict(name="<author 1>", books_written=1)),
            g.Object(dict(name="<author 2>", books_written=2)),
        ]

    @g.resolver(g.ListType(Reader))
    def resolve_reader(graph, query):
        assert_that(
            query,
            is_query(
                g.ListType(Reader)(
                    g.key("name", Reader.fields.name()),
                    g.key("books_read", Reader.fields.books_read()),
                )))
        return [
            g.Object(dict(name="<reader 3>", books_read=3)),
            g.Object(dict(name="<reader 4>", books_read=4)),
        ]

    resolvers = (
        unions.resolver,
        resolve_author,
        resolve_reader,
    )

    graph_definition = g.define_graph(resolvers)
    graph = graph_definition.create_graph({})

    query = unions.select(
        g.ListType(Person)(
            g.key("name", Person.fields.name()),
            g.key("books_read", Reader.fields.books_read()),
            g.key("books_written", Author.fields.books_written()),
        ),
        (
            # TODO: Make sure select is called
            (Author, lambda author_query: author_query),
            (Reader, lambda reader_query: reader_query),
        ),
        merge=flatten,
    )
    result = graph.resolve(query)

    assert_that(
        result,
        contains_exactly(
            has_attrs(name="<author 1>", books_written=1),
            has_attrs(name="<author 2>", books_written=2),
            has_attrs(name="<reader 3>", books_read=3),
            has_attrs(name="<reader 4>", books_read=4),
        ))