本文整理匯總了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'})