Skip to content

Commit

Permalink
Added HTTP GET Middleware Options (#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Nov 16, 2020
1 parent 0a76b29 commit 13ebe83
Show file tree
Hide file tree
Showing 112 changed files with 1,645 additions and 509 deletions.
@@ -1,4 +1,4 @@
namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
public enum AllowedContentType
{
Expand Down
@@ -1,13 +1,11 @@
using System;

namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
public static class ContentType
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=\"-\"";

public static ReadOnlySpan<char> JsonSpan() => new char[]
Expand Down
@@ -1,10 +1,10 @@
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using Microsoft.AspNetCore.Http;
using HotChocolate.Execution;

namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
public class DefaultHttpRequestInterceptor : IHttpRequestInterceptor
{
Expand All @@ -21,7 +21,7 @@ public class DefaultHttpRequestInterceptor : IHttpRequestInterceptor

if (context.IsTracingEnabled())
{
requestBuilder.TryAddProperty(ContextDataKeys.EnableTracing, true);
requestBuilder.TryAddProperty(WellKnownContextData.EnableTracing, true);
}

return default;
Expand Down
Expand Up @@ -6,7 +6,7 @@
using HotChocolate.AspNetCore.Subscriptions.Messages;
using HotChocolate.Execution;

namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
public class DefaultSocketSessionInterceptor : ISocketSessionInterceptor
{
Expand All @@ -29,7 +29,7 @@ public class DefaultSocketSessionInterceptor : ISocketSessionInterceptor

if (connection.HttpContext.IsTracingEnabled())
{
requestBuilder.TryAddProperty(ContextDataKeys.EnableTracing, true);
requestBuilder.TryAddProperty(WellKnownContextData.EnableTracing, true);
}

return default;
Expand Down
@@ -1,10 +1,10 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using Microsoft.AspNetCore.Http;
using HotChocolate.Execution;

namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
internal sealed class DelegateHttpRequestInterceptor : DefaultHttpRequestInterceptor
{
Expand Down
@@ -1,6 +1,6 @@
using HotChocolate.Execution;

namespace HotChocolate.AspNetCore.Utilities
namespace HotChocolate.AspNetCore
{
internal static class ErrorHelper
{
Expand Down
@@ -0,0 +1,22 @@
using System;
using Microsoft.AspNetCore.Builder;

namespace HotChocolate.AspNetCore.Extensions
{
/// <summary>
/// Represents the endpoint convention builder for GraphQL.
/// </summary>
public sealed class GraphQLEndpointConventionBuilder : IEndpointConventionBuilder
{
private readonly IEndpointConventionBuilder _builder;

internal GraphQLEndpointConventionBuilder(IEndpointConventionBuilder builder)
{
_builder = builder;
}

/// <inheritdoc />
public void Add(Action<EndpointBuilder> convention) =>
_builder.Add(convention);
}
}
@@ -1,19 +1,47 @@
using System;
using HotChocolate.AspNetCore.Utilities;
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Serialization;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
{
/// <summary>
/// Adds an interceptor for GraphQL requests to the GraphQL configuration.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <typeparam name="T">
/// The <see cref="IHttpRequestInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(
this IRequestExecutorBuilder builder)
where T : class, IHttpRequestInterceptor =>
builder.ConfigureSchemaServices(s => s
.RemoveAll<IHttpRequestInterceptor>()
.AddSingleton<IHttpRequestInterceptor, T>());

/// <summary>
/// Adds an interceptor for GraphQL requests to the GraphQL configuration.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="factory">
/// A factory that creates the interceptor instance.
/// </param>
/// <typeparam name="T">
/// The <see cref="IHttpRequestInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, T> factory)
Expand All @@ -22,21 +50,48 @@ public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
.RemoveAll<IHttpRequestInterceptor>()
.AddSingleton<IHttpRequestInterceptor, T>(factory));

/// <summary>
/// Adds an interceptor for GraphQL requests to the GraphQL configuration.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="interceptor">
/// The interceptor instance that shall be added to the configuration.
/// </param>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
public static IRequestExecutorBuilder AddHttpRequestInterceptor(
this IRequestExecutorBuilder builder,
HttpRequestInterceptorDelegate interceptor) =>
AddHttpRequestInterceptor(
builder,
sp => new DelegateHttpRequestInterceptor(interceptor));
_ => new DelegateHttpRequestInterceptor(interceptor));

private static IRequestExecutorBuilder AddHttpRequestInterceptor(
private static IRequestExecutorBuilder AddDefaultHttpRequestInterceptor(
this IRequestExecutorBuilder builder)
{
return builder.ConfigureSchemaServices(s =>
s.TryAddSingleton<IHttpRequestInterceptor, DefaultHttpRequestInterceptor>());
return builder.ConfigureSchemaServices(
s => s.TryAddSingleton<IHttpRequestInterceptor, DefaultHttpRequestInterceptor>());
}

public static IServiceCollection AddHttpRequestSerializer(
/// <summary>
/// Adds the <see cref="DefaultHttpResultSerializer"/> with specific serialization settings
/// to the DI.
/// </summary>
/// <param name="services">
/// The <see cref="IServiceCollection"/>.
/// </param>
/// <param name="batchSerialization">
/// Specifies the batch serialization format.
/// </param>
/// <param name="deferSerialization"></param>
/// Specifies the defer/stream serialization format.
/// <returns>
/// Returns the <see cref="IServiceCollection"/> so that configuration can be chained.
/// </returns>
public static IServiceCollection AddHttpResultSerializer(
this IServiceCollection services,
HttpResultSerialization batchSerialization = HttpResultSerialization.MultiPartChunked,
HttpResultSerialization deferSerialization = HttpResultSerialization.MultiPartChunked)
Expand All @@ -49,7 +104,19 @@ public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
return services;
}

public static IServiceCollection AddHttpRequestSerializer<T>(
/// <summary>
/// Adds a custom http request serializer to the DI.
/// </summary>
/// <param name="services">
/// The <see cref="IServiceCollection"/>.
/// </param>
/// <typeparam name="T">
/// The type of the custom <see cref="IHttpResultSerializer"/>.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IServiceCollection"/> so that configuration can be chained.
/// </returns>
public static IServiceCollection AddHttpResultSerializer<T>(
this IServiceCollection services)
where T : class, IHttpResultSerializer
{
Expand Down
@@ -1,22 +1,48 @@
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Subscriptions;
using HotChocolate.AspNetCore.Subscriptions.Messages;
using HotChocolate.AspNetCore.Utilities;
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
{
/// <summary>
/// Adds an interceptor for GraphQL socket sessions to the GraphQL configuration.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <typeparam name="T">
/// The <see cref="ISocketSessionInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
this IRequestExecutorBuilder builder)
where T : class, ISocketSessionInterceptor =>
builder.ConfigureSchemaServices(s => s
.RemoveAll<ISocketSessionInterceptor>()
.AddSingleton<ISocketSessionInterceptor, T>());


/// <summary>
/// Adds an interceptor for GraphQL socket sessions to the GraphQL configuration.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="factory">
/// A factory that creates the interceptor instance.
/// </param>
/// <typeparam name="T">
/// The <see cref="ISocketSessionInterceptor"/> implementation.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, T> factory)
Expand Down
@@ -1,11 +1,23 @@
using System;
using HotChocolate.AspNetCore.Utilities;
using HotChocolate.AspNetCore.Warmup;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
{
/// <summary>
/// Adds the current GraphQL configuration to the warmup background service.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The <see cref="IRequestExecutorBuilder"/> is <c>null</c>.
/// </exception>
public static IRequestExecutorBuilder InitializeOnStartup(
this IRequestExecutorBuilder builder)
{
Expand All @@ -14,7 +26,7 @@ public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
throw new ArgumentNullException(nameof(builder));
}

builder.Services.AddHostedService<ExecutorWarmupTask>();
builder.Services.AddHostedService<ExecutorWarmupService>();
builder.Services.AddSingleton(new WarmupSchema(builder.Name));
return builder;
}
Expand Down

0 comments on commit 13ebe83

Please sign in to comment.