Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL over HTTP Spec draft compliance. (#5367)
- Loading branch information
1 parent
5f0e65f
commit 44a7ced
Showing
147 changed files
with
2,970 additions
and
1,345 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/CookieCrumble/src/CookieCrumble/Formatters/HttpResponseSnapshotValueFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Buffers; | ||
|
||
namespace CookieCrumble.Formatters; | ||
|
||
public class HttpResponseSnapshotValueFormatter : SnapshotValueFormatter<HttpResponseMessage> | ||
{ | ||
protected override void Format(IBufferWriter<byte> snapshot, HttpResponseMessage value) | ||
{ | ||
var first = true; | ||
|
||
foreach (var header in value.Headers.Concat(value.Content.Headers)) | ||
{ | ||
if (first) | ||
{ | ||
snapshot.Append("Headers:"); | ||
snapshot.AppendLine(); | ||
first = false; | ||
} | ||
|
||
snapshot.Append($"{header.Key}: {string.Join(" ", header.Value)}"); | ||
snapshot.AppendLine(); | ||
} | ||
|
||
if (!first) | ||
{ | ||
snapshot.Append("-------------------------->"); | ||
snapshot.AppendLine(); | ||
} | ||
|
||
snapshot.Append($"Status Code: {value.StatusCode}"); | ||
|
||
snapshot.AppendLine(); | ||
snapshot.Append("-------------------------->"); | ||
snapshot.AppendLine(); | ||
|
||
snapshot.Append(value.Content.ReadAsStringAsync().Result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/HotChocolate/AspNetCore/src/AspNetCore/AcceptMediaType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using HotChocolate.Utilities; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace HotChocolate.AspNetCore; | ||
|
||
/// <summary> | ||
/// Representation of a single media type entry from the accept header. | ||
/// </summary> | ||
public readonly struct AcceptMediaType | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="AcceptMediaType"/>. | ||
/// </summary> | ||
/// <param name="type"> | ||
/// The type of the media type header entry. | ||
/// </param> | ||
/// <param name="subType"> | ||
/// The subtype of the media type header entry. | ||
/// </param> | ||
/// <param name="quality"> | ||
/// The value of the quality parameter `q`. | ||
/// </param> | ||
/// <param name="charset"> | ||
/// The charset. | ||
/// </param> | ||
/// <exception cref="ArgumentNullException"> | ||
/// Type or subtype are empty. | ||
/// </exception> | ||
internal AcceptMediaType( | ||
StringSegment type, | ||
StringSegment subType, | ||
double? quality, | ||
StringSegment charset) | ||
{ | ||
if (!type.HasValue) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
if (!subType.HasValue) | ||
{ | ||
throw new ArgumentNullException(nameof(subType)); | ||
} | ||
|
||
Type = type.Value; | ||
SubType = subType.Value; | ||
Quality = quality; | ||
Charset = charset.HasValue ? charset.Value : null; | ||
IsUtf8 = Charset?.Equals("utf-8", StringComparison.OrdinalIgnoreCase) ?? true; | ||
|
||
if (Type.EqualsOrdinal(ContentType.Types.All) && SubType.EqualsOrdinal(ContentType.Types.All)) | ||
{ | ||
Kind = AcceptMediaTypeKind.All; | ||
} | ||
else if (Type.EqualsOrdinal(ContentType.Types.Application)) | ||
{ | ||
if (SubType.EqualsOrdinal(ContentType.Types.All)) | ||
{ | ||
Kind = AcceptMediaTypeKind.AllApplication; | ||
} | ||
else if (SubType.EqualsOrdinal(ContentType.SubTypes.GraphQLResponse)) | ||
{ | ||
Kind = AcceptMediaTypeKind.ApplicationGraphQL; | ||
} | ||
else if (SubType.EqualsOrdinal(ContentType.SubTypes.Json)) | ||
{ | ||
Kind = AcceptMediaTypeKind.ApplicationJson; | ||
} | ||
} | ||
else if (Type.EqualsOrdinal(ContentType.Types.MultiPart)) | ||
{ | ||
if (SubType.EqualsOrdinal(ContentType.Types.All)) | ||
{ | ||
Kind = AcceptMediaTypeKind.AllMultiPart; | ||
} | ||
else if (SubType.EqualsOrdinal(ContentType.SubTypes.Mixed)) | ||
{ | ||
Kind = AcceptMediaTypeKind.MultiPartMixed; | ||
} | ||
} | ||
else if (Type.EqualsOrdinal(ContentType.Types.Text) && SubType.EqualsOrdinal(ContentType.SubTypes.EventStream)) | ||
{ | ||
Kind = AcceptMediaTypeKind.EventStream; | ||
} | ||
else | ||
{ | ||
Kind = AcceptMediaTypeKind.Unknown; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the media type kind which is an enum representing well-known media type. | ||
/// </summary> | ||
public AcceptMediaTypeKind Kind { get; } | ||
|
||
/// <summary> | ||
/// Gets the type of the <see cref="AcceptMediaType"/>. | ||
/// </summary> | ||
/// <example> | ||
/// For the media type <c>"application/json"</c>, | ||
/// the property gives the value <c>"application"</c>. | ||
/// </example> | ||
/// <remarks> | ||
/// See <see href="https://tools.ietf.org/html/rfc6838#section-4.2"/> | ||
/// for more details on the type. | ||
/// </remarks> | ||
public string Type { get; } | ||
|
||
/// <summary> | ||
/// Gets the subtype of the <see cref="AcceptMediaType"/>. | ||
/// </summary> | ||
/// <example> | ||
/// For the media type <c>"application/vnd.example+json"</c>, the property gives the value | ||
/// <c>"vnd.example+json"</c>. | ||
/// </example> | ||
/// <remarks> | ||
/// See <see href="https://tools.ietf.org/html/rfc6838#section-4.2"/> | ||
/// for more details on the subtype. | ||
/// </remarks> | ||
public string SubType { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value of the quality parameter. Returns null | ||
/// if there is no quality. | ||
/// </summary> | ||
public double? Quality { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the value of the charset parameter. | ||
/// Returns <c>null</c> if there is no charset. | ||
/// </summary> | ||
public string? Charset { get; } | ||
|
||
/// <summary> | ||
/// Defines if the charset is UTF-8. | ||
/// </summary> | ||
public bool IsUtf8 { get; } | ||
} |
47 changes: 47 additions & 0 deletions
47
src/HotChocolate/AspNetCore/src/AspNetCore/AcceptMediaTypeKind.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace HotChocolate.AspNetCore; | ||
|
||
/// <summary> | ||
/// Representation of well-known media kinds. We use this to avoid constant string comparison. | ||
/// </summary> | ||
public enum AcceptMediaTypeKind | ||
{ | ||
/// <summary> | ||
/// Not a well-known meda type. | ||
/// </summary> | ||
Unknown = 0, | ||
|
||
/// <summary> | ||
/// *.* | ||
/// </summary> | ||
All, | ||
|
||
/// <summary> | ||
/// application/* | ||
/// </summary> | ||
AllApplication, | ||
|
||
/// <summary> | ||
/// multipart/* | ||
/// </summary> | ||
AllMultiPart, | ||
|
||
/// <summary> | ||
/// application/graphql-response+json | ||
/// </summary> | ||
ApplicationGraphQL, | ||
|
||
/// <summary> | ||
/// application/json | ||
/// </summary> | ||
ApplicationJson, | ||
|
||
/// <summary> | ||
/// multipart/mixed | ||
/// </summary> | ||
MultiPartMixed, | ||
|
||
/// <summary> | ||
/// text/event-stream | ||
/// </summary> | ||
EventStream | ||
} |
8 changes: 0 additions & 8 deletions
8
src/HotChocolate/AspNetCore/src/AspNetCore/AllowedContentType.cs
This file was deleted.
Oops, something went wrong.
77 changes: 37 additions & 40 deletions
77
src/HotChocolate/AspNetCore/src/AspNetCore/ContentType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,48 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace HotChocolate.AspNetCore; | ||
|
||
internal static class ContentType | ||
{ | ||
public const string GraphQL = "application/graphql; charset=utf-8"; | ||
public const string Json = "application/json; charset=utf-8"; | ||
public const string MultiPart = "multipart/mixed; boundary=\"-\""; | ||
private const string _utf8 = "charset=utf-8"; | ||
private const string _boundary = "boundary=\"-\""; | ||
public const string GraphQL = $"{Types.Application}/{SubTypes.GraphQL};{_utf8}"; | ||
public const string Json = $"{Types.Application}/{SubTypes.Json};{_utf8}"; | ||
public const string MultiPartMixed = $"{Types.MultiPart}/{SubTypes.Mixed};{_boundary};{_utf8}"; | ||
public const string GraphQLResponse = $"{Types.Application}/{SubTypes.GraphQLResponse};{_utf8}"; | ||
public const string EventStream = $"{Types.Text}/{SubTypes.EventStream};{_utf8}"; | ||
|
||
public static ReadOnlySpan<char> JsonSpan() => new char[] | ||
private static readonly char[] _jsonArray = | ||
{ | ||
'a', | ||
'p', | ||
'p', | ||
'l', | ||
'i', | ||
'c', | ||
'a', | ||
't', | ||
'i', | ||
'o', | ||
'n', | ||
'/', | ||
'j', | ||
's', | ||
'o', | ||
'n' | ||
'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'j', 's', 'o', 'n' | ||
}; | ||
|
||
public static ReadOnlySpan<char> MultiPartSpan() => new char[] | ||
private static readonly char[] _multiPartFormArray = | ||
{ | ||
'm', | ||
'u', | ||
'l', | ||
't', | ||
'i', | ||
'p', | ||
'a', | ||
'r', | ||
't', | ||
'/', | ||
'f', | ||
'o', | ||
'r', | ||
'm', | ||
'-', | ||
'd', | ||
'a', | ||
't', | ||
'a' | ||
'm', 'u', 'l', 't', 'i', 'p', 'a', 'r', 't', '/', 'f', 'o', 'r', 'm', '-', 'd', 'a', | ||
't', 'a' | ||
}; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static ReadOnlySpan<char> JsonSpan() => _jsonArray; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static ReadOnlySpan<char> MultiPartFormSpan() => _multiPartFormArray; | ||
|
||
public static class Types | ||
{ | ||
public const string All = "*"; | ||
public const string Application = "application"; | ||
public const string MultiPart = "multipart"; | ||
public const string Text = "text"; | ||
} | ||
|
||
public static class SubTypes | ||
{ | ||
public const string GraphQL = "graphql"; | ||
public const string GraphQLResponse = "graphql-response+json"; | ||
public const string Json = "json"; | ||
public const string Mixed = "mixed"; | ||
public const string EventStream = "event-stream"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.