Skip to content

Commit 6d22223

Browse files
authored
Review for #581 (#651)
1 parent ad0acdb commit 6d22223

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

src/Transports.AspNetCore/GraphQLHttpMiddleware.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ public async Task InvokeAsync(HttpContext context)
6161
if (!isGet && !isPost)
6262
{
6363
httpResponse.Headers["Allow"] = "GET, POST";
64-
await WriteErrorResponseAsync(httpResponse, writer, cancellationToken,
64+
await WriteErrorResponseAsync(
65+
httpResponse,
66+
writer,
67+
cancellationToken,
6568
$"Invalid HTTP method. Only GET and POST are supported. {DOCS_URL}",
66-
httpStatusCode: HttpStatusCode.MethodNotAllowed
69+
HttpStatusCode.MethodNotAllowed
6770
);
6871
return;
6972
}
@@ -75,7 +78,12 @@ await WriteErrorResponseAsync(httpResponse, writer, cancellationToken,
7578
{
7679
if (!MediaTypeHeaderValue.TryParse(httpRequest.ContentType, out var mediaTypeHeader))
7780
{
78-
await WriteErrorResponseAsync(httpResponse, writer, cancellationToken, $"Invalid 'Content-Type' header: value '{httpRequest.ContentType}' could not be parsed.");
81+
await WriteErrorResponseAsync(
82+
httpResponse,
83+
writer,
84+
cancellationToken,
85+
$"Invalid 'Content-Type' header: value '{httpRequest.ContentType}' could not be parsed.",
86+
HttpStatusCode.BadRequest);
7987
return;
8088
}
8189

@@ -88,7 +96,7 @@ await WriteErrorResponseAsync(httpResponse, writer, cancellationToken,
8896
var message = deserializationResult.Exception is null
8997
? "JSON body text could not be parsed."
9098
: $"JSON body text could not be parsed. {deserializationResult.Exception.Message}";
91-
await WriteErrorResponseAsync(httpResponse, writer, cancellationToken, message);
99+
await WriteErrorResponseAsync(httpResponse, writer, cancellationToken, message, HttpStatusCode.BadRequest);
92100
return;
93101
}
94102
bodyGQLRequest = deserializationResult.Single;
@@ -132,9 +140,12 @@ await WriteErrorResponseAsync(
132140

133141
if (string.IsNullOrWhiteSpace(gqlRequest.Query))
134142
{
135-
await WriteErrorResponseAsync(httpResponse, writer, cancellationToken,
143+
await WriteErrorResponseAsync(
144+
httpResponse,
145+
writer,
146+
cancellationToken,
136147
"GraphQL query is missing.",
137-
httpStatusCode: HttpStatusCode.BadRequest
148+
HttpStatusCode.BadRequest
138149
);
139150
return;
140151
}
@@ -204,7 +215,7 @@ protected virtual Task RequestExecutedAsync(in GraphQLRequestExecutionResult req
204215
}
205216

206217
private Task WriteErrorResponseAsync(HttpResponse httpResponse, IDocumentWriter writer, CancellationToken cancellationToken,
207-
string errorMessage, HttpStatusCode httpStatusCode = HttpStatusCode.BadRequest)
218+
string errorMessage, HttpStatusCode httpStatusCode)
208219
{
209220
var result = new ExecutionResult
210221
{

src/Transports.AspNetCore/GraphQLRequestDeserializationException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace GraphQL.Server.Transports.AspNetCore
44
{
5+
/// <summary>
6+
/// Exception used by <see cref="IGraphQLRequestDeserializer"/> implementations
7+
/// when deserialization failed.
8+
/// </summary>
59
public class GraphQLRequestDeserializationException : Exception
610
{
711
public GraphQLRequestDeserializationException(string message) : base(message)
@@ -12,6 +16,14 @@ public GraphQLRequestDeserializationException(Exception inner) : base(inner.Mess
1216
{
1317
}
1418

19+
public GraphQLRequestDeserializationException(string message, Exception inner) : base(message, inner)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Creates an instance of <see cref="GraphQLRequestDeserializationException"/> for a situations
25+
/// when the first symbol of JSON body neither '{' nor '['.
26+
/// </summary>
1527
public static GraphQLRequestDeserializationException InvalidFirstChar()
1628
{
1729
return new GraphQLRequestDeserializationException("Body text should start with '{' for normal graphql query or with '[' for batched query.");

src/Transports.AspNetCore/GraphQLRequestDeserializationResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class GraphQLRequestDeserializationResult
1010
/// Flag indicating success. When false, the <see cref="GraphQLHttpMiddleware{TSchema}"/>
1111
/// will return a 400 BadRequest HTTP status to the client.
1212
/// </summary>
13-
public bool IsSuccessful { get; set; }
13+
public bool IsSuccessful { get; set; } // TODO: delete in v6
1414

1515
/// <summary>
1616
/// A deserialized GraphQL request,
@@ -25,7 +25,7 @@ public class GraphQLRequestDeserializationResult
2525
public GraphQLRequest[] Batch { get; set; }
2626

2727
/// <summary>
28-
/// If deserialization throws an exception, it is stored here
28+
/// If deserialization throws an exception, it is stored here.
2929
/// </summary>
3030
public GraphQLRequestDeserializationException Exception { get; set; }
3131
}

tests/ApiApprovalTests/GraphQL.Server.Transports.AspNetCore.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace GraphQL.Server.Transports.AspNetCore
2626
{
2727
public GraphQLRequestDeserializationException(System.Exception inner) { }
2828
public GraphQLRequestDeserializationException(string message) { }
29+
public GraphQLRequestDeserializationException(string message, System.Exception inner) { }
2930
public static GraphQL.Server.Transports.AspNetCore.GraphQLRequestDeserializationException InvalidFirstChar() { }
3031
}
3132
public class GraphQLRequestDeserializationResult

0 commit comments

Comments
 (0)