Skip to content

Commit fba26a5

Browse files
authored
Update to GraphQL 4 (#456)
1 parent 36af7e5 commit fba26a5

13 files changed

Lines changed: 45 additions & 28 deletions

readme.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ Validators need to be added to the `ValidatorTypeCache`. This should be done onc
9999
ValidatorTypeCache validatorTypeCache = new();
100100
validatorTypeCache.AddValidatorsFromAssembly(assemblyContainingValidators);
101101
Schema schema = new();
102+
schema.UseFluentValidation();
102103
DocumentExecuter executer = new();
103104
```
104-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L18-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-startconfig' title='Start of snippet'>anchor</a></sup>
105+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L18-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-startconfig' title='Start of snippet'>anchor</a></sup>
105106
<!-- endSnippet -->
106107

107108
Generally `ValidatorTypeCache` is scoped per app and can be collocated with `Schema`, `DocumentExecuter` initialization.
@@ -129,7 +130,7 @@ options.UseFluentValidation(validatorTypeCache);
129130

130131
var executionResult = await executer.ExecuteAsync(options);
131132
```
132-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L30-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefluentvalidation' title='Start of snippet'>anchor</a></sup>
133+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L31-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefluentvalidation' title='Start of snippet'>anchor</a></sup>
133134
<!-- endSnippet -->
134135

135136

@@ -156,7 +157,7 @@ public class MyUserContext :
156157
public string MyProperty { get; }
157158
}
158159
```
159-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L45-L58' title='Snippet source file'>snippet source</a> | <a href='#snippet-contextimplementingdictionary' title='Start of snippet'>anchor</a></sup>
160+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L46-L59' title='Snippet source file'>snippet source</a> | <a href='#snippet-contextimplementingdictionary' title='Start of snippet'>anchor</a></sup>
160161
<!-- endSnippet -->
161162

162163
The `ExecutionOptions.UserContext` can then be set as follows:
@@ -176,7 +177,7 @@ ExecutionOptions options = new()
176177
};
177178
options.UseFluentValidation(validatorTypeCache);
178179
```
179-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L62-L76' title='Snippet source file'>snippet source</a> | <a href='#snippet-executequerywithcontextimplementingdictionary' title='Start of snippet'>anchor</a></sup>
180+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L63-L77' title='Snippet source file'>snippet source</a> | <a href='#snippet-executequerywithcontextimplementingdictionary' title='Start of snippet'>anchor</a></sup>
180181
<!-- endSnippet -->
181182

182183

@@ -203,7 +204,7 @@ ExecutionOptions options = new()
203204
};
204205
options.UseFluentValidation(validatorTypeCache);
205206
```
206-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L81-L101' title='Snippet source file'>snippet source</a> | <a href='#snippet-executequerywithcontextinsidedictionary' title='Start of snippet'>anchor</a></sup>
207+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L82-L102' title='Snippet source file'>snippet source</a> | <a href='#snippet-executequerywithcontextinsidedictionary' title='Start of snippet'>anchor</a></sup>
207208
<!-- endSnippet -->
208209

209210

@@ -222,7 +223,7 @@ ExecutionOptions options = new()
222223
};
223224
options.UseFluentValidation(validatorTypeCache);
224225
```
225-
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L106-L116' title='Snippet source file'>snippet source</a> | <a href='#snippet-nocontext' title='Start of snippet'>anchor</a></sup>
226+
<sup><a href='/src/Tests/Snippets/QueryExecution.cs#L107-L117' title='Snippet source file'>snippet source</a> | <a href='#snippet-nocontext' title='Start of snippet'>anchor</a></sup>
226227
<!-- endSnippet -->
227228

228229
Then the `UseFluentValidation` method will instantiate it to a new `Dictionary<string, object>`.
@@ -354,10 +355,10 @@ public class QueryTests
354355
};
355356
ResolveFieldContext fieldContext = new()
356357
{
357-
Arguments = new Dictionary<string, object>
358+
Arguments = new Dictionary<string, ArgumentValue>
358359
{
359360
{
360-
"input", input
361+
"input", new ArgumentValue(input, ArgumentSource.Variable)
361362
}
362363
},
363364
UserContext = userContext
@@ -376,12 +377,14 @@ public class QueryTests
376377
FluentValidationExtensions.AddCacheToContext(
377378
userContext,
378379
ValidatorCacheBuilder.Instance);
380+
381+
var value = new Dictionary<string, object>();
379382
ResolveFieldContext fieldContext = new()
380383
{
381-
Arguments = new Dictionary<string, object>
384+
Arguments = new Dictionary<string, ArgumentValue>
382385
{
383386
{
384-
"input", new Dictionary<string, object>()
387+
"input", new ArgumentValue(value, ArgumentSource.Variable)
385388
}
386389
},
387390
UserContext = userContext
@@ -392,7 +395,7 @@ public class QueryTests
392395
}
393396
}
394397
```
395-
<sup><a href='/src/SampleWeb.Tests/QueryTests.cs#L9-L67' title='Snippet source file'>snippet source</a> | <a href='#snippet-querytests' title='Start of snippet'>anchor</a></sup>
398+
<sup><a href='/src/SampleWeb.Tests/QueryTests.cs#L10-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-querytests' title='Start of snippet'>anchor</a></sup>
396399
<!-- endSnippet -->
397400

398401

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>5.2.0</Version>
3+
<Version>6.0.0</Version>
44
<AssemblyVersion>1.0.0</AssemblyVersion>
55
<PackageTags>GraphQL, Validation, FluentValidation</PackageTags>
66
<Description>Add FluentValidation (https://fluentvalidation.net/) support to GraphQL.net (https://github.com/graphql-dotnet/graphql-dotnet)</Description>

src/GraphQL.FluentValidation/FluentValidationExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FluentValidation;
22
using GraphQL.FluentValidation;
33
using GraphQL.Instrumentation;
4+
using GraphQL.Types;
45

56
namespace GraphQL
67
{
@@ -19,9 +20,18 @@ public static ExecutionOptions UseFluentValidation(this ExecutionOptions executi
1920

2021
validatorTypeCache.Freeze();
2122
executionOptions.SetCache(validatorTypeCache);
22-
ValidationMiddleware validationMiddleware = new();
23-
executionOptions.FieldMiddleware.Use(validationMiddleware);
2423
return executionOptions;
2524
}
25+
26+
/// <summary>
27+
/// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./>
28+
/// </summary>
29+
public static void UseFluentValidation(this Schema schema)
30+
{
31+
Guard.AgainstNull(schema, nameof(schema));
32+
33+
ValidationMiddleware validationMiddleware = new();
34+
schema.FieldMiddleware.Use(validationMiddleware);
35+
}
2636
}
2737
}

src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
</PropertyGroup>
55
<ItemGroup>
66
<PackageReference Include="FluentValidation" Version="9.5.3" />
7-
<PackageReference Include="GraphQL" Version="3.3.2" />
7+
<PackageReference Include="GraphQL" Version="4.0.2" />
8+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
89
<PackageReference Include="ProjectDefaults" Version="1.0.54" PrivateAssets="All" />
910
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
1011
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" />

src/GraphQL.FluentValidation/ValidatorTypeCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Linq;
66
using System.Reflection;
77
using FluentValidation;
8-
using GraphQL.Utilities;
8+
using Microsoft.Extensions.DependencyInjection;
99

1010
namespace GraphQL.FluentValidation
1111
{

src/SampleWeb.Tests/QueryTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
using System.Threading.Tasks;
44
using FluentValidation;
55
using GraphQL;
6+
using GraphQL.Execution;
67
using VerifyXunit;
78
using Xunit;
89

910
#region QueryTests
11+
1012
[UsesVerify]
1113
public class QueryTests
1214
{
@@ -26,10 +28,10 @@ public Task RunInputQuery()
2628
};
2729
ResolveFieldContext fieldContext = new()
2830
{
29-
Arguments = new Dictionary<string, object>
31+
Arguments = new Dictionary<string, ArgumentValue>
3032
{
3133
{
32-
"input", input
34+
"input", new ArgumentValue(input, ArgumentSource.Variable)
3335
}
3436
},
3537
UserContext = userContext
@@ -48,12 +50,14 @@ public Task RunInvalidInputQuery()
4850
FluentValidationExtensions.AddCacheToContext(
4951
userContext,
5052
ValidatorCacheBuilder.Instance);
53+
54+
var value = new Dictionary<string, object>();
5155
ResolveFieldContext fieldContext = new()
5256
{
53-
Arguments = new Dictionary<string, object>
57+
Arguments = new Dictionary<string, ArgumentValue>
5458
{
5559
{
56-
"input", new Dictionary<string, object>()
60+
"input", new ArgumentValue(value, ArgumentSource.Variable)
5761
}
5862
},
5963
UserContext = userContext

src/SampleWeb.Tests/SampleWeb.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net5</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="GraphQL" Version="3.3.2" />
6+
<PackageReference Include="GraphQL" Version="4.0.2" />
77
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.4" />
88
<PackageReference Include="Xunit" Version="2.4.1" />
99
<PackageReference Include="Verify.Xunit" Version="11.0.3" />

src/SampleWeb/SampleWeb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Folder Include="wwwroot\" />
77
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="9.5.3" />
88
<PackageReference Include="graphiql" Version="2.0.0" />
9-
<PackageReference Include="GraphQL.NewtonsoftJson" Version="3.3.2" />
9+
<PackageReference Include="GraphQL.NewtonsoftJson" Version="4.0.2" />
1010
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4" />
1111
<ProjectReference Include="..\GraphQL.FluentValidation\GraphQL.FluentValidation.csproj" />
1212
<PackageReference Include="ProjectDefaults" Version="1.0.54" PrivateAssets="All" />

src/SampleWeb/Schema.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public class Schema : GraphQL.Types.Schema
55
public Schema(IServiceProvider serviceProvider, Query query) :
66
base(serviceProvider)
77
{
8+
RegisterTypeMapping(typeof(MyInput),typeof(MyInputGraph));
9+
RegisterTypeMapping(typeof(Result),typeof(ResultGraph));
810
Query = query;
911
}
1012
}

src/SampleWeb/Startup.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using GraphQL;
77
using GraphQL.NewtonsoftJson;
88
using GraphQL.Types;
9-
using GraphQL.Utilities;
109
using Microsoft.AspNetCore.Builder;
1110
using Microsoft.AspNetCore.Mvc;
1211
using Microsoft.Extensions.DependencyInjection;
@@ -15,9 +14,6 @@ public class Startup
1514
{
1615
public void ConfigureServices(IServiceCollection services)
1716
{
18-
GraphTypeTypeRegistry.Register<MyInput, MyInputGraph>();
19-
GraphTypeTypeRegistry.Register<Result, ResultGraph>();
20-
2117
foreach (var type in GetGraphQLTypes())
2218
{
2319
services.AddSingleton(type);

0 commit comments

Comments
 (0)