Skip to content

Commit

Permalink
Merge pull request #61 from graphql-dotnet/profiling
Browse files Browse the repository at this point in the history
Fix field instrumentation and add support for field middleware
  • Loading branch information
tlil authored Aug 16, 2017
2 parents 43e821e + d7f05f0 commit 1eecb44
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public Task<Dictionary<string, object>> ExecuteFieldsAsync(ExecutionContext cont
public async Task<ResolveFieldResult<object>> ResolveFieldAsync(ExecutionContext context, IObjectGraphType parentType, object source, Fields fields, IEnumerable<string> path)
{
context.CancellationToken.ThrowIfCancellationRequested();
var fieldPath = path?.ToList() ?? new List<string>();

var resolveResult = new ResolveFieldResult<object>
{
Expand Down Expand Up @@ -306,6 +307,7 @@ public async Task<ResolveFieldResult<object>> ResolveFieldAsync(ExecutionContext
resolveContext.CancellationToken = context.CancellationToken;
resolveContext.Metrics = context.Metrics;
resolveContext.Errors = context.Errors;
resolveContext.Path.AddRange(fieldPath);

var resolver = fieldDefinition.Resolver ?? new NameFieldResolver();
var result = resolver.Resolve(resolveContext);
Expand All @@ -319,7 +321,7 @@ public async Task<ResolveFieldResult<object>> ResolveFieldAsync(ExecutionContext
var exception = aggregateException.InnerExceptions.Count == 1
? aggregateException.InnerException
: aggregateException;
return GenerateError(resolveResult, field, context, exception, path);
return GenerateError(resolveResult, field, context, exception, fieldPath);
}
await task.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ namespace GraphQL.Instrumentation
{
public class InstrumentFieldsMiddleware
{
public Task<object> Resolve(ResolveFieldContext context, FieldMiddlewareDelegate next)
public async Task<object> Resolve(ResolveFieldContext context, FieldMiddlewareDelegate next)
{
var metadata = new Dictionary<string, object>
{
{"typeName", context.ParentType.Name},
{"fieldName", context.FieldName}
{"fieldName", context.FieldName},
{"path", context.Path},
{"arguments", context.Arguments},

};
var path = $"{context.ParentType.Name}.{context.FieldName}";

using (context.Metrics.Subject("field", context.FieldName, metadata))
using (context.Metrics.Subject("field", path, metadata))
{
return next(context);
return await next(context).ConfigureAwait(false);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions deps/graphql-dotnet/src/GraphQL/Types/ResolveFieldContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ResolveFieldContext<TSource>

public ExecutionErrors Errors { get; set; }

public List<string> Path { get; private set; } = new List<string>();

public ResolveFieldContext() { }

public ResolveFieldContext(ResolveFieldContext context)
Expand Down
18 changes: 18 additions & 0 deletions src/GraphQL.Conventions/Adapters/Engine/GraphQLEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class GraphQLEngine

private List<System.Type> _schemaTypes = new List<System.Type>();

private List<System.Type> _middleware = new List<System.Type>();

private class NoopValidationRule : IValidationRule
{
public INodeVisitor Validate(ValidationContext context)
Expand Down Expand Up @@ -154,6 +156,17 @@ public GraphQLEngine WithAttributesFromAssemblies(IEnumerable<System.Type> assem
return this;
}

public GraphQLEngine WithMiddleware(System.Type type)
{
_middleware.Add(type);
return this;
}

public GraphQLEngine WithMiddleware<T>()
{
return WithMiddleware(typeof(T));
}

public GraphQLEngine BuildSchema(params System.Type[] types)
{
if (_schema == null)
Expand Down Expand Up @@ -232,6 +245,11 @@ internal async Task<ExecutionResult> Execute(
configuration.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
}

foreach (var middleware in _middleware)
{
configuration.FieldMiddleware.Use(middleware);
}

var result = await _documentExecutor.ExecuteAsync(configuration).ConfigureAwait(false);

if (result.Errors != null)
Expand Down
2 changes: 2 additions & 0 deletions src/GraphQL.Conventions/Adapters/ResolutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void SetArgument(string name, object value)

public CancellationToken CancellationToken => FieldContext.CancellationToken;

public IEnumerable<string> Path => FieldContext.Path;

public ResolveFieldContext FieldContext { get; private set; }
}
}
4 changes: 2 additions & 2 deletions src/GraphQL.Conventions/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[assembly: AssemblyCopyright("Copyright 2016-2017 Tommy Lillehagen. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.2.15")]
[assembly: AssemblyInformationalVersion("1.2.15")]
[assembly: AssemblyFileVersion("1.2.16")]
[assembly: AssemblyInformationalVersion("1.2.16")]
[assembly: CLSCompliant(false)]

[assembly: InternalsVisibleTo("Tests")]
6 changes: 6 additions & 0 deletions src/GraphQL.Conventions/Execution/IResolutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Threading;
using GraphQL.Conventions.Types.Descriptors;
using GraphQL.Types;

namespace GraphQL.Conventions
{
Expand All @@ -22,5 +24,9 @@ public interface IResolutionContext
GraphFieldInfo FieldInfo { get; }

CancellationToken CancellationToken { get; }

IEnumerable<string> Path { get; }

ResolveFieldContext FieldContext { get; }
}
}
2 changes: 1 addition & 1 deletion src/GraphQL.Conventions/GraphQL.Conventions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>GraphQL Conventions for .NET</Description>
<VersionPrefix>1.2.15</VersionPrefix>
<VersionPrefix>1.2.16</VersionPrefix>
<Authors>Tommy Lillehagen</Authors>
<TargetFrameworks>netstandard1.5;net45</TargetFrameworks>
<DebugType>portable</DebugType>
Expand Down
19 changes: 17 additions & 2 deletions src/GraphQL.Conventions/Web/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class RequestHandlerBuilder : IDependencyInjector

readonly List<Type> _exceptionsTreatedAsWarnings = new List<Type>();

readonly List<Type> _middleware = new List<Type>();

IDependencyInjector _dependencyInjector;

ResolveTypeDelegate _resolveTypeDelegate;
Expand Down Expand Up @@ -127,6 +129,12 @@ public RequestHandlerBuilder WithComplexityConfiguration(ComplexityConfiguration
return this;
}

public RequestHandlerBuilder WithMiddleware<T>()
{
_middleware.Add(typeof(T));
return this;
}

public IRequestHandler Generate()
{
return new RequestHandlerImpl(
Expand All @@ -138,7 +146,8 @@ public IRequestHandler Generate()
_useProfiling,
_outputViolationsAsWarnings,
_fieldResolutionStrategy,
_complexityConfiguration);
_complexityConfiguration,
_middleware);
}

public object Resolve(TypeInfo typeInfo)
Expand Down Expand Up @@ -172,7 +181,8 @@ internal RequestHandlerImpl(
bool useProfiling,
bool outputViolationsAsWarnings,
FieldResolutionStrategy fieldResolutionStrategy,
ComplexityConfiguration complexityConfiguration)
ComplexityConfiguration complexityConfiguration,
IEnumerable<Type> middleware)
{
_dependencyInjector = dependencyInjector;
_engine.WithAttributesFromAssemblies(assemblyTypes);
Expand All @@ -183,6 +193,11 @@ internal RequestHandlerImpl(
_engine.WithFieldResolutionStrategy(fieldResolutionStrategy);
_engine.BuildSchema(schemaTypes.ToArray());
_complexityConfiguration = complexityConfiguration;

foreach (var type in middleware)
{
_engine.WithMiddleware(type);
}
}

public async Task<Response> ProcessRequest(Request request, IUserContext userContext)
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Conventions/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.2.15-*",
"version": "1.2.16-*",
"description": "GraphQL Conventions for .NET",
"authors": [
"Tommy Lillehagen"
Expand Down

0 comments on commit 1eecb44

Please sign in to comment.