Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
<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>
<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>
<!-- endSnippet -->

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

var executionResult = await executer.ExecuteAsync(options);
```
<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>
<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>
<!-- endSnippet -->


Expand All @@ -156,7 +157,7 @@ public class MyUserContext :
public string MyProperty { get; }
}
```
<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>
<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>
<!-- endSnippet -->

The `ExecutionOptions.UserContext` can then be set as follows:
Expand All @@ -176,7 +177,7 @@ ExecutionOptions options = new()
};
options.UseFluentValidation(validatorTypeCache);
```
<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>
<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>
<!-- endSnippet -->


Expand All @@ -203,7 +204,7 @@ ExecutionOptions options = new()
};
options.UseFluentValidation(validatorTypeCache);
```
<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>
<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>
<!-- endSnippet -->


Expand All @@ -222,7 +223,7 @@ ExecutionOptions options = new()
};
options.UseFluentValidation(validatorTypeCache);
```
<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>
<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>
<!-- endSnippet -->

Then the `UseFluentValidation` method will instantiate it to a new `Dictionary<string, object>`.
Expand Down Expand Up @@ -354,10 +355,10 @@ public class QueryTests
};
ResolveFieldContext fieldContext = new()
{
Arguments = new Dictionary<string, object>
Arguments = new Dictionary<string, ArgumentValue>
{
{
"input", input
"input", new ArgumentValue(input, ArgumentSource.Variable)
}
},
UserContext = userContext
Expand All @@ -376,12 +377,14 @@ public class QueryTests
FluentValidationExtensions.AddCacheToContext(
userContext,
ValidatorCacheBuilder.Instance);

var value = new Dictionary<string, object>();
ResolveFieldContext fieldContext = new()
{
Arguments = new Dictionary<string, object>
Arguments = new Dictionary<string, ArgumentValue>
{
{
"input", new Dictionary<string, object>()
"input", new ArgumentValue(value, ArgumentSource.Variable)
}
},
UserContext = userContext
Expand All @@ -392,7 +395,7 @@ public class QueryTests
}
}
```
<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>
<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>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>5.2.0</Version>
<Version>6.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>GraphQL, Validation, FluentValidation</PackageTags>
<Description>Add FluentValidation (https://fluentvalidation.net/) support to GraphQL.net (https://github.com/graphql-dotnet/graphql-dotnet)</Description>
Expand Down
14 changes: 12 additions & 2 deletions src/GraphQL.FluentValidation/FluentValidationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentValidation;
using GraphQL.FluentValidation;
using GraphQL.Instrumentation;
using GraphQL.Types;

namespace GraphQL
{
Expand All @@ -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;
}

/// <summary>
/// Adds a FieldMiddleware to the GraphQL pipeline that converts a <see cref="ValidationException"/> to <see cref="ExecutionError"/>s./>
/// </summary>
public static void UseFluentValidation(this Schema schema)
{
Guard.AgainstNull(schema, nameof(schema));

ValidationMiddleware validationMiddleware = new();
schema.FieldMiddleware.Use(validationMiddleware);
}
}
}
3 changes: 2 additions & 1 deletion src/GraphQL.FluentValidation/GraphQL.FluentValidation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.5.3" />
<PackageReference Include="GraphQL" Version="3.3.2" />
<PackageReference Include="GraphQL" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="ProjectDefaults" Version="1.0.54" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" />
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.FluentValidation/ValidatorTypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Reflection;
using FluentValidation;
using GraphQL.Utilities;
using Microsoft.Extensions.DependencyInjection;

namespace GraphQL.FluentValidation
{
Expand Down
12 changes: 8 additions & 4 deletions src/SampleWeb.Tests/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,10 +28,10 @@ public Task RunInputQuery()
};
ResolveFieldContext fieldContext = new()
{
Arguments = new Dictionary<string, object>
Arguments = new Dictionary<string, ArgumentValue>
{
{
"input", input
"input", new ArgumentValue(input, ArgumentSource.Variable)
}
},
UserContext = userContext
Expand All @@ -48,12 +50,14 @@ public Task RunInvalidInputQuery()
FluentValidationExtensions.AddCacheToContext(
userContext,
ValidatorCacheBuilder.Instance);

var value = new Dictionary<string, object>();
ResolveFieldContext fieldContext = new()
{
Arguments = new Dictionary<string, object>
Arguments = new Dictionary<string, ArgumentValue>
{
{
"input", new Dictionary<string, object>()
"input", new ArgumentValue(value, ArgumentSource.Variable)
}
},
UserContext = userContext
Expand Down
2 changes: 1 addition & 1 deletion src/SampleWeb.Tests/SampleWeb.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net5</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphQL" Version="3.3.2" />
<PackageReference Include="GraphQL" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.4" />
<PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="Verify.Xunit" Version="11.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion src/SampleWeb/SampleWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Folder Include="wwwroot\" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="9.5.3" />
<PackageReference Include="graphiql" Version="2.0.0" />
<PackageReference Include="GraphQL.NewtonsoftJson" Version="3.3.2" />
<PackageReference Include="GraphQL.NewtonsoftJson" Version="4.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4" />
<ProjectReference Include="..\GraphQL.FluentValidation\GraphQL.FluentValidation.csproj" />
<PackageReference Include="ProjectDefaults" Version="1.0.54" PrivateAssets="All" />
Expand Down
2 changes: 2 additions & 0 deletions src/SampleWeb/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 0 additions & 4 deletions src/SampleWeb/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,9 +14,6 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
GraphTypeTypeRegistry.Register<MyInput, MyInputGraph>();
GraphTypeTypeRegistry.Register<Result, ResultGraph>();

foreach (var type in GetGraphQLTypes())
{
services.AddSingleton(type);
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/QueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static async Task<string> ExecuteQuery(string queryString, Inputs? inputs

queryString = queryString.Replace("'", "\"");
using Schema schema = new();
schema.UseFluentValidation();
DocumentExecuter documentExecuter = new();

ExecutionOptions executionOptions = new()
{
Schema = schema,
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Snippets/QueryExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void ExecuteQuery(Assembly assemblyContainingValidators)
ValidatorTypeCache validatorTypeCache = new();
validatorTypeCache.AddValidatorsFromAssembly(assemblyContainingValidators);
Schema schema = new();
schema.UseFluentValidation();
DocumentExecuter executer = new();

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="9.5.3" />
<PackageReference Include="GraphQL.NewtonsoftJson" Version="3.3.2" />
<PackageReference Include="GraphQL.NewtonsoftJson" Version="4.0.2" />
<PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
Expand Down