本文整理汇总了Python中graphql.error.format_error方法的典型用法代码示例。如果您正苦于以下问题:Python error.format_error方法的具体用法?Python error.format_error怎么用?Python error.format_error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphql.error
的用法示例。
在下文中一共展示了error.format_error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def check(doc, data, expected):
# type: (str, Union[NullingData, ThrowingData], Dict[str, Any]) -> None
ast = parse(doc)
response = execute(schema, ast, data)
if response.errors:
result = {
"data": response.data,
"errors": [format_error(e) for e in response.errors],
}
if result["errors"] != expected["errors"]:
assert result["data"] == expected["data"]
# Sometimes the fields resolves asynchronously, so
# we need to check that the errors are the same, but might be
# raised in a different order.
assert sorted(result["errors"], key=order_errors) == sorted(
expected["errors"], key=order_errors
)
else:
assert result == expected
else:
result = {"data": response.data}
assert result == expected
示例2: check
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def check(
doc, # type: str
expected, # type: Union[Dict[str, Dict[str, None]], Dict[str, Dict[str, str]]]
args=None, # type: Any
):
# type: (...) -> None
ast = parse(doc)
response = execute(schema, ast, variable_values=args)
if response.errors:
result = {
"data": response.data,
"errors": [format_error(e) for e in response.errors],
}
else:
result = {"data": response.data}
assert result == expected
# Handles objects and nullability
示例3: test_errors_on_deep_nested_errors_and_with_many_errors
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_errors_on_deep_nested_errors_and_with_many_errors(self):
# type: () -> None
nested_doc = """
query q($input: TestNestedInputObject) {
fieldWithNestedObjectInput(input: $input)
}
"""
params = {"input": {"na": {"a": "foo"}}}
with raises(GraphQLError) as excinfo:
check(nested_doc, {}, params)
assert format_error(excinfo.value) == {
"locations": [{"column": 19, "line": 2}],
"message": 'Variable "$input" got invalid value {}.\n'
'In field "na": In field "c": Expected "String!", found null.\n'
'In field "nb": Expected "String!", found null.'.format(
stringify(params["input"])
),
}
示例4: test_does_not_allow_lists_of_non_nulls_to_contain_null
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_does_not_allow_lists_of_non_nulls_to_contain_null():
# type: () -> None
doc = """
query q($input: [String!]) {
listNN(input: $input)
}
"""
params = {"input": ["A", None, "B"]}
with raises(GraphQLError) as excinfo:
check(doc, {}, params)
assert format_error(excinfo.value) == {
"locations": [{"column": 13, "line": 2}],
"message": 'Variable "$input" got invalid value {}.\n'
'In element #1: Expected "String!", found null.'.format(
stringify(params["input"])
),
}
示例5: test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null():
# type: () -> None
doc = """
query q($input: [String!]!) {
nnListNN(input: $input)
}
"""
params = {"input": ["A", None, "B"]}
with raises(GraphQLError) as excinfo:
check(doc, {}, params)
assert format_error(excinfo.value) == {
"locations": [{"column": 13, "line": 2}],
"message": 'Variable "$input" got invalid value {}.\n'
'In element #1: Expected "String!", found null.'.format(
stringify(params["input"])
),
}
示例6: test_does_not_allow_invalid_types_to_be_used_as_values
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_does_not_allow_invalid_types_to_be_used_as_values():
# type: () -> None
doc = """
query q($input: TestType!) {
fieldWithObjectInput(input: $input)
}
"""
params = {"input": {"list": ["A", "B"]}}
with raises(GraphQLError) as excinfo:
check(doc, {}, params)
assert format_error(excinfo.value) == {
"locations": [{"column": 13, "line": 2}],
"message": 'Variable "$input" expected value of type "TestType!" which cannot be used as an input type.',
}
示例7: test_does_not_allow_unknown_types_to_be_used_as_values
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_does_not_allow_unknown_types_to_be_used_as_values():
# type: () -> None
doc = """
query q($input: UnknownType!) {
fieldWithObjectInput(input: $input)
}
"""
params = {"input": "whoknows"}
with raises(GraphQLError) as excinfo:
check(doc, {}, params)
assert format_error(excinfo.value) == {
"locations": [{"column": 13, "line": 2}],
"message": 'Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.',
}
# noinspection PyMethodMayBeStatic
示例8: default_format_error
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def default_format_error(error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {"message": str(error)}
示例9: format_error
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def format_error(error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {"message": six.text_type(error)}
示例10: format_error
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def format_error(error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {'message': six.text_type(error)}
示例11: error_format
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def error_format(exception: Exception) -> List[Dict[str, Any]]:
if isinstance(exception, ExecutionError):
return [{"message": e} for e in exception.errors]
elif isinstance(exception, GraphQLError):
return [format_graphql_error(exception)]
elif isinstance(exception, web.HTTPError):
return [{"message": exception.log_message}]
else:
return [{"message": "Unknown server error"}]
示例12: format_error
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def format_error(error: Union[GraphQLError, GraphQLSyntaxError]) -> Dict[str, Any]:
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {"message": str(error)}
示例13: test_asyncio_py35_executor_with_error
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_asyncio_py35_executor_with_error():
ast = parse("query Example { a, b }")
async def resolver(context, *_):
await asyncio.sleep(0.001)
return "hey"
async def resolver_2(context, *_):
await asyncio.sleep(0.003)
raise Exception("resolver_2 failed!")
Type = GraphQLObjectType(
"Type",
{
"a": GraphQLField(GraphQLString, resolver=resolver),
"b": GraphQLField(GraphQLString, resolver=resolver_2),
},
)
result = execute(GraphQLSchema(Type), ast, executor=AsyncioExecutor())
formatted_errors = list(map(format_error, result.errors))
assert formatted_errors == [
{
"locations": [{"line": 1, "column": 20}],
"path": ["b"],
"message": "resolver_2 failed!",
}
]
assert result.data == {"a": "hey", "b": None}
示例14: expect_invalid
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def expect_invalid(schema, rules, query, expected_errors, sort_list=True):
errors = validate(schema, parse(query), rules)
assert errors, "Should not validate"
for error in expected_errors:
error["locations"] = [
{"line": loc.line, "column": loc.column} for loc in error["locations"]
]
if sort_list:
assert sort_lists(list(map(format_error, errors))) == sort_lists(
expected_errors
)
else:
assert list(map(format_error, errors)) == expected_errors
示例15: test_cannot_use_client_schema_for_general_execution
# 需要导入模块: from graphql import error [as 别名]
# 或者: from graphql.error import format_error [as 别名]
def test_cannot_use_client_schema_for_general_execution():
customScalar = GraphQLScalarType(name="CustomScalar", serialize=lambda: None)
schema = GraphQLSchema(
query=GraphQLObjectType(
name="Query",
fields={
"foo": GraphQLField(
GraphQLString,
args=OrderedDict(
[
("custom1", GraphQLArgument(customScalar)),
("custom2", GraphQLArgument(customScalar)),
]
),
)
},
)
)
introspection = graphql(schema, introspection_query)
client_schema = build_client_schema(introspection.data)
class data:
foo = "bar"
result = graphql(
client_schema,
"query NoNo($v: CustomScalar) { foo(custom1: 123, custom2: $v) }",
data,
{"v": "baz"},
)
assert result.data == {"foo": None}
assert [format_error(e) for e in result.errors] == [
{
"locations": [{"column": 32, "line": 1}],
"message": "Client Schema cannot be used for execution.",
"path": ["foo"],
}
]