Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"}}
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"}
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"}
def test_attempt_bind_object_type_to_undefined_type_raises_error(schema):
query = ObjectType("Test")
with pytest.raises(ValueError):
query.bind_to_schema(schema)
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,
}
def test_value_error_is_raised_if_field_decorator_was_used_without_argument():
query = ObjectType("Query")
with pytest.raises(ValueError):
query.field(lambda *_: "World")
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)
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]:
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)
"""
)
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)