Skip to content

Commit 580af9e

Browse files
authored
Less allocations in FieldMiddlewareBuilder (graphql-dotnet#1268)
* remove IFieldMiddlewareBuilder.Build * Assert -> Should * Shouldly 4.0.0-beta for ThrowAsync fix * netcoreapp unification * fixes graphql-dotnet#492 * remove ExecutionOptions.SetFieldMiddleware
1 parent 492c95c commit 580af9e

28 files changed

+138
-110
lines changed

src/GraphQL.ApiTests/ApiApprovalTests.PublicApi.approved.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ namespace GraphQL
262262
public object Root { get; set; }
263263
public GraphQL.Types.ISchema Schema { get; set; }
264264
public GraphQL.Introspection.ISchemaFilter SchemaFilter { get; set; }
265-
public bool SetFieldMiddleware { get; set; }
266265
public bool ThrowOnUnhandledException { get; set; }
267266
public System.Func<GraphQL.Execution.ExecutionContext, System.Exception, System.Exception> UnhandledExceptionDelegate { get; set; }
268267
public System.Collections.Generic.IDictionary<string, object> UserContext { get; set; }
@@ -705,7 +704,6 @@ namespace GraphQL.Instrumentation
705704
public interface IFieldMiddlewareBuilder
706705
{
707706
void ApplyTo(GraphQL.Types.ISchema schema);
708-
GraphQL.Instrumentation.FieldMiddlewareDelegate Build(GraphQL.Instrumentation.FieldMiddlewareDelegate start = null);
709707
GraphQL.Instrumentation.IFieldMiddlewareBuilder Use(System.Func<GraphQL.Instrumentation.FieldMiddlewareDelegate, GraphQL.Instrumentation.FieldMiddlewareDelegate> middleware);
710708
}
711709
public class InstrumentFieldsMiddleware

src/GraphQL.DataLoader.Tests/GraphQL.DataLoader.Tests.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
56
<IsPackable>false</IsPackable>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<PackageReference Include="Bogus" Version="28.0.2" />
10+
<PackageReference Include="Bogus" Version="28.0.3" />
1011
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1213
<PackageReference Include="Moq" Version="4.12.0" />
1314
<PackageReference Include="Nito.AsyncEx.Context" Version="5.0.0" />
14-
<PackageReference Include="Shouldly" Version="3.0.2" />
15+
<PackageReference Include="Shouldly" Version="4.0.0-beta0002" />
1516
<PackageReference Include="xunit" Version="2.4.1" />
1617
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
1718
<PrivateAssets>all</PrivateAssets>

src/GraphQL.DataLoader.Tests/SimpleDataLoaderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
using GraphQL.DataLoader.Tests.Models;
2+
using GraphQL.DataLoader.Tests.Stores;
3+
using Moq;
4+
using Shouldly;
15
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Threading;
59
using System.Threading.Tasks;
6-
using GraphQL.DataLoader.Tests.Models;
7-
using GraphQL.DataLoader.Tests.Stores;
8-
using Moq;
9-
using Shouldly;
1010
using Xunit;
1111

1212
namespace GraphQL.DataLoader.Tests
@@ -75,7 +75,7 @@ public async Task Operation_Can_Be_Cancelled()
7575

7676
await loader.DispatchAsync(cts.Token);
7777

78-
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
78+
await Should.ThrowAsync<TaskCanceledException>(async () =>
7979
{
8080
await task;
8181
});
@@ -103,7 +103,7 @@ public async Task Operation_Cancelled_Before_Dispatch_Does_Not_Execute()
103103

104104
await loader.DispatchAsync(cts.Token);
105105

106-
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
106+
await Should.ThrowAsync<TaskCanceledException>(async () =>
107107
{
108108
await task;
109109
});

src/GraphQL.Harness.Tests/BasicTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public class BasicTests : SystemTestBase<Startup>
1010
[Fact]
1111
public async Task hero()
1212
{
13-
await run(_ =>
13+
await run(scenario =>
1414
{
1515
var input = new GraphQLRequest
1616
{
1717
Query = @"{ hero { name} }"
1818
};
19-
_.Post.Json(input).ToUrl("/api/graphql");
20-
_.StatusCodeShouldBe(HttpStatusCode.OK);
21-
_.GraphQL().ShouldBeSuccess(@"{ ""hero"": { ""name"": ""R2-D2"" }}");
19+
scenario.Post.Json(input).ToUrl("/api/graphql");
20+
scenario.StatusCodeShouldBe(HttpStatusCode.OK);
21+
scenario.GraphQL().ShouldBeSuccess(@"{ ""hero"": { ""name"": ""R2-D2"" }}");
2222
});
2323
}
2424
}

src/GraphQL.Harness.Tests/GraphQL.Harness.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="Alba" Version="3.0.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
13-
<PackageReference Include="Shouldly" Version="3.0.2" />
13+
<PackageReference Include="Shouldly" Version="4.0.0-beta0002" />
1414
<PackageReference Include="xunit" Version="2.4.1" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
1616
<PrivateAssets>all</PrivateAssets>

src/GraphQL.Harness/GraphQL.Harness.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
56
</PropertyGroup>
67

78
<ItemGroup>

src/GraphQL.Harness/GraphQLMiddleware.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ private async Task ExecuteAsync(HttpContext context, ISchema schema)
5454

5555
var request = Deserialize<GraphQLRequest>(context.Request.Body);
5656

57-
var result = await _executer.ExecuteAsync(_ =>
57+
var result = await _executer.ExecuteAsync(options =>
5858
{
59-
_.Schema = schema;
60-
_.Query = request.Query;
61-
_.OperationName = request.OperationName;
62-
_.Inputs = request.Variables.ToInputs();
63-
_.UserContext = _settings.BuildUserContext?.Invoke(context);
64-
_.EnableMetrics = _settings.EnableMetrics;
65-
_.ExposeExceptions = _settings.ExposeExceptions;
59+
options.Schema = schema;
60+
options.Query = request.Query;
61+
options.OperationName = request.OperationName;
62+
options.Inputs = request.Variables.ToInputs();
63+
options.UserContext = _settings.BuildUserContext?.Invoke(context);
64+
options.EnableMetrics = _settings.EnableMetrics;
65+
options.ExposeExceptions = _settings.ExposeExceptions;
6666
if (_settings.EnableMetrics)
6767
{
68-
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
68+
options.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
6969
}
7070
});
7171

src/GraphQL.Tests/Bugs/Bug252ExecutorAppliesFIeldBuilderOnce.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
21
using GraphQL.Instrumentation;
32
using GraphQL.Types;
3+
using Shouldly;
4+
using System;
45
using Xunit;
56

67
namespace GraphQL.Tests.Bugs
@@ -9,10 +10,11 @@ namespace GraphQL.Tests.Bugs
910
/// This class adds a variable to count the calls to ApplyTo()
1011
/// in the FieldMiddlewareBuilder class
1112
/// </summary>
12-
public class ApplyCounterMiddlewareBuilder : GraphQL.Instrumentation.IFieldMiddlewareBuilder
13+
public class ApplyCounterMiddlewareBuilder : IFieldMiddlewareBuilder
1314
{
1415
public int AppliedCount;
15-
IFieldMiddlewareBuilder overriddenBuilder = new FieldMiddlewareBuilder();
16+
private FieldMiddlewareBuilder overriddenBuilder = new FieldMiddlewareBuilder();
17+
1618
public void ApplyTo(ISchema schema)
1719
{
1820
AppliedCount++;
@@ -29,6 +31,7 @@ public IFieldMiddlewareBuilder Use(Func<FieldMiddlewareDelegate, FieldMiddleware
2931
return overriddenBuilder.Use(middleware);
3032
}
3133
}
34+
3235
public class Bug252ExecutorAppliesBuilderOnceTests
3336
{
3437
[Fact]
@@ -42,7 +45,7 @@ public void apply_to_not_called_without_execute()
4245
// no execute in this test
4346
//docExec.ExecuteAsync(execOptions).Wait();
4447

45-
Assert.Equal(0, mockMiddleware.AppliedCount);
48+
mockMiddleware.AppliedCount.ShouldBe(0);
4649
}
4750
[Fact]
4851
public void apply_to_called_once()
@@ -58,7 +61,7 @@ public void apply_to_called_once()
5861

5962
docExec.ExecuteAsync(execOptions).Wait();
6063

61-
Assert.Equal(1, mockMiddleware.AppliedCount);
64+
mockMiddleware.AppliedCount.ShouldBe(1);
6265
}
6366

6467
[Fact]
@@ -76,7 +79,7 @@ public void apply_to_called_once_with_multiple_execute()
7679
docExec.ExecuteAsync(execOptions).Wait();
7780
docExec.ExecuteAsync(execOptions).Wait();
7881

79-
Assert.Equal(1, mockMiddleware.AppliedCount);
82+
mockMiddleware.AppliedCount.ShouldBe(1);
8083
}
8184
}
8285
}

src/GraphQL.Tests/Execution/Performance/ListPerformanceTests.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using GraphQL.Conversion;
2+
using GraphQL.Types;
3+
using GraphQL.Utilities;
4+
using Shouldly;
15
using System;
26
using System.Collections.Generic;
37
using System.Diagnostics;
48
using System.Threading.Tasks;
5-
using GraphQL.Conversion;
6-
using GraphQL.Types;
7-
using GraphQL.Utilities;
89
using Xunit;
910
using Xunit.Abstractions;
1011

@@ -109,7 +110,6 @@ query AQuery {
109110
var runResult2 = await Executer.ExecuteAsync(_ =>
110111
{
111112
_.EnableMetrics = false;
112-
_.SetFieldMiddleware = false;
113113
_.Schema = Schema;
114114
_.Query = query;
115115
_.Root = PeopleList;
@@ -124,8 +124,8 @@ query AQuery {
124124

125125
_output.WriteLine($"Total Milliseconds: {smallListTimer.ElapsedMilliseconds}");
126126

127-
Assert.Null(runResult2.Errors);
128-
Assert.True(smallListTimer.ElapsedMilliseconds < 6000 * 2); //machine specific data with a buffer
127+
runResult2.Errors.ShouldBeNull();
128+
smallListTimer.ElapsedMilliseconds.ShouldBeLessThan(6000 * 2); //machine specific data with a buffer
129129
}
130130

131131
[Fact(Skip = "Benchmarks only, these numbers are machine dependant.")]
@@ -145,7 +145,6 @@ query AQuery {
145145

146146
var runResult2 = await Executer.ExecuteAsync(_ =>
147147
{
148-
_.SetFieldMiddleware = false;
149148
_.EnableMetrics = false;
150149
_.Schema = Schema;
151150
_.Query = query;
@@ -161,8 +160,8 @@ query AQuery {
161160

162161
_output.WriteLine($"Total Milliseconds: {smallListTimer.ElapsedMilliseconds}");
163162

164-
Assert.Null(runResult2.Errors);
165-
Assert.True(smallListTimer.ElapsedMilliseconds < 700 * 2); //machine specific data with a buffer
163+
runResult2.Errors.ShouldBeNull();
164+
smallListTimer.ElapsedMilliseconds.ShouldBeLessThan(700 * 2); //machine specific data with a buffer
166165
}
167166

168167
[Fact(Skip = "Benchmarks only, these numbers are machine dependant.")]
@@ -194,7 +193,6 @@ ... on Cat {
194193

195194
var runResult2 = await Executer.ExecuteAsync(_ =>
196195
{
197-
_.SetFieldMiddleware = false;
198196
_.EnableMetrics = false;
199197
_.Schema = Schema;
200198
_.Query = query;
@@ -210,8 +208,8 @@ ... on Cat {
210208

211209
_output.WriteLine($"Total Milliseconds: {smallListTimer.ElapsedMilliseconds}");
212210

213-
Assert.Null(runResult2.Errors);
214-
Assert.True(smallListTimer.ElapsedMilliseconds < 5600 * 2); //machine specific data with a buffer
211+
runResult2.Errors.ShouldBeNull();
212+
smallListTimer.ElapsedMilliseconds.ShouldBeLessThan(5600 * 2); //machine specific data with a buffer
215213
}
216214
}
217215

src/GraphQL.Tests/Execution/Performance/StarWarsPerformanceTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System.Diagnostics;
21
using GraphQL.Conversion;
32
using GraphQL.Tests.StarWars;
3+
using Shouldly;
4+
using System.Diagnostics;
45
using Xunit;
56
using Xunit.Abstractions;
67

@@ -42,7 +43,6 @@ query HeroNameAndFriendsQuery {
4243
{
4344
runResult2 = Executer.ExecuteAsync(_ =>
4445
{
45-
_.SetFieldMiddleware = false;
4646
_.EnableMetrics = false;
4747
_.Schema = Schema;
4848
_.Query = query;
@@ -59,8 +59,8 @@ query HeroNameAndFriendsQuery {
5959

6060
_output.WriteLine($"Milliseconds: {smallListTimer.ElapsedMilliseconds}");
6161

62-
Assert.Null(runResult2.Errors);
63-
Assert.True(smallListTimer.ElapsedMilliseconds < 9400 * 2); //machine specific data with a buffer
62+
runResult2.Errors.ShouldBeNull();
63+
smallListTimer.ElapsedMilliseconds.ShouldBeLessThan(9400 * 2); //machine specific data with a buffer
6464
}
6565
}
6666
}

src/GraphQL.Tests/Execution/Performance/ThreadPerformanceTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ query HeroNameAndFriendsQuery {
8383
var runResult2 = Executer.ExecuteAsync(_ =>
8484
{
8585
_.EnableMetrics = false;
86-
_.SetFieldMiddleware = false;
8786
_.Schema = Schema;
8887
_.Query = query;
8988
}).GetAwaiter().GetResult();

src/GraphQL.Tests/Execution/ResolveFieldContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void long_to_int_should_throw_for_out_of_range()
4444
{
4545
long val = 89429901947254093;
4646
_context.Arguments["a"] = val;
47-
Assert.Throws<OverflowException>(() => _context.GetArgument<int>("a"));
47+
Should.Throw<OverflowException>(() => _context.GetArgument<int>("a"));
4848
}
4949

5050
[Fact]

src/GraphQL.Tests/GraphQL.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
5+
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
56
<AssemblyName>GraphQL.Tests</AssemblyName>
67
<PackageId>GraphQL.Tests</PackageId>
78
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
@@ -36,7 +37,7 @@
3637
</PackageReference>
3738
<PackageReference Include="xunit.runner.reporters" Version="2.4.1" />
3839
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
39-
<PackageReference Include="Shouldly" Version="3.0.2" />
40+
<PackageReference Include="Shouldly" Version="4.0.0-beta0002" />
4041
</ItemGroup>
4142

4243
<ItemGroup>

src/GraphQL.Tests/Instrumentation/ApolloTracingTests.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
41
using GraphQL.Instrumentation;
52
using GraphQL.Tests.StarWars;
63
using Shouldly;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
77
using Xunit;
88

99
namespace GraphQL.Tests.Instrumentation
1010
{
1111
public class ApolloTracingTests : StarWarsTestBase
1212
{
13-
public ApolloTracingTests()
14-
: base()
15-
{
16-
}
17-
1813
[Fact]
1914
public void extension_has_expected_format()
2015
{
@@ -44,8 +39,8 @@ public void extension_has_expected_format()
4439
trace.Parsing.Duration.ShouldNotBe(0);
4540
trace.Validation.StartOffset.ShouldNotBe(0);
4641
trace.Validation.Duration.ShouldNotBe(0);
47-
Assert.NotEqual(trace.Validation.StartOffset, trace.Parsing.StartOffset);
48-
Assert.NotEqual(trace.Validation.Duration, trace.Parsing.Duration);
42+
trace.Validation.StartOffset.ShouldNotBeSameAs(trace.Parsing.StartOffset);
43+
trace.Validation.Duration.ShouldNotBeSameAs(trace.Parsing.Duration);
4944

5045
var expectedPaths = new HashSet<List<object>>
5146
{

src/GraphQL.Tests/Types/BooleanGraphTypeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void coerces_zero_string_to_false()
6262
[InlineData("21")]
6363
public void coerces_input_to_exception(string input)
6464
{
65-
FormatException formatException = Assert.Throws<FormatException>(() => type.ParseValue(input));
65+
FormatException formatException = Should.Throw<FormatException>(() => type.ParseValue(input));
6666
formatException.Message.ShouldBe("String was not recognized as a valid Boolean.");
6767
}
6868

src/GraphQL.Tests/Types/DateGraphTypeTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public void coerces_invalid_string_to_exception()
7575
{
7676
CultureTestHelper.UseCultures(() =>
7777
{
78-
Assert.Throws<FormatException>(
79-
()=>_type.ParseValue("some unknown date"));
78+
Should.Throw<FormatException>(() => _type.ParseValue("some unknown date"));
8079
});
8180
}
8281

@@ -85,8 +84,7 @@ public void coerces_invalidly_formatted_date_to_exception()
8584
{
8685
CultureTestHelper.UseCultures(() =>
8786
{
88-
Assert.Throws<FormatException>(
89-
()=> _type.ParseValue("Dec 32 2012"));
87+
Should.Throw<FormatException>(() => _type.ParseValue("Dec 32 2012"));
9088
});
9189
}
9290

0 commit comments

Comments
 (0)