-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental @OneOf directive support for input types
- Loading branch information
Showing
25 changed files
with
546 additions
and
58 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/GraphQL.Extensions.Experimental/GraphQL.Extensions.Experimental.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>true</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\GraphQL.Language\GraphQL.Language.csproj" /> | ||
<ProjectReference Include="..\GraphQL\GraphQL.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
66 changes: 66 additions & 0 deletions
66
src/GraphQL.Extensions.Experimental/OneOf/OneOfDirective.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Tanka.GraphQL.Language; | ||
using Tanka.GraphQL.Language.Nodes; | ||
using Tanka.GraphQL.Language.Nodes.TypeSystem; | ||
using Tanka.GraphQL.TypeSystem; | ||
using Tanka.GraphQL.Validation; | ||
|
||
namespace Tanka.GraphQL.Extensions.Experimental.OneOf; | ||
|
||
public class OneOfDirective | ||
{ | ||
private static readonly List<NodeKind> AllowedKinds = [NodeKind.ObjectValue, NodeKind.Variable]; | ||
|
||
public static DirectiveDefinition Directive => | ||
$"directive @oneOf on {TypeSystemDirectiveLocations.INPUT_OBJECT}"; | ||
|
||
public static CombineRule OneOfValidationRule() | ||
{ | ||
return (context, rule) => | ||
{ | ||
rule.EnterArgument += argument => | ||
{ | ||
InputValueDefinition? argumentDefinition = context.Tracker.ArgumentDefinition; | ||
|
||
if (argumentDefinition is null) | ||
return; | ||
|
||
if (!AllowedKinds.Contains(argument.Value.Kind)) | ||
return; | ||
|
||
if (context.Schema.GetNamedType(argumentDefinition.Type.Unwrap().Name) is not InputObjectDefinition | ||
inputObject) | ||
return; | ||
|
||
if (!inputObject.HasDirective(Directive.Name)) | ||
return; | ||
|
||
if (argument.Value.Kind == NodeKind.Variable) | ||
{ | ||
var variable = (Variable)argument.Value; | ||
|
||
if (context.VariableValues?.TryGetValue(variable.Name, out object? variableValue) != true) return; | ||
|
||
if (variableValue is not null) | ||
{ | ||
var coercedValue = Values.CoerceValue( | ||
context.Schema, | ||
variableValue, | ||
argumentDefinition.Type) as IReadOnlyDictionary<string, object?>; | ||
|
||
if (coercedValue?.Count(kv => kv.Value is not null) != 1) | ||
context.Error("ONEOF001", | ||
$"Invalid value for '@oneOf' input '{inputObject.Name}'. @oneOf input objects can only have one field value set."); | ||
} | ||
} | ||
else | ||
{ | ||
var objectValue = (ObjectValue)argument.Value; | ||
|
||
if (objectValue.Count != 1) | ||
context.Error("ONEOF001", | ||
$"Invalid value for '@oneOf' input '{inputObject.Name}'. @oneOf input objects can only have one field value set."); | ||
} | ||
}; | ||
}; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/GraphQL.Extensions.Experimental/OneOf/SchemaBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Tanka.GraphQL.TypeSystem; | ||
|
||
namespace Tanka.GraphQL.Extensions.Experimental.OneOf; | ||
|
||
public static class SchemaBuilderExtensions | ||
{ | ||
public static SchemaBuilder AddOneOf(this SchemaBuilder builder) | ||
{ | ||
builder.Add(OneOfDirective.Directive); | ||
return builder; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/GraphQL.Extensions.Experimental/OneOf/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using Tanka.GraphQL.Validation; | ||
|
||
namespace Tanka.GraphQL.Extensions.Experimental.OneOf; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddOneOf(this IServiceCollection services) | ||
{ | ||
services.PostConfigure<AsyncValidatorOptions>( | ||
options => options.Rules.Add(OneOfDirective.OneOfValidationRule()) | ||
); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Tanka.GraphQL.Validation; | ||
|
||
namespace Tanka.GraphQL; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDefaultTankaGraphQLServices(this IServiceCollection services) | ||
{ | ||
return services.AddDefaultValidator(); | ||
} | ||
|
||
public static IServiceCollection AddDefaultValidator(this IServiceCollection services) | ||
{ | ||
services.AddOptions(); | ||
services.AddOptions<AsyncValidatorOptions>(); | ||
services.TryAddSingleton<IAsyncValidator>(p => | ||
new AsyncValidator(p.GetRequiredService<IOptions<AsyncValidatorOptions>>())); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddDefaultValidatorRule(this IServiceCollection services, CombineRule rule) | ||
{ | ||
services.PostConfigure<AsyncValidatorOptions>(options => options.Rules.Add(rule)); | ||
return services; | ||
} | ||
} |
Oops, something went wrong.