async def _execute(): result = execute(schema, ensure_document(doc), **kwargs) if isawaitable(result): return await result elif isinstance(result, Future): return result.result(timeout=2) return result
async def test_get_directive_arguments_missing(mocker): CustomDirective = Directive( "custom", ["FIELD"], [Argument("a", String), Argument("b", Int)]) resolver = mocker.Mock(return_value=42) execute( Schema(test_type, directives=[CustomDirective]), parse("{ a }"), initial_value=_obj(a=resolver), ) (_, info), _ = resolver.call_args assert info.get_directive_arguments("custom") is None
async def test_get_directive_arguments_unknown(mocker): CustomDirective = Directive( "custom", ["FIELD"], [Argument("a", String), Argument("b", Int)]) resolver = mocker.Mock(return_value=42) execute( Schema(test_type, directives=[CustomDirective]), parse('{ a @custom(a: "foo", b: 42) }'), initial_value=_obj(a=resolver), ) (_, info), _ = resolver.call_args with pytest.raises(KeyError): info.get_directive_arguments("foo")
async def test_get_directive_arguments_known_with_variables(mocker): CustomDirective = Directive( "custom", ["FIELD"], [Argument("a", String), Argument("b", Int)]) resolver = mocker.Mock(return_value=42) execute( Schema(test_type, directives=[CustomDirective]), parse('query ($b: Int!) { a @custom(a: "foo", b: $b) }'), initial_value=_obj(a=resolver), variables={"b": 42}, ) (_, info), _ = resolver.call_args assert info.get_directive_arguments("custom") == { "a": "foo", "b": 42, }
def assert_sync_execution(schema: Schema, doc: Union[Document, str], expected_data: Any = None, expected_errors: Optional[ List[ExpectedError]] = None, expected_exc: Optional[ExpectedExcDef] = None, **kwargs: Any) -> None: doc = ensure_document(doc) if isinstance(expected_exc, tuple): expected_exc, expected_msg = expected_exc else: expected_exc, expected_msg = expected_exc, None if expected_exc is not None: with pytest.raises(expected_exc) as exc_info: execute(schema, doc, **kwargs) if expected_msg: assert str(exc_info.value) == expected_msg else: result = execute(schema, doc, **kwargs) assert not isawaitable(result) and isinstance(result, GraphQLResult) assert_execution_result(result, expected_data, expected_errors)
async def test_result_is_ordered_according_to_query(): """ Check that deep iteration order of keys in result corresponds to order of appearance in query accounting for fragment use """ data, _ = execute(_LIBRARY_SCHEMA, parse(_LIBRARY_QUERY)) def _extract_keys_in_order(d): if not isinstance(d, dict): return None keys = [] for key, value in d.items(): if isinstance(value, dict): keys.append((key, _extract_keys_in_order(value))) elif isinstance(value, list): keys.append((key, [_extract_keys_in_order(i) for i in value])) else: keys.append((key, None)) return keys assert _extract_keys_in_order(data) == [ ( "feed", [ [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], [("id", None), ("title", None)], ], ), ( "article", [ ("id", None), ("isPublished", None), ("title", None), ("body", None), ( "author", [ ("id", None), ("name", None), ( "pic", [("url", None), ("width", None), ("height", None)], ), ( "recentArticle", [ ("id", None), ("isPublished", None), ("title", None), ("body", None), ("keywords", [None, None, None, None, None]), ], ), ], ), ], ), ]