Skip to content

Commit 25bc258

Browse files
committed
Add MiniProfiler Fields Middleware
1 parent 201791a commit 25bc258

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using GraphQL.Instrumentation;
2+
using GraphQL.Types;
3+
using StackExchange.Profiling;
4+
using System.Threading.Tasks;
5+
6+
namespace Our.Umbraco.GraphQL.Instrumentation
7+
{
8+
public class MiniProfilerFieldsMiddleware
9+
{
10+
public Task<object> Resolve(ResolveFieldContext context, FieldMiddlewareDelegate next)
11+
{
12+
using (MiniProfiler.Current.Step($"[GraphQL] Resolving {string.Join(".", context.Path)}", ProfileLevel.Info))
13+
{
14+
return next(context);
15+
}
16+
}
17+
}
18+
}

src/Our.Umbraco.GraphQL/Our.Umbraco.GraphQL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<Compile Include="Filters\NotFilter.cs" />
252252
<Compile Include="Filters\OrFilter.cs" />
253253
<Compile Include="Filters\StartsWithFilter.cs" />
254+
<Compile Include="Instrumentation\MiniProfilerFieldsMiddleware.cs" />
254255
<Compile Include="Schema\Conventions.cs" />
255256
<Compile Include="Types\ConnectionExtensions.cs" />
256257
<Compile Include="Types\ConnectionWithFilterBuilder.cs" />

src/Our.Umbraco.GraphQL/Web/GraphQLMiddleware.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
using GraphQL.Instrumentation;
99
using GraphQL.Utilities;
1010
using Microsoft.Owin;
11+
using Newtonsoft.Json;
12+
using Newtonsoft.Json.Linq;
13+
using Newtonsoft.Json.Serialization;
14+
using Our.Umbraco.GraphQL.Instrumentation;
1115
using Our.Umbraco.GraphQL.Schema;
1216
using StackExchange.Profiling;
1317
using Umbraco.Core;
@@ -75,7 +79,7 @@ public override async Task Invoke(IOwinContext context)
7579
Inputs variables = requestParams.Variables;
7680

7781
var start = DateTime.Now;
78-
82+
MiniProfiler.Start();
7983
var result = await _documentExecutor
8084
.ExecuteAsync(x =>
8185
{
@@ -86,6 +90,7 @@ public override async Task Invoke(IOwinContext context)
8690
{
8791
x.EnableMetrics = true;
8892
x.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
93+
x.FieldMiddleware.Use<MiniProfilerFieldsMiddleware>();
8994
}
9095
x.FieldNameConverter = new DefaultFieldNameConverter();
9196
x.Inputs = variables;
@@ -104,7 +109,14 @@ public override async Task Invoke(IOwinContext context)
104109
if (_options.EnableMetrics && result.Errors == null)
105110
{
106111
result.EnrichWithApolloTracing(start);
112+
113+
if (result.Extensions == null)
114+
{
115+
result.Extensions = new Dictionary<string, object>();
116+
}
117+
result.Extensions["miniProfiler"] = JObject.FromObject(MiniProfiler.Current, new JsonSerializer { ContractResolver = new CamelCasePropertyNamesContractResolver() });
107118
}
119+
MiniProfiler.Stop();
108120

109121
return result;
110122
});

0 commit comments

Comments
 (0)