Skip to content

Commit

Permalink
Fix field instrumentation and add support for field middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
tlil committed Aug 16, 2017
1 parent 43e821e commit a431239
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ 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},
{"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
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
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")]
3 changes: 3 additions & 0 deletions src/GraphQL.Conventions/Execution/IResolutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading;
using GraphQL.Conventions.Types.Descriptors;
using GraphQL.Types;

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

CancellationToken CancellationToken { 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 a431239

Please sign in to comment.