Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;

internal sealed class GraphQLOverWebSocketProtocolHandler(
ISocketSessionInterceptor interceptor,
IWebSocketPayloadFormatter formatter)
IWebSocketPayloadFormatter formatter,
IDocumentCache documentCache,
IDocumentHashProvider documentHashProvider)
: IGraphQLOverWebSocketProtocolHandler
{
public string Name => GraphQL_Transport_WS;
Expand Down Expand Up @@ -296,7 +298,7 @@ public ValueTask OnConnectionInitTimeoutAsync(
CancellationToken cancellationToken)
=> session.Connection.CloseConnectionInitTimeoutAsync(cancellationToken);

private static bool TryParseSubscribeMessage(
private bool TryParseSubscribeMessage(
JsonElement messageElement,
[NotNullWhen(true)] out SubscribeMessage? message)
{
Expand All @@ -317,7 +319,10 @@ private static bool TryParseSubscribeMessage(

var id = idProp.GetString()!;
var requestData = JsonMarshal.GetRawUtf8Value(payloadProp);
var request = Parse(requestData);
var request = Parse(
requestData,
cache: documentCache,
hashProvider: documentHashProvider);

if (request.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using HotChocolate.AspNetCore.Subscriptions.Protocols.Apollo;
using HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;
using HotChocolate.Execution.Configuration;
using HotChocolate.Language;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -91,7 +92,9 @@ private static IRequestExecutorBuilder AddGraphQLOverWebSocketProtocol(
s => s.AddSingleton<IProtocolHandler>(
sp => new GraphQLOverWebSocketProtocolHandler(
sp.GetRequiredService<ISocketSessionInterceptor>(),
sp.GetRequiredService<IWebSocketPayloadFormatter>())));
sp.GetRequiredService<IWebSocketPayloadFormatter>(),
sp.GetRequiredService<IDocumentCache>(),
sp.GetRequiredService<IDocumentHashProvider>())));

/// <summary>
/// Adds a custom WebSocket payload formatter to the DI.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using HotChocolate.AspNetCore.Formatters;
using HotChocolate.AspNetCore.Subscriptions.Protocols;
using HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;
using HotChocolate.AspNetCore.Tests.Utilities;
using HotChocolate.AspNetCore.Tests.Utilities.Subscriptions.GraphQLOverWebSocket;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.PersistedOperations;
using HotChocolate.Subscriptions.Diagnostics;
using HotChocolate.Text.Json;
using HotChocolate.Transport.Formatters;
Expand Down Expand Up @@ -284,6 +288,69 @@ await testServer.SendPostRequestAsync(
});
}

[Fact]
public Task Subscribe_With_PersistedQuery_Extension_Only_Works()
=> TryTest(
async ct =>
{
// arrange
var storage = new OperationStorage();
var hashProvider = new MD5DocumentHashProvider(HashFormat.Base64);
const string query = "subscription { onReview(episode: NEW_HOPE) { stars } }";
var hash = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(query)).Value;
storage.AddOperation(hash, query);

using var testServer = CreateStarWarsServer(
configureServices: services => services
.AddGraphQLServer()
.AddMD5DocumentHashProvider(HashFormat.Base64)
.ConfigureSchemaServices(c => c.AddSingleton<IOperationDocumentStorage>(storage)),
output: output);
var client = CreateWebSocketClient(testServer);
using var webSocket = await ConnectToServerAsync(client, ct);

var subscribeMessage = JsonSerializer.Serialize(
new
{
type = "subscribe",
id = "abc",
payload = new
{
extensions = new Dictionary<string, object?>
{
["persistedQuery"] = new Dictionary<string, object?>
{
["version"] = 1,
[hashProvider.Name] = hash
}
}
}
});

// act
await webSocket.SendMessageAsync(subscribeMessage, ct);

await testServer.SendPostRequestAsync(
new ClientQueryRequest
{
Query =
"""
mutation {
createReview(episode: NEW_HOPE review: {
commentary: "foo"
stars: 5
}) {
stars
}
}
"""
});

// assert
var message = await WaitForMessage(webSocket, Messages.Next, ct);
Assert.NotNull(message);
});

[Fact]
public Task Subscribe_Id_Not_Unique()
{
Expand Down Expand Up @@ -956,6 +1023,31 @@ await testServer.SendPostRequestAsync(
Assert.False(messageOnReview.TryGetProperty("commentary", out _));
});

private sealed class OperationStorage : IOperationDocumentStorage
{
private readonly Dictionary<string, OperationDocument> _cache =
new(StringComparer.Ordinal);

public ValueTask<IOperationDocument?> TryReadAsync(
OperationDocumentId documentId,
CancellationToken cancellationToken = default)
=> _cache.TryGetValue(documentId.Value, out var value)
? new ValueTask<IOperationDocument?>(value)
: new ValueTask<IOperationDocument?>(default(IOperationDocument));

public ValueTask SaveAsync(
OperationDocumentId documentId,
IOperationDocument document,
CancellationToken cancellationToken = default)
=> throw new NotImplementedException();

public void AddOperation(string key, string sourceText)
{
var doc = new OperationDocument(Utf8GraphQLParser.Parse(sourceText));
_cache.Add(key, doc);
}
}

private class AuthInterceptor : DefaultSocketSessionInterceptor
{
public override ValueTask<ConnectionStatus> OnConnectAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,7 @@ private static IFusionGatewayBuilder AddGraphQLOverWebSocketProtocol(
(_, s) => s.AddSingleton<IProtocolHandler>(
sp => new GraphQLOverWebSocketProtocolHandler(
sp.GetRequiredService<ISocketSessionInterceptor>(),
sp.GetRequiredService<IWebSocketPayloadFormatter>())));
sp.GetRequiredService<IWebSocketPayloadFormatter>(),
sp.GetRequiredService<IDocumentCache>(),
sp.GetRequiredService<IDocumentHashProvider>())));
}
Loading