diff --git a/readme.md b/readme.md index d289ac13..59cca2af 100644 --- a/readme.md +++ b/readme.md @@ -99,9 +99,10 @@ Validators need to be added to the `ValidatorTypeCache`. This should be done onc ValidatorTypeCache validatorTypeCache = new(); validatorTypeCache.AddValidatorsFromAssembly(assemblyContainingValidators); Schema schema = new(); +schema.UseFluentValidation(); DocumentExecuter executer = new(); ``` -snippet source | anchor +snippet source | anchor Generally `ValidatorTypeCache` is scoped per app and can be collocated with `Schema`, `DocumentExecuter` initialization. @@ -129,7 +130,7 @@ options.UseFluentValidation(validatorTypeCache); var executionResult = await executer.ExecuteAsync(options); ``` -snippet source | anchor +snippet source | anchor @@ -156,7 +157,7 @@ public class MyUserContext : public string MyProperty { get; } } ``` -snippet source | anchor +snippet source | anchor The `ExecutionOptions.UserContext` can then be set as follows: @@ -176,7 +177,7 @@ ExecutionOptions options = new() }; options.UseFluentValidation(validatorTypeCache); ``` -snippet source | anchor +snippet source | anchor @@ -203,7 +204,7 @@ ExecutionOptions options = new() }; options.UseFluentValidation(validatorTypeCache); ``` -snippet source | anchor +snippet source | anchor @@ -222,7 +223,7 @@ ExecutionOptions options = new() }; options.UseFluentValidation(validatorTypeCache); ``` -snippet source | anchor +snippet source | anchor Then the `UseFluentValidation` method will instantiate it to a new `Dictionary`. @@ -354,10 +355,10 @@ public class QueryTests }; ResolveFieldContext fieldContext = new() { - Arguments = new Dictionary + Arguments = new Dictionary { { - "input", input + "input", new ArgumentValue(input, ArgumentSource.Variable) } }, UserContext = userContext @@ -376,12 +377,14 @@ public class QueryTests FluentValidationExtensions.AddCacheToContext( userContext, ValidatorCacheBuilder.Instance); + + var value = new Dictionary(); ResolveFieldContext fieldContext = new() { - Arguments = new Dictionary + Arguments = new Dictionary { { - "input", new Dictionary() + "input", new ArgumentValue(value, ArgumentSource.Variable) } }, UserContext = userContext @@ -392,7 +395,7 @@ public class QueryTests } } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 971e85f9..07499734 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@ - 5.2.0 + 6.0.0 1.0.0 GraphQL, Validation, FluentValidation Add FluentValidation (https://fluentvalidation.net/) support to GraphQL.net (https://github.com/graphql-dotnet/graphql-dotnet) diff --git a/src/GraphQL.FluentValidation/FluentValidationExtensions.cs b/src/GraphQL.FluentValidation/FluentValidationExtensions.cs index e3cd2f65..346d5b02 100644 --- a/src/GraphQL.FluentValidation/FluentValidationExtensions.cs +++ b/src/GraphQL.FluentValidation/FluentValidationExtensions.cs @@ -1,6 +1,7 @@ using FluentValidation; using GraphQL.FluentValidation; using GraphQL.Instrumentation; +using GraphQL.Types; namespace GraphQL { @@ -19,9 +20,18 @@ public static ExecutionOptions UseFluentValidation(this ExecutionOptions executi validatorTypeCache.Freeze(); executionOptions.SetCache(validatorTypeCache); - ValidationMiddleware validationMiddleware = new(); - executionOptions.FieldMiddleware.Use(validationMiddleware); return executionOptions; } + + /// + /// Adds a FieldMiddleware to the GraphQL pipeline that converts a to s./> + /// + public static void UseFluentValidation(this Schema schema) + { + Guard.AgainstNull(schema, nameof(schema)); + + ValidationMiddleware validationMiddleware = new(); + schema.FieldMiddleware.Use(validationMiddleware); + } } } \ No newline at end of file diff --git a/src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj b/src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj index 47ac9317..b389308c 100644 --- a/src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj +++ b/src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj @@ -4,7 +4,8 @@ - + + diff --git a/src/GraphQL.FluentValidation/ValidatorTypeCache.cs b/src/GraphQL.FluentValidation/ValidatorTypeCache.cs index 954a0a00..031e82d6 100644 --- a/src/GraphQL.FluentValidation/ValidatorTypeCache.cs +++ b/src/GraphQL.FluentValidation/ValidatorTypeCache.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Reflection; using FluentValidation; -using GraphQL.Utilities; +using Microsoft.Extensions.DependencyInjection; namespace GraphQL.FluentValidation { diff --git a/src/SampleWeb.Tests/QueryTests.cs b/src/SampleWeb.Tests/QueryTests.cs index c57aa23f..49fb33a6 100644 --- a/src/SampleWeb.Tests/QueryTests.cs +++ b/src/SampleWeb.Tests/QueryTests.cs @@ -3,10 +3,12 @@ using System.Threading.Tasks; using FluentValidation; using GraphQL; +using GraphQL.Execution; using VerifyXunit; using Xunit; #region QueryTests + [UsesVerify] public class QueryTests { @@ -26,10 +28,10 @@ public Task RunInputQuery() }; ResolveFieldContext fieldContext = new() { - Arguments = new Dictionary + Arguments = new Dictionary { { - "input", input + "input", new ArgumentValue(input, ArgumentSource.Variable) } }, UserContext = userContext @@ -48,12 +50,14 @@ public Task RunInvalidInputQuery() FluentValidationExtensions.AddCacheToContext( userContext, ValidatorCacheBuilder.Instance); + + var value = new Dictionary(); ResolveFieldContext fieldContext = new() { - Arguments = new Dictionary + Arguments = new Dictionary { { - "input", new Dictionary() + "input", new ArgumentValue(value, ArgumentSource.Variable) } }, UserContext = userContext diff --git a/src/SampleWeb.Tests/SampleWeb.Tests.csproj b/src/SampleWeb.Tests/SampleWeb.Tests.csproj index 32ed0716..7faf54d8 100644 --- a/src/SampleWeb.Tests/SampleWeb.Tests.csproj +++ b/src/SampleWeb.Tests/SampleWeb.Tests.csproj @@ -3,7 +3,7 @@ net5 - + diff --git a/src/SampleWeb/SampleWeb.csproj b/src/SampleWeb/SampleWeb.csproj index ed0fec72..5945e8b6 100644 --- a/src/SampleWeb/SampleWeb.csproj +++ b/src/SampleWeb/SampleWeb.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/SampleWeb/Schema.cs b/src/SampleWeb/Schema.cs index 40ba33b1..cb28f920 100644 --- a/src/SampleWeb/Schema.cs +++ b/src/SampleWeb/Schema.cs @@ -5,6 +5,8 @@ public class Schema : GraphQL.Types.Schema public Schema(IServiceProvider serviceProvider, Query query) : base(serviceProvider) { + RegisterTypeMapping(typeof(MyInput),typeof(MyInputGraph)); + RegisterTypeMapping(typeof(Result),typeof(ResultGraph)); Query = query; } } \ No newline at end of file diff --git a/src/SampleWeb/Startup.cs b/src/SampleWeb/Startup.cs index 1509085a..58b7d703 100644 --- a/src/SampleWeb/Startup.cs +++ b/src/SampleWeb/Startup.cs @@ -6,7 +6,6 @@ using GraphQL; using GraphQL.NewtonsoftJson; using GraphQL.Types; -using GraphQL.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; @@ -15,9 +14,6 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { - GraphTypeTypeRegistry.Register(); - GraphTypeTypeRegistry.Register(); - foreach (var type in GetGraphQLTypes()) { services.AddSingleton(type); diff --git a/src/Tests/QueryExecutor.cs b/src/Tests/QueryExecutor.cs index d4fb1400..5c4a4b3a 100644 --- a/src/Tests/QueryExecutor.cs +++ b/src/Tests/QueryExecutor.cs @@ -12,8 +12,8 @@ public static async Task ExecuteQuery(string queryString, Inputs? inputs queryString = queryString.Replace("'", "\""); using Schema schema = new(); + schema.UseFluentValidation(); DocumentExecuter documentExecuter = new(); - ExecutionOptions executionOptions = new() { Schema = schema, diff --git a/src/Tests/Snippets/QueryExecution.cs b/src/Tests/Snippets/QueryExecution.cs index 543b71f7..bf5a8c53 100644 --- a/src/Tests/Snippets/QueryExecution.cs +++ b/src/Tests/Snippets/QueryExecution.cs @@ -20,6 +20,7 @@ void ExecuteQuery(Assembly assemblyContainingValidators) ValidatorTypeCache validatorTypeCache = new(); validatorTypeCache.AddValidatorsFromAssembly(assemblyContainingValidators); Schema schema = new(); + schema.UseFluentValidation(); DocumentExecuter executer = new(); #endregion diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index 1d93bfbd..2eda9b69 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -4,7 +4,7 @@ - +