Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graphql http client as transport project #6330

Merged
merged 36 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4efb515
create transport abstraction project to share basic transport classes
A360JMaxxgamer Jul 8, 2023
c148cb4
move JsonDefaults.cs to abstractions project to share serializer sett…
A360JMaxxgamer Jul 8, 2023
98c7d2c
add missing using
A360JMaxxgamer Jul 8, 2023
6566603
start implementation of graphql over http client
A360JMaxxgamer Jul 8, 2023
b313cad
send basic correct request to service
A360JMaxxgamer Jul 8, 2023
2490e30
add unittest to ensure simple post request returns expected data
A360JMaxxgamer Jul 8, 2023
67692e2
implement basic get operation on graphql http client
A360JMaxxgamer Jul 8, 2023
8d31225
add test case for variables via query parameter on graphql http client
A360JMaxxgamer Jul 8, 2023
8611be8
add usage of ArrayWriter from HotChocolate.Utilities project and move…
A360JMaxxgamer Jul 14, 2023
8da8b92
add missing content type header for post requests
A360JMaxxgamer Jul 16, 2023
ef518d2
Merge branch 'main' into graphql-over-http-client
A360JMaxxgamer Jul 16, 2023
7a1741d
add missing import
A360JMaxxgamer Jul 17, 2023
51a82de
Merge remote-tracking branch 'origin/graphql-over-http-client' into g…
A360JMaxxgamer Jul 17, 2023
7227b35
Merge branch 'main' into graphql-over-http-client
A360JMaxxgamer Jul 17, 2023
a2de24b
Started to reorg things
michaelstaib Jul 18, 2023
8de2a08
Put more meat on the post method
michaelstaib Jul 18, 2023
4bbf231
Structural Refinements
michaelstaib Jul 18, 2023
abce457
reworked tests
michaelstaib Jul 18, 2023
d274392
fixed snapshots
michaelstaib Jul 18, 2023
bb2ece7
fixed snapshots
michaelstaib Jul 18, 2023
364e14f
cleanup
michaelstaib Jul 18, 2023
d9b5234
cleanup
michaelstaib Jul 18, 2023
df01e90
Integrate new client with fusion
michaelstaib Jul 18, 2023
6e53a40
cleanup
michaelstaib Jul 18, 2023
4c594de
cleanup
michaelstaib Jul 18, 2023
beb33c2
cleanup
michaelstaib Jul 18, 2023
0dc01c8
cleanup
michaelstaib Jul 18, 2023
ea07f05
cleanup
michaelstaib Jul 18, 2023
f09f360
cleanup
michaelstaib Jul 18, 2023
ab4c13b
Update src/HotChocolate/AspNetCore/test/Transport.Http.Tests/HotChoco…
michaelstaib Jul 18, 2023
1fe0626
Removed the document syntax tree from the resolve nodes
michaelstaib Jul 18, 2023
23c143c
cleanup
michaelstaib Jul 18, 2023
8ba57a5
cleanup
michaelstaib Jul 18, 2023
5c5d208
cleanup
michaelstaib Jul 18, 2023
14759c8
cleanup
michaelstaib Jul 18, 2023
b6c1c54
fixed solution file
michaelstaib Jul 18, 2023
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
1 change: 1 addition & 0 deletions src/CookieCrumble/src/CookieCrumble/CookieCrumble.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\HotChocolate\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\HotChocolate\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\HotChocolate\AspNetCore\src\Transport.Abstractions\HotChocolate.Transport.Abstractions.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Language.Utilities;

Expand Down Expand Up @@ -26,4 +27,4 @@ protected override void Format(IBufferWriter<byte> snapshot, ISyntaxNode value)

ArrayPool<char>.Shared.Return(buffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Buffers;
using System.Text.Json;
using HotChocolate.Transport;

namespace CookieCrumble.Formatters;

internal sealed class OperationResultSnapshotValueFormatter : SnapshotValueFormatter<OperationResult>
{
protected override void Format(IBufferWriter<byte> snapshot, OperationResult value)
{
if (value.Data.ValueKind is JsonValueKind.Object)
{
snapshot.Append("Data:");
snapshot.AppendLine();
snapshot.Append(value.Data.ToString());
snapshot.AppendLine();
}

if (value.Errors.ValueKind is JsonValueKind.Array)
{
snapshot.Append("Errors:");
snapshot.AppendLine();
snapshot.Append(value.Errors.ToString());
snapshot.AppendLine();
}

if (value.Extensions.ValueKind is JsonValueKind.Object)
{
snapshot.Append("Extensions:");
snapshot.AppendLine();
snapshot.Append(value.Extensions.ToString());
snapshot.AppendLine();
}
}
}
1 change: 1 addition & 0 deletions src/CookieCrumble/src/CookieCrumble/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public sealed class Snapshot
new ExceptionSnapshotValueFormatter(),
new SchemaErrorSnapshotValueFormatter(),
new HttpResponseSnapshotValueFormatter(),
new OperationResultSnapshotValueFormatter(),
#if NET7_0_OR_GREATER
new QueryPlanSnapshotValueFormatter(),
#endif
Expand Down
262 changes: 102 additions & 160 deletions src/HotChocolate/AspNetCore/HotChocolate.AspNetCore.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private async ValueTask<AuthorizeResult> AuthorizeAsync(

// If a policy name was supplied we will try to resolve the policy
// and authorize with it.
var policy = await _policyProvider.GetPolicyAsync(policyName).ConfigureAwait(false);
var policy = await _policyProvider.GetPolicyAsync(policyName!).ConfigureAwait(false);

if (policy is null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace HotChocolate.Transport;

/// <summary>
/// This class provides the default content types for GraphQL requests and responses.
/// </summary>
public static class ContentType
{
/// <summary>
/// Gets the application/json content type.
/// </summary>
public const string Json = "application/json";

/// <summary>
/// Gets the application/graphql-response+json content type.
/// </summary>
public const string GraphQL = "application/graphql-response+json";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(LibraryTargetFrameworks)</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<PackageId>HotChocolate.Transport.Abstractions</PackageId>
<AssemblyName>HotChocolate.Transport.Abstractions</AssemblyName>
<RootNamespace>HotChocolate.Transport</RootNamespace>
<Description>This package contains common abstractions and base classes for transport.</Description>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Transport.Http" />
<InternalsVisibleTo Include="HotChocolate.Transport.Sockets" />
<InternalsVisibleTo Include="HotChocolate.Transport.Sockets.Client" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Language\src\Language.SyntaxTree\HotChocolate.Language.SyntaxTree.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
<PackageReference Include="System.Text.Json" Version="6.0.7" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\TransportAbstractionResoucrces.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TransportAbstractionResoucrces.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\TransportAbstractionResoucrces.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TransportAbstractionResoucrces.resx</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using HotChocolate.Language;
using static HotChocolate.Transport.Sockets.Client.Properties.SocketClientResources;
using HotChocolate.Transport.Serialization;
using static HotChocolate.Transport.Properties.TransportAbstractionResoucrces;

namespace HotChocolate.Transport.Sockets.Client;
namespace HotChocolate.Transport;

/// <summary>
/// Represents a GraphQL operation request that can be sent over a WebSocket connection.
Expand Down Expand Up @@ -32,20 +34,20 @@ namespace HotChocolate.Transport.Sockets.Client;
/// Thrown if the query, ID, and extensions parameters are all null.
/// </exception>
public OperationRequest(
DocumentNode? query,
string? query,
string? id,
string? operationName,
ObjectValueNode? variables,
ObjectValueNode? extensions)
{
if (query is null && id is null && extensions is null)
if (string.IsNullOrWhiteSpace(query) && id is null && extensions is null)
{
throw new ArgumentException(
OperationRequest_QueryOrPersistedQueryId,
nameof(query));
}

Query = query?.ToString(false);
Query = query;
Id = id;
OperationName = operationName;
VariablesNode = variables;
Expand Down Expand Up @@ -80,7 +82,7 @@ public OperationRequest(
IReadOnlyDictionary<string, object?>? variables = null,
IReadOnlyDictionary<string, object?>? extensions = null)
{
if (query is null && id is null && extensions is null)
if (string.IsNullOrWhiteSpace(query) && id is null && extensions is null)
{
throw new ArgumentException(
OperationRequest_QueryOrPersistedQueryId,
Expand Down Expand Up @@ -131,6 +133,22 @@ public OperationRequest(
/// </summary>
public ObjectValueNode? ExtensionsNode { get; }

/// <summary>
/// Writes a serialized version of this request to a <see cref="Utf8JsonWriter"/>.
/// </summary>
/// <param name="writer">
/// The JSON writer.
/// </param>
public void WriteTo(Utf8JsonWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}

Utf8JsonWriterHelper.WriteOperationRequest(writer, this);
}

/// <summary>
/// Determines whether this <see cref="OperationRequest"/> object is equal to another object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Text.Json;
using static HotChocolate.Transport.Serialization.Utf8GraphQLResultProperties;

namespace HotChocolate.Transport.Sockets.Client;
namespace HotChocolate.Transport;

/// <summary>
/// Represents the result of a GraphQL operation.
/// </summary>
public sealed class OperationResult : IDisposable
{
private readonly JsonDocument _document;
private readonly IDisposable? _memoryOwner;

/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class with the
/// specified JSON document and optional data, errors, and extensions.
/// </summary>
/// <param name="document">
/// The <see cref="JsonDocument"/> object representing the JSON result of the
/// <param name="memoryOwner">
/// The memory owner of the json elements.
/// operation.
/// </param>
/// <param name="data">
Expand All @@ -30,12 +31,12 @@ public sealed class OperationResult : IDisposable
/// operation.
/// </param>
public OperationResult(
JsonDocument document,
IDisposable? memoryOwner = default,
JsonElement data = default,
JsonElement errors = default,
JsonElement extensions = default)
{
_document = document;
_memoryOwner = memoryOwner;
Data = data;
Errors = errors;
Extensions = extensions;
Expand Down Expand Up @@ -63,5 +64,22 @@ public OperationResult(
/// Releases all resources used by the <see cref="OperationResult"/> object.
/// </summary>
public void Dispose()
=> _document.Dispose();
=> _memoryOwner?.Dispose();

public static OperationResult Parse(JsonDocument document)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}

var root = document.RootElement;

return new OperationResult(
document,
root.TryGetProperty(DataProp, out var data) ? data : default,
root.TryGetProperty(ErrorsProp, out var errors) ? errors : default,
root.TryGetProperty(ExtensionsProp, out var extensions) ? extensions : default);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">

</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="OperationRequest_QueryOrPersistedQueryId" xml:space="preserve">
<value>Either a query needs to be provided or a persisted query id.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using System.Text.Json;

namespace HotChocolate.Transport.Sockets.Client.Helpers;
namespace HotChocolate.Transport.Serialization;

internal static class JsonDefaults
/// <summary>
/// A helper class that contains the default settings for JSON serialization.
/// </summary>
internal static class JsonOptionDefaults
{
/// <summary>
/// Gets the default <see cref="JsonWriterOptions"/>.
/// </summary>
public static JsonWriterOptions WriterOptions { get; } =
new() { Indented = false };

/// <summary>
/// Gets the default <see cref="JsonSerializerOptions"/>.
/// </summary>
#if NET6_0_OR_GREATER
public static JsonSerializerOptions SerializerOptions { get; } =
new(JsonSerializerDefaults.Web);
Expand Down
Loading
Loading