本文整理汇总了Python中graphql.format_error方法的典型用法代码示例。如果您正苦于以下问题:Python graphql.format_error方法的具体用法?Python graphql.format_error怎么用?Python graphql.format_error使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphql
的用法示例。
在下文中一共展示了graphql.format_error方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_error
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def test_parse_error(client):
with pytest.raises(Exception) as exc_info:
gql(
"""
qeury
"""
)
error = exc_info.value
assert isinstance(error, GraphQLError)
formatted_error = format_error(error)
assert formatted_error["locations"] == [{"column": 13, "line": 2}]
assert formatted_error["message"] == "Syntax Error: Unexpected Name 'qeury'."
示例2: encode_execution_results
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def encode_execution_results(
execution_results: List[Optional[ExecutionResult]],
format_error: Callable[[GraphQLError], Dict] = format_error_default,
is_batch: bool = False,
encode: Callable[[Dict], Any] = json_encode,
) -> ServerResponse:
"""Serialize the ExecutionResults.
This function takes the ExecutionResults that are returned by run_http_query()
and serializes them using JSON to produce an HTTP response.
If you set is_batch=True, then all ExecutionResults will be returned, otherwise only
the first one will be used. You can also pass a custom function that formats the
errors in the ExecutionResults, expecting a dictionary as result and another custom
function that is used to serialize the output.
Returns a ServerResponse tuple with the serialized response as the first item and
a status code of 200 or 400 in case any result was invalid as the second item.
"""
results = [
format_execution_result(execution_result, format_error)
for execution_result in execution_results
]
result, status_codes = zip(*results)
status_code = max(status_codes)
if not is_batch:
result = result[0]
return ServerResponse(encode(result), status_code)
示例3: format_execution_result
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def format_execution_result(
execution_result: Optional[ExecutionResult],
format_error: Optional[Callable[[GraphQLError], Dict]] = format_error_default,
) -> FormattedResult:
"""Format an execution result into a GraphQLResponse.
This converts the given execution result into a FormattedResult that contains
the ExecutionResult converted to a dictionary and an appropriate status code.
"""
status_code = 200
response: Optional[Dict[str, Any]] = None
if execution_result:
if execution_result.errors:
fe = [format_error(e) for e in execution_result.errors] # type: ignore
response = {"errors": fe}
if execution_result.errors and any(
not getattr(e, "path", None) for e in execution_result.errors
):
status_code = 400
else:
response["data"] = execution_result.data
else:
response = {"data": execution_result.data}
return FormattedResult(response, status_code)
示例4: __format_error
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def __format_error(self, error):
if isinstance(error, GraphQLError):
return format_graphql_error(error)
return {'message': str(error)}
示例5: _raise_non_graphql_exceptions
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def _raise_non_graphql_exceptions(error):
if isinstance(error, graphql.GraphQLError):
return graphql.format_error(error)
raise error
示例6: __init__
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def __init__(self, schema=None, format_error=None, **execute_options):
super().__init__(schema or unchained.graphene_bundle.root_schema,
format_error=format_error or _raise_non_graphql_exceptions,
**execute_options)
示例7: execution_result_to_dict
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def execution_result_to_dict(self, execution_result):
result = OrderedDict()
if execution_result.data:
result['data'] = execution_result.data
if execution_result.errors:
result['errors'] = [format_error(error)
for error in execution_result.errors]
return result
示例8: graphql_view
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import format_error [as 别名]
def graphql_view(request):
payload = await request.json()
response = await schema.execute(payload.get('query', ''), return_promise=True)
data = {}
if response.errors:
data['errors'] = [format_error(e) for e in response.errors]
if response.data:
data['data'] = response.data
jsondata = json.dumps(data,)
return web.Response(text=jsondata, headers={'Content-Type': 'application/json'})