Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Fusion Source Schema Package for Hot Chocolate #7203

Merged
merged 4 commits into from
Jul 9, 2024
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
2 changes: 2 additions & 0 deletions src/HotChocolate/Fusion/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions src/HotChocolate/Fusion/HotChocolate.Fusion.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Fusion.Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Fusion.Tests.Shared", "test\Shared\HotChocolate.Fusion.Tests.Shared.csproj", "{7C2BD6EE-D899-4269-AA5B-5CD3CA433953}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Fusion.SourceSchema", "src\SourceSchema\HotChocolate.Fusion.SourceSchema.csproj", "{5A0F102A-6BB7-484E-9898-564B27779233}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{7C2BD6EE-D899-4269-AA5B-5CD3CA433953}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C2BD6EE-D899-4269-AA5B-5CD3CA433953}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C2BD6EE-D899-4269-AA5B-5CD3CA433953}.Release|Any CPU.Build.0 = Release|Any CPU
{5A0F102A-6BB7-484E-9898-564B27779233}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A0F102A-6BB7-484E-9898-564B27779233}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A0F102A-6BB7-484E-9898-564B27779233}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A0F102A-6BB7-484E-9898-564B27779233}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BFF3B578-BAF7-4197-8DE5-091D53FB74B0} = {C831C01A-D213-4682-B19D-9946A72B0F77}
Expand All @@ -95,5 +101,6 @@ Global
{A5D42FCF-BF47-49EE-AAD9-B7B79E61D5B9} = {9973827C-F78F-412D-BD95-7D589C3FAA53}
{0564D85A-0CC5-4029-8660-7FF15E1034FD} = {9973827C-F78F-412D-BD95-7D589C3FAA53}
{7C2BD6EE-D899-4269-AA5B-5CD3CA433953} = {9973827C-F78F-412D-BD95-7D589C3FAA53}
{5A0F102A-6BB7-484E-9898-564B27779233} = {C831C01A-D213-4682-B19D-9946A72B0F77}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ internal static class DirectivesHelper
public const string RequireDirectiveName = "require";
public const string RemoveDirectiveName = "remove";
public const string RenameDirectiveName = "rename";
public const string InternalDirectiveName = "internal";
public const string CoordinateArg = "coordinate";
public const string NewNameArg = "newName";
public const string FieldArg = "field";


public static bool ContainsIsDirective(this IDirectivesProvider member)
=> member.Directives.ContainsName(IsDirectiveName);

public static bool ContainsInternalDirective(this IDirectivesProvider member)
=> member.Directives.ContainsName(InternalDirectiveName);

public static IsDirective GetIsDirective(this IDirectivesProvider member)
{
var directive = member.Directives[IsDirectiveName].First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ internal static class TagContextExtensions
public static TagContext GetTagContext(this CompositionContext context)
{
const string key = "HotChocolate.Fusion.Composition.TagContext";
if(context.ContextData.TryGetValue(key, out var value) &&

if(context.ContextData.TryGetValue(key, out var value) &&
value is TagContext tagContext)
{
return tagContext;
Expand All @@ -16,4 +16,4 @@ public static TagContext GetTagContext(this CompositionContext context)
context.ContextData[key] = tagContext;
return tagContext;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public static void TryApplySource<T>(
T target)
where T : ITypeSystemMemberDefinition, IFeatureProvider, IDirectivesProvider
{
if (target.ContainsInternalDirective())
{
return;
}

if (source.TryGetOriginalName(out var originalName))
{
target.Directives.Add(
Expand All @@ -45,6 +50,11 @@ public static void ApplySource<T>(
T target)
where T : ITypeSystemMemberDefinition, IFeatureProvider, IDirectivesProvider
{
if (target.ContainsInternalDirective())
{
return;
}

if (source.TryGetOriginalName(out var originalName))
{
target.Directives.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ private static void MergeRootFields(
continue;
}

if (field.ContainsInternalDirective())
{
continue;
}

if (targetRootType.Fields.TryGetField(field.Name, out var targetField))
{
context.MergeField(field, targetField, targetRootType.Name);
Expand All @@ -113,7 +118,7 @@ private static void MergeRootFields(
arguments,
null);

var selectionSet = new SelectionSetNode(new[] { selection, });
var selectionSet = new SelectionSetNode([selection]);

foreach (var arg in field.Arguments)
{
Expand Down
2 changes: 2 additions & 0 deletions src/HotChocolate/Fusion/src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PropertyGroup>
<NoWarn>$(NoWarn);CA1812</NoWarn>
<Nullable>enable</Nullable>
<WarningsAsErrors>$(WarningsAsErrors);nullable</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using HotChocolate.Fusion.SourceSchema.Types;

namespace HotChocolate.Types;

public static class FusionObjectFieldDescriptorExtensions
{
public static IObjectFieldDescriptor Internal(
this IObjectFieldDescriptor descriptor)
=> descriptor.Directive(InternalDirective.Instance);

public static IObjectFieldDescriptor Lookup(
this IObjectFieldDescriptor descriptor)
=> descriptor.Directive(LookupDirective.Instance);

public static IArgumentDescriptor Is(
this IArgumentDescriptor descriptor,
string field)
=> descriptor.Directive(new IsDirective(field));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>HotChocolate.Fusion.SourceSchema</AssemblyName>
<RootNamespace>HotChocolate.Fusion.SourceSchema</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Core\src\Types\HotChocolate.Types.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Fusion.SourceSchema.Types;


public sealed class InternalAttribute : ObjectFieldDescriptorAttribute
{
protected override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
=> descriptor.Directive(InternalDirective.Instance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HotChocolate.Types;

namespace HotChocolate.Fusion.SourceSchema.Types;

[DirectiveType("internal", DirectiveLocation.FieldDefinition)]
public sealed class InternalDirective
{
private InternalDirective() { }

public static InternalDirective Instance { get; } = new();
}
16 changes: 16 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/IsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Fusion.SourceSchema.Types;

public sealed class IsAttribute(string field) : ArgumentDescriptorAttribute
{
public string Field { get; set; } = field;

protected override void OnConfigure(
IDescriptorContext context,
IArgumentDescriptor descriptor,
ParameterInfo parameter)
=> descriptor.Directive(new IsDirective(Field));
}
22 changes: 22 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/IsDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HotChocolate.Types;

namespace HotChocolate.Fusion.SourceSchema.Types;

[DirectiveType("is", DirectiveLocation.ArgumentDefinition)]
public sealed class IsDirective
{
public IsDirective(string field)
{
if (string.IsNullOrEmpty(field))
{
throw new ArgumentException(
"Value cannot be null or empty.",
nameof(field));
}

Field = field;
}

[GraphQLName("field")]
public string Field { get; }
}
14 changes: 14 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/LookupAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Fusion.SourceSchema.Types;

public sealed class LookupAttribute : ObjectFieldDescriptorAttribute
{
protected override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member)
=> descriptor.Directive(LookupDirective.Instance);
}
11 changes: 11 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/LookupDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HotChocolate.Types;

namespace HotChocolate.Fusion.SourceSchema.Types;

[DirectiveType("lookup", DirectiveLocation.FieldDefinition)]
public sealed class LookupDirective
{
private LookupDirective() { }

public static LookupDirective Instance { get; } = new();
}
16 changes: 16 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/RequireAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Fusion.SourceSchema.Types;

public sealed class RequireAttribute(string field) : ArgumentDescriptorAttribute
{
public string Field { get; set; } = field;

protected override void OnConfigure(
IDescriptorContext context,
IArgumentDescriptor descriptor,
ParameterInfo parameter)
=> descriptor.Directive(new RequireDirective(Field));
}
22 changes: 22 additions & 0 deletions src/HotChocolate/Fusion/src/SourceSchema/Types/RequireDirective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HotChocolate.Types;

namespace HotChocolate.Fusion.SourceSchema.Types;

[DirectiveType("require", DirectiveLocation.ArgumentDefinition)]
public sealed class RequireDirective
{
public RequireDirective(string field)
{
if (string.IsNullOrEmpty(field))
{
throw new ArgumentException(
"Value cannot be null or empty.",
nameof(field));
}

Field = field;
}

[GraphQLName("field")]
public string Field { get; }
}
Loading
Loading