Skip to content

Commit

Permalink
Added new dependency injection configuration API (#1971)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed May 27, 2020
1 parent c12223b commit 7f63f0d
Show file tree
Hide file tree
Showing 200 changed files with 5,815 additions and 1,332 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.201"
"version": "3.1.300"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void AddGraphQL_ServicesSchemaBuilder()
services,
SchemaBuilder.New()
.AddDocumentFromString("type Query { a: String }")
.Use(next => context => Task.CompletedTask));
.Use(next => context => default(ValueTask)));

// assert
services.Select(t => ReflectionUtils.GetTypeName(t.ServiceType))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using BenchmarkDotNet.Attributes;
using System.Collections.Generic;
using System.Buffers;
using Microsoft.Extensions.DependencyInjection;
using HotChocolate.StarWars;
using System.Threading.Tasks;
using System;
using HotChocolate.Language;
using System.Text;

namespace HotChocolate.Execution.Benchmarks
{
[RPlotExporter, CategoriesColumn, RankColumn, MeanColumn, MedianColumn, MemoryDiagnoser]
public class DefaultExecutionPipelineBenchmark
{
private readonly IRequestExecutor _executor;
private readonly IReadOnlyQueryRequest _request;

public DefaultExecutionPipelineBenchmark()
{
var services = new ServiceCollection()
.AddStarWarsRepositories()
.AddGraphQL()
.ConfigureSchema(b => b.AddStarWarsTypes())
.Services.BuildServiceProvider();

_executor = services
.GetRequiredService<IRequestExecutorResolver>()
.GetRequestExecutorAsync().Result;

var md5 = new MD5DocumentHashProvider();
var resources = new ResourceHelper();
string introspectionQuery = resources.GetResourceString("IntrospectionQuery.graphql");
string hash = md5.ComputeHash(Encoding.UTF8.GetBytes(introspectionQuery).AsSpan());
DocumentNode document = Utf8GraphQLParser.Parse(introspectionQuery);
_request = QueryRequestBuilder.New()
.SetQuery(document)
.SetQueryHash(hash)
.SetQueryName(hash)
.Create();

SchemaIntrospection().Wait();
}

[Benchmark]
public async Task SchemaIntrospection()
{
IExecutionResult result = await _executor.ExecuteAsync(_request);
// var jsonWriter = new HotChocolate.Execution.Serialization.JsonQueryResultSerializer(true);
// Console.WriteLine(jsonWriter.Serialize((IReadOnlyQueryResult)result));
((IDisposable)result).Dispose();
}

[Benchmark]
public async Task SchemaIntrospectionMultiple()
{
Task<IExecutionResult> task1 = _executor.ExecuteAsync(_request);
Task<IExecutionResult> task2 = _executor.ExecuteAsync(_request);
Task<IExecutionResult> task3 = _executor.ExecuteAsync(_request);
Task<IExecutionResult> task4 = _executor.ExecuteAsync(_request);
Task<IExecutionResult> task5 = _executor.ExecuteAsync(_request);

((IDisposable)(await task1)).Dispose();
((IDisposable)(await task2)).Dispose();
task1 = _executor.ExecuteAsync(_request);
((IDisposable)(await task3)).Dispose();
task2 = _executor.ExecuteAsync(_request);
((IDisposable)(await task4)).Dispose();
((IDisposable)(await task5)).Dispose();
((IDisposable)(await task1)).Dispose();
((IDisposable)(await task2)).Dispose();
}
}
}
24 changes: 21 additions & 3 deletions src/HotChocolate/Core/benchmark/Execution.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
using BenchmarkDotNet.Running;
using System.Threading.Tasks;
using BenchmarkDotNet.Running;

namespace HotChocolate.Execution.Benchmarks
{
class Program
{
static void Main(string[] args) =>
BenchmarkRunner.Run<VariableCoercionBenchmarks>();

static void Main(string[] args) =>
BenchmarkRunner.Run<DefaultExecutionPipelineBenchmark>();


/*
static async Task Main(string[] args)
{
var bench = new DefaultExecutionPipelineBenchmark();
for (int i = 0; i < 1000; i++)
{
await bench.SchemaIntrospection();
}
}
*/

}
}
35 changes: 35 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/ErrorHandlerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace HotChocolate
{
public static class ErrorHandlerExtensions
{
public static IEnumerable<IError> Handle(
this IErrorHandler errorHandler,
IEnumerable<IError> errors)
{
if (errorHandler == null)
{
throw new ArgumentNullException(nameof(errorHandler));
}

if (errors == null)
{
throw new ArgumentNullException(nameof(errors));
}

return HandleEnumerator(errorHandler, errors);
}

private static IEnumerable<IError> HandleEnumerator(
IErrorHandler errorHandler,
IEnumerable<IError> errors)
{
foreach (IError error in errors)
{
yield return errorHandler.Handle(error);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IReadOnlyQueryRequest

IReadOnlyDictionary<string, object?>? VariableValues { get; }

object? InitialValue { get; }
object? InitialValue { get; }

IReadOnlyDictionary<string, object?>? ContextData { get; }

Expand Down
11 changes: 0 additions & 11 deletions src/HotChocolate/Core/src/Abstractions/Execution/IResult.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

#nullable enable

namespace HotChocolate.Execution
{
/// <summary>
/// This interface represents the owner of the rented objects associated
/// with the result data structure.
///
/// When this object is disposed it will return the objects representing the
/// <see cref="Data" /> object structure back to the object pools.
/// </summary>
public interface IResultMemoryOwner : IDisposable
{
/// <summary>
/// The data object structure representing the GraphQL result.
/// </summary>
IResultMap? Data { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace HotChocolate.Execution
#nullable enable

using System.Diagnostics.CodeAnalysis;

namespace HotChocolate.Execution
{
/// <summary>
/// Represents a collection of coerced variables.
Expand Down Expand Up @@ -30,6 +34,6 @@ public interface IVariableValueCollection
/// <c>true</c> if a coerced variable exists and can be converted
/// to the requested type; otherwise, <c>false</c> will be returned.
/// </returns>
bool TryGetVariable<T>(NameString name, out T value);
bool TryGetVariable<T>(NameString name, [NotNullWhen(true)]out T value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public QueryResult(
IReadOnlyList<IError>? errors,
IReadOnlyDictionary<string, object?>? extension = null,
IReadOnlyDictionary<string, object?>? contextData = null,
IDisposable? disposable = null)
IDisposable? resultMemoryOwner = null)
{
if (data is null && errors is null)
{
Expand All @@ -27,7 +27,7 @@ public QueryResult(
Errors = errors;
Extensions = extension;
ContextData = contextData;
_disposable = disposable;
_disposable = resultMemoryOwner;
}

public IReadOnlyDictionary<string, object?>? Data { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/HotChocolate/Core/src/Abstractions/IErrorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IErrorBuilder

IErrorBuilder RemoveException();

IErrorBuilder SetExtension(string key, object value);
IErrorBuilder SetExtension(string key, object? value);

IErrorBuilder RemoveExtension(string key);

Expand Down
31 changes: 0 additions & 31 deletions src/HotChocolate/Core/src/Abstractions/IErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;

namespace HotChocolate
{
Expand All @@ -14,34 +13,4 @@ public interface IErrorHandler
/// <returns></returns>
IErrorBuilder CreateUnexpectedError(Exception exception);
}

public static class ErrorHandlerExtensions
{
public static IEnumerable<IError> Handle(
this IErrorHandler errorHandler,
IEnumerable<IError> errors)
{
if (errorHandler == null)
{
throw new ArgumentNullException(nameof(errorHandler));
}

if (errors == null)
{
throw new ArgumentNullException(nameof(errors));
}

return HandleEnumerator(errorHandler, errors);
}

private static IEnumerable<IError> HandleEnumerator(
IErrorHandler errorHandler,
IEnumerable<IError> errors)
{
foreach (IError error in errors)
{
yield return errorHandler.Handle(error);
}
}
}
}
2 changes: 2 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public IReadOnlyList<object> ToList()
default:
throw new NotSupportedException();
}

current = current.Parent;
}

return stack;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ internal static class ClassMiddlewareFactory
internal static QueryMiddleware Create<TMiddleware>()
where TMiddleware : class
{
/*
return next =>
{
MiddlewareFactory<TMiddleware, QueryDelegate> factory =
MiddlewareActivator.CompileFactory<TMiddleware, QueryDelegate>();
MiddlewareCompiler.CompileFactory<TMiddleware, QueryDelegate>();
return CreateDelegate((s, n) => factory(s, n), next);
};
*/

throw new NotImplementedException();
}

internal static QueryMiddleware Create<TMiddleware>(
Expand All @@ -29,11 +33,12 @@ internal static QueryDelegate CreateDelegate<TMiddleware>(
QueryDelegate next)
where TMiddleware : class
{
/*
object sync = new object();
TMiddleware middleware = null;
ClassQueryDelegate<TMiddleware, IQueryContext> compiled =
MiddlewareActivator.CompileMiddleware<TMiddleware, IQueryContext>();
MiddlewareCompiler.CompileMiddleware<TMiddleware, IQueryContext>();
return context =>
{
Expand All @@ -48,8 +53,12 @@ internal static QueryDelegate CreateDelegate<TMiddleware>(
}
}
return compiled(context, context.Services, middleware);
throw new NotImplementedException();
// return compiled(context, context.Services, middleware);
};
*/
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public DocumentNode GetOrParseDocument(string queryId, IQuery query, Func<IQuery
throw new NotImplementedException();
}

public void TryAddDocument(string queryId, DocumentNode document)
{
throw new NotImplementedException();
}

public bool TryGetDocument(string key, out DocumentNode document)
{
if (_cache.TryGet(key, out ICachedQuery query))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ public T Service<T>() =>
public object Service(Type service) =>
_middlewareContext.Service(service);

public Task<T> ResolveAsync<T>() =>
_middlewareContext.ResolveAsync<T>();

public IReadOnlyList<IFieldSelection> CollectFields(
ObjectType typeContext) =>
_middlewareContext.CollectFields(typeContext);
Expand Down Expand Up @@ -148,5 +145,10 @@ public Optional<T> ArgumentOptional<T>(NameString name)
{
throw new NotImplementedException();
}

ValueTask<T> IMiddlewareContext.ResolveAsync<T>()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static FieldDelegate BuildComponent(
{
if (HasErrors(context.Result))
{
return Task.CompletedTask;
return default(ValueTask);
}
return component.Invoke(
Expand Down
Loading

0 comments on commit 7f63f0d

Please sign in to comment.