How to use the ariadne.ObjectType function in ariadne

To help you get started, we’ve selected a few ariadne examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mirumee / ariadne / tests / test_modularization.py View on Github external
def test_different_types_resolver_maps_are_merged_into_executable_schema():
    type_defs = """
        type Query {
            user: User
        }

        type User {
            username: String
        }
    """

    query = QueryType()
    query.set_field("user", lambda *_: Mock(first_name="Joe"))

    user = ObjectType("User")
    user.set_alias("username", "first_name")

    schema = make_executable_schema(type_defs, [query, user])

    result = graphql_sync(schema, "{ user { username } }")
    assert result.errors is None
    assert result.data == {"user": {"username": "Joe"}}
github mirumee / ariadne / tests / test_objects.py View on Github external
def test_field_resolver_can_be_set_using_setter(schema):
    query = ObjectType("Query")
    query.set_field("hello", lambda *_: "World")
    query.bind_to_schema(schema)

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": "World"}
github mirumee / ariadne / tests / test_objects.py View on Github external
def test_set_alias_method_creates_resolver_for_specified_attribute(schema):
    query = ObjectType("Query")
    query.set_alias("hello", "test")
    query.bind_to_schema(schema)

    result = graphql_sync(schema, "{ hello }", root_value={"test": "World"})
    assert result.errors is None
    assert result.data == {"hello": "World"}
github mirumee / ariadne / tests / test_objects.py View on Github external
def test_attempt_bind_object_type_to_undefined_type_raises_error(schema):
    query = ObjectType("Test")
    with pytest.raises(ValueError):
        query.bind_to_schema(schema)
github mirumee / ariadne / tests / test_fallback_resolvers.py View on Github external
def test_default_fallback_is_not_replacing_already_set_resolvers(schema):
    resolvers_map = ObjectType("Query")
    resolvers_map.set_field("hello", lambda *_: False)
    resolvers_map.set_field("snake_case", lambda *_: False)
    resolvers_map.bind_to_schema(schema)
    fallback_resolvers.bind_to_schema(schema)
    query_root = {"hello": True, "snake_case": True, "camel": True, "camel_case": True}
    result = graphql_sync(schema, query, root_value=query_root)
    assert result.data == {
        "hello": False,
        "snake_case": False,
        "Camel": None,
        "camelCase": None,
    }
github mirumee / ariadne / tests / test_objects.py View on Github external
def test_value_error_is_raised_if_field_decorator_was_used_without_argument():
    query = ObjectType("Query")
    with pytest.raises(ValueError):
        query.field(lambda *_: "World")
github mirumee / ariadne / tests / test_objects.py View on Github external
def test_attempt_bind_object_type_field_to_undefined_field_raises_error(schema):
    query = ObjectType("Query")
    query.set_alias("user", "_")
    with pytest.raises(ValueError):
        query.bind_to_schema(schema)
github rafalp / Misago / misago / graphql / types / thread.py View on Github external
from typing import Optional, cast

from ariadne import ObjectType
from graphql import GraphQLResolveInfo

from ...loaders import load_category, load_post, load_user
from ...types import Category, Post, Thread, User


thread_type = ObjectType("Thread")

thread_type.set_alias("starterName", "starter_name")
thread_type.set_alias("lastPosterName", "last_poster_name")
thread_type.set_alias("startedAt", "started_at")
thread_type.set_alias("lastPostedAt", "last_posted_at")
thread_type.set_alias("isClosed", "is_closed")


@thread_type.field("category")
async def resolve_category(obj: Thread, info: GraphQLResolveInfo) -> Category:
    category = await load_category(info.context, obj.category_id)
    return cast(Category, category)


@thread_type.field("firstPost")
async def resolve_first_post(obj: Thread, info: GraphQLResolveInfo) -> Optional[Post]:
github rafalp / Misago / misago / graphql / types / category.py View on Github external
from typing import Optional, List

from ariadne import ObjectType
from graphql import GraphQLResolveInfo

from ...loaders import load_category, load_category_children
from ...types import Category


category_type = ObjectType("Category")


@category_type.field("parent")
async def resolve_parent(
    category: Category, info: GraphQLResolveInfo
) -> Optional[Category]:
    if category.parent_id:
        return await load_category(info.context, category.parent_id)
    return None


@category_type.field("children")
async def resolve_children(
    category: Category, info: GraphQLResolveInfo
) -> List[Category]:
    return await load_category_children(info.context, category.id)
github mirumee / graphql-workshop / 02-object-types-final / example.py View on Github external
"""
)

query = QueryType()  # our Query type


@query.field("books")  # Query.books
def resolve_books(*_):
    return [
        {"title": "The Color of Magic", "year": 1983},
        {"title": "The Light Fantastic", "year": 1986},
        {"title": "Equal Rites", "year": 1987},
    ]


book = ObjectType("Book")


@book.field("age")
def resolve_book_age(book, *_):
    return 2019 - book["year"]


# Create an executable GraphQL schema
schema = make_executable_schema(type_defs, [query, book])

# Create the ASGI app
app = GraphQL(schema, debug=True)