|
5 | 5 | using Microsoft.AspNetCore.Http; |
6 | 6 | using Microsoft.AspNetCore.Routing; |
7 | 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | +using HotChocolate.AspNetCore.Instrumentation; |
8 | 9 | using HotChocolate.Buffers; |
9 | 10 | using HotChocolate.Execution; |
10 | 11 | using HotChocolate.Language; |
@@ -61,75 +62,81 @@ await Results.Problem( |
61 | 62 | .Get(schemaName) |
62 | 63 | .ExecutorProxy; |
63 | 64 | var session = await proxy.GetOrCreateSessionAsync(context.RequestAborted); |
| 65 | + var requestKind = HttpMethods.IsGet(context.Request.Method) |
| 66 | + ? HttpRequestKind.HttpGet |
| 67 | + : HttpRequestKind.HttpPost; |
64 | 68 |
|
65 | | - using var variableBuffer = new PooledArrayWriter(); |
66 | | - using var variables = await BuildVariablesAsync( |
67 | | - endpointDescriptor, |
68 | | - context, |
69 | | - variableBuffer, |
70 | | - cancellationToken); |
| 69 | + using (session.DiagnosticEvents.ExecuteHttpRequest(context, requestKind)) |
| 70 | + { |
| 71 | + using var variableBuffer = new PooledArrayWriter(); |
| 72 | + using var variables = await BuildVariablesAsync( |
| 73 | + endpointDescriptor, |
| 74 | + context, |
| 75 | + variableBuffer, |
| 76 | + cancellationToken); |
71 | 77 |
|
72 | | - var requestBuilder = OperationRequestBuilder.New() |
73 | | - .SetDocument(endpointDescriptor.Document) |
74 | | - .SetErrorHandlingMode(ErrorHandlingMode.Propagate) |
75 | | - .SetVariableValues(variables); |
| 78 | + var requestBuilder = OperationRequestBuilder.New() |
| 79 | + .SetDocument(endpointDescriptor.Document) |
| 80 | + .SetErrorHandlingMode(ErrorHandlingMode.Propagate) |
| 81 | + .SetVariableValues(variables); |
76 | 82 |
|
77 | | - await session.OnCreateAsync(context, requestBuilder, cancellationToken); |
| 83 | + await session.OnCreateAsync(context, requestBuilder, cancellationToken); |
78 | 84 |
|
79 | | - var executionResult = await session.ExecuteAsync( |
80 | | - requestBuilder.Build(), |
81 | | - cancellationToken).ConfigureAwait(false); |
| 85 | + var executionResult = await session.ExecuteAsync( |
| 86 | + requestBuilder.Build(), |
| 87 | + cancellationToken).ConfigureAwait(false); |
82 | 88 |
|
83 | | - // If the request was canceled, we do not attempt to write a response. |
84 | | - if (cancellationToken.IsCancellationRequested) |
85 | | - { |
86 | | - return; |
87 | | - } |
| 89 | + // If the request was canceled, we do not attempt to write a response. |
| 90 | + if (cancellationToken.IsCancellationRequested) |
| 91 | + { |
| 92 | + return; |
| 93 | + } |
88 | 94 |
|
89 | | - // If we do not have an operation result, something went wrong, and we return HTTP 500. |
90 | | - if (executionResult is not OperationResult operationResult) |
91 | | - { |
| 95 | + // If we do not have an operation result, something went wrong, and we return HTTP 500. |
| 96 | + if (executionResult is not OperationResult operationResult) |
| 97 | + { |
92 | 98 | #if NET9_0_OR_GREATER |
93 | | - await Results.InternalServerError().ExecuteAsync(context); |
| 99 | + await Results.InternalServerError().ExecuteAsync(context); |
94 | 100 | #else |
95 | | - await Results.StatusCode(500).ExecuteAsync(context); |
| 101 | + await Results.StatusCode(500).ExecuteAsync(context); |
96 | 102 | #endif |
97 | | - return; |
98 | | - } |
99 | | - |
100 | | - // If the request had validation errors or execution didn't start, we return HTTP 400. |
101 | | - if (operationResult.ContextData.ContainsKey(ExecutionContextData.ValidationErrors) |
102 | | - || !operationResult.Data.HasValue) |
103 | | - { |
104 | | - var firstErrorMessage = operationResult.Errors.FirstOrDefault()?.Message; |
| 103 | + return; |
| 104 | + } |
105 | 105 |
|
106 | | - if (!string.IsNullOrEmpty(firstErrorMessage)) |
| 106 | + // If the request had validation errors or execution didn't start, we return HTTP 400. |
| 107 | + if (operationResult.ContextData.ContainsKey(ExecutionContextData.ValidationErrors) |
| 108 | + || !operationResult.Data.HasValue) |
107 | 109 | { |
108 | | - await Results.Problem( |
109 | | - detail: firstErrorMessage, |
110 | | - statusCode: StatusCodes.Status400BadRequest).ExecuteAsync(context); |
| 110 | + var firstErrorMessage = operationResult.Errors.FirstOrDefault()?.Message; |
| 111 | + |
| 112 | + if (!string.IsNullOrEmpty(firstErrorMessage)) |
| 113 | + { |
| 114 | + await Results.Problem( |
| 115 | + detail: firstErrorMessage, |
| 116 | + statusCode: StatusCodes.Status400BadRequest).ExecuteAsync(context); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + await Results.BadRequest().ExecuteAsync(context); |
| 121 | + } |
| 122 | + |
| 123 | + return; |
111 | 124 | } |
112 | | - else |
| 125 | + |
| 126 | + // If execution started, and we produced GraphQL errors, |
| 127 | + // we return HTTP 500 or 401/403 for authorization errors. |
| 128 | + if (!operationResult.Errors.IsEmpty) |
113 | 129 | { |
114 | | - await Results.BadRequest().ExecuteAsync(context); |
115 | | - } |
| 130 | + var result = GetResultFromErrors(operationResult.Errors); |
116 | 131 |
|
117 | | - return; |
118 | | - } |
| 132 | + await result.ExecuteAsync(context); |
| 133 | + return; |
| 134 | + } |
119 | 135 |
|
120 | | - // If execution started, and we produced GraphQL errors, |
121 | | - // we return HTTP 500 or 401/403 for authorization errors. |
122 | | - if (!operationResult.Errors.IsEmpty) |
123 | | - { |
124 | | - var result = GetResultFromErrors(operationResult.Errors); |
| 136 | + var formatter = session.Schema.Services.GetRequiredService<IOpenApiResultFormatter>(); |
125 | 137 |
|
126 | | - await result.ExecuteAsync(context); |
127 | | - return; |
| 138 | + await formatter.FormatResultAsync(operationResult, context, endpointDescriptor, cancellationToken); |
128 | 139 | } |
129 | | - |
130 | | - var formatter = session.Schema.Services.GetRequiredService<IOpenApiResultFormatter>(); |
131 | | - |
132 | | - await formatter.FormatResultAsync(operationResult, context, endpointDescriptor, cancellationToken); |
133 | 140 | } |
134 | 141 | catch (BadRequestException badRequestException) |
135 | 142 | { |
|
0 commit comments