Skip to content

Commit

Permalink
Simplified Directive API (#5630)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jan 9, 2023
1 parent 0456ec5 commit 2ebdb17
Show file tree
Hide file tree
Showing 513 changed files with 9,838 additions and 5,501 deletions.
10 changes: 5 additions & 5 deletions .build/Build.PublicApiAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ partial class Build
var projectFiles = ProjectModelTasks.ParseSolution(AllSolutionFile)
.AllProjects
.Where(t => t.GetProperty<string>("AddPublicApiAnalyzers") != "false")
.Where(t => !Path.GetDirectoryName(t.Path)
.Where(t => !Path.GetDirectoryName(t.Path)!
.EndsWith("tests", StringComparison.OrdinalIgnoreCase))
.Where(t => !Path.GetDirectoryName(t.Path)
.Where(t => !Path.GetDirectoryName(t.Path)!
.EndsWith("test", StringComparison.OrdinalIgnoreCase))
.Select(t => Path.GetDirectoryName(t.Path)!)
.ToArray();
Expand All @@ -61,9 +61,9 @@ partial class Build
var projectFiles = ProjectModelTasks.ParseSolution(AllSolutionFile)
.AllProjects
.Where(t => t.GetProperty<string>("AddPublicApiAnalyzers") != "false")
.Where(t => !Path.GetDirectoryName(t.Path)
.Where(t => !Path.GetDirectoryName(t.Path)!
.EndsWith("tests", StringComparison.OrdinalIgnoreCase))
.Where(t => !Path.GetDirectoryName(t.Path)
.Where(t => !Path.GetDirectoryName(t.Path)!
.EndsWith("test", StringComparison.OrdinalIgnoreCase))
.Select(t => Path.GetDirectoryName(t.Path)!)
.ToArray();
Expand All @@ -86,7 +86,7 @@ partial class Build
return;
}

AbsolutePath shippedPath = SourceDirectory / "**" / _shippedApiFile;
var shippedPath = SourceDirectory / "**" / _shippedApiFile;

Git($@" --no-pager diff --minimal -U0 --word-diff ""{from}"" ""{to}"" -- ""{shippedPath}""", RootDirectory);
});
Expand Down
24 changes: 23 additions & 1 deletion .build/Build.Tests.2.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Drawing;
using System.Linq;
using Colorful;
using Nuke.Common;
using Nuke.Common.IO;
using Nuke.Common.Tooling;
Expand Down Expand Up @@ -98,11 +101,30 @@ void RunClientTests(AbsolutePath solutionFile)

void RunTests(AbsolutePath solutionFile)
{
var solutionDirectory = solutionFile.Parent!;
var testDirectory = solutionDirectory / "test";

DotNetBuild(c => c
.SetProjectFile(solutionFile)
.SetConfiguration(Debug));

var testProjects = ParseSolution(solutionFile).GetProjects("*.Tests");
// we only select test projects that are located in the solutions test directory.
// this will ensure that on build we do not execute referenced tests from other solutions.
var testProjects = ParseSolution(solutionFile)
.GetProjects("*.Tests")
.Where(t => t.Path.ToString().StartsWith(testDirectory))
.ToArray();


Console.WriteLine("╬============================================");
Console.WriteLine("║ Prepared Tests:");
Console.WriteLine($"║ {RootDirectory.GetRelativePathTo(solutionDirectory)}:");

foreach (var testProject in testProjects)
{
Console.WriteLine($"║ - {RootDirectory.GetRelativePathTo( testProject.Path.Parent!)}:");
}
Console.WriteLine("╬================================");

try
{
Expand Down
6 changes: 4 additions & 2 deletions src/CookieCrumble/src/CookieCrumble/FileResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ namespace CookieCrumble;

public static class FileResource
{
public static string? Open(string name)
public static string Open(string name)
{
var path = Path.Combine("__resources__", name);
return File.Exists(path) ? File.ReadAllText(path) : null;
return File.Exists(path)
? File.ReadAllText(path)
: throw new InvalidOperationException($"The test resource {path} is missing!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private static DirectiveDefinitionNode SerializeDirectiveTypeDefinition(
.ToList();

var locations = directiveType.Locations
.AsEnumerable()
.Select(l => new NameNode(l.MapDirectiveLocation().ToString()))
.ToList();

Expand All @@ -32,4 +33,4 @@ private static DirectiveDefinitionNode SerializeDirectiveTypeDefinition(
locations
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static NamedTypeNode SerializeNamedType(
}

private static IReadOnlyList<DirectiveNode> SerializeDirectives(
IReadOnlyCollection<IDirective> directives,
IReadOnlyCollection<Directive> directives,
Context context)
{
if (directives.Count == 0)
Expand All @@ -139,9 +139,9 @@ private static IReadOnlyList<DirectiveNode> SerializeDirectives(

foreach (var directive in directives)
{
if (context.DirectiveNames.Contains(directive.Name))
if (context.DirectiveNames.Contains(directive.Type.Name))
{
(directiveNodes ??= new()).Add(directive.ToNode(true));
(directiveNodes ??= new()).Add(directive.AsSyntaxNode(true));
}
}

Expand Down Expand Up @@ -177,6 +177,7 @@ private static bool IsBuiltInType(INamedType type) =>

private sealed class Context
{
// ReSharper disable once CollectionNeverQueried.Local
public HashSet<string> TypeNames { get; } = new();
public HashSet<string> DirectiveNames { get; } = new(_builtInDirectives);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void AddToUnionIfHasTypeLevelKeyDirective(
ObjectTypeDefinition objectTypeDefinition)
{
if (objectTypeDefinition.Directives.Any(
d => d.Reference is NameDirectiveReference { Name: WellKnownTypeNames.Key }) ||
d => d.Value is DirectiveNode { Name.Value: WellKnownTypeNames.Key }) ||
objectTypeDefinition.Fields.Any(f => f.ContextData.ContainsKey(WellKnownTypeNames.Key)))
{
_entityTypes.Add(objectType);
Expand Down Expand Up @@ -245,10 +245,10 @@ private void AggregatePropertyLevelKeyDirectives(
{
discoveryContext.Dependencies.Add(
new TypeDependency(
directiveDefinition.TypeReference,
TypeDependencyKind.Completed));
directiveDefinition.Type,
TypeDependencyFulfilled.Completed));

discoveryContext.RegisterDependency(directiveDefinition.Reference);
discoveryContext.Dependencies.Add(new(directiveDefinition.Type));
}

// since this type has now a key directive we also need to add this type to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public static void TryAddExternalSetter(ObjectType type, ObjectTypeDefinition ty

foreach (var field in type.Fields)
{
if (field.Directives.Contains(WellKnownTypeNames.External) &&
field.Member is PropertyInfo property &&
property.SetMethod is not null)
if (field.Directives.ContainsDirective(WellKnownTypeNames.External) &&
field.Member is PropertyInfo { SetMethod: { } } property)
{
var expression = CreateTrySetValue(type.RuntimeType, property, field.Name);
(block ??= new()).Add(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using HotChocolate.Types;
using HotChocolate.Utilities;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.ApolloFederation.Directives;

Expand All @@ -24,9 +23,8 @@ public void AddExternalDirective_EnsureAvailableInSchema()
Assert.NotNull(directive);
Assert.IsType<ExternalDirectiveType>(directive);
Assert.Equal(WellKnownTypeNames.External, directive!.Name);
Assert.Empty(directive!.Arguments);
Assert.Collection(directive!.Locations,
t => Assert.Equal(DirectiveLocation.FieldDefinition, t));
Assert.Empty(directive.Arguments);
Assert.Equal(DirectiveLocation.FieldDefinition, directive.Locations);
}

[Fact]
Expand All @@ -52,11 +50,7 @@ public void AnnotateExternalToTypeFieldCodeFirst()
// assert
Assert.Collection(
query.Fields["field"].Directives,
item => Assert.Equal(
WellKnownTypeNames.External,
item.Name
)
);
item => Assert.Equal(WellKnownTypeNames.External, item.Type.Name));
schema.ToString().MatchSnapshot();
}

Expand All @@ -77,7 +71,7 @@ type Query {
"
)
.AddDirectiveType<ExternalDirectiveType>()
.Use(next => context => default)
.Use(_ => _ => default)
.Create();

// act
Expand All @@ -86,11 +80,7 @@ type Query {
// assert
Assert.Collection(
queryInterface.Fields["field"].Directives,
item => Assert.Equal(
WellKnownTypeNames.External,
item.Name
)
);
item => Assert.Equal(WellKnownTypeNames.External, item.Type.Name));
schema.ToString().MatchSnapshot();
}

Expand All @@ -111,11 +101,7 @@ public void AnnotateExternalToTypeFieldPureCodeFirst()
// assert
Assert.Collection(
query.Fields["idCode"].Directives,
item => Assert.Equal(
WellKnownTypeNames.External,
item.Name
)
);
item => Assert.Equal(WellKnownTypeNames.External, item.Type.Name));
schema.ToString().MatchSnapshot();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using HotChocolate.Types;
using HotChocolate.Utilities;
using Snapshooter.Xunit;
using Xunit;

namespace HotChocolate.ApolloFederation.Directives;

Expand All @@ -29,10 +28,8 @@ public void AddKeyDirective_EnsureAvailableInSchema()
Assert.Equal(WellKnownTypeNames.Key, directive!.Name);
Assert.Single(directive.Arguments);
AssertDirectiveHasFieldsArgument(directive);
Assert.Collection(directive.Locations,
t => Assert.Equal(DirectiveLocation.Object, t),
t => Assert.Equal(DirectiveLocation.Interface, t));

Assert.True(directive.Locations.HasFlag(DirectiveLocation.Object));
Assert.True(directive.Locations.HasFlag(DirectiveLocation.Interface));
}

[Fact]
Expand Down Expand Up @@ -68,9 +65,9 @@ public void AnnotateKeyToObjectTypeCodeFirst()
testType.Directives,
item =>
{
Assert.Equal(WellKnownTypeNames.Key, item.Name);
Assert.Equal("fields", item.ToNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.ToNode().Arguments[0].Value.ToString());
Assert.Equal(WellKnownTypeNames.Key, item.Type.Name);
Assert.Equal("fields", item.AsSyntaxNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.AsSyntaxNode().Arguments[0].Value.ToString());
});

schema.ToString().MatchSnapshot();
Expand Down Expand Up @@ -99,7 +96,7 @@ interface IQuery {
}")
.AddDirectiveType<KeyDirectiveType>()
.AddType<FieldSetType>()
.Use(next => context => default)
.Use(_ => _ => default)
.Create();

// act
Expand All @@ -109,12 +106,9 @@ interface IQuery {
Assert.Collection(testType.Directives,
item =>
{
Assert.Equal(
WellKnownTypeNames.Key,
item.Name
);
Assert.Equal("fields", item.ToNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.ToNode().Arguments[0].Value.ToString());
Assert.Equal(WellKnownTypeNames.Key, item.Type.Name);
Assert.Equal("fields", item.AsSyntaxNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.AsSyntaxNode().Arguments[0].Value.ToString());
});

schema.ToString().MatchSnapshot();
Expand All @@ -138,12 +132,9 @@ public void AnnotateKeyToObjectTypePureCodeFirst()
Assert.Collection(testType.Directives,
item =>
{
Assert.Equal(
WellKnownTypeNames.Key,
item.Name
);
Assert.Equal("fields", item.ToNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.ToNode().Arguments[0].Value.ToString());
Assert.Equal(WellKnownTypeNames.Key, item.Type.Name);
Assert.Equal("fields", item.AsSyntaxNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.AsSyntaxNode().Arguments[0].Value.ToString());
});

schema.ToString().MatchSnapshot();
Expand All @@ -167,12 +158,9 @@ public void AnnotateKeyToClassAttributePureCodeFirst()
Assert.Collection(testType.Directives,
item =>
{
Assert.Equal(
WellKnownTypeNames.Key,
item.Name
);
Assert.Equal("fields", item.ToNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.ToNode().Arguments[0].Value.ToString());
Assert.Equal(WellKnownTypeNames.Key, item.Type.Name);
Assert.Equal("fields", item.AsSyntaxNode().Arguments[0].Name.ToString());
Assert.Equal("\"id\"", item.AsSyntaxNode().Arguments[0].Value.ToString());
});

schema.ToString().MatchSnapshot();
Expand All @@ -196,12 +184,9 @@ public void AnnotateKeyToClassAttributesPureCodeFirst()
Assert.Collection(testType.Directives,
item =>
{
Assert.Equal(
WellKnownTypeNames.Key,
item.Name
);
Assert.Equal("fields", item.ToNode().Arguments[0].Name.ToString());
Assert.Equal("\"id name\"", item.ToNode().Arguments[0].Value.ToString());
Assert.Equal(WellKnownTypeNames.Key, item.Type.Name);
Assert.Equal("fields", item.AsSyntaxNode().Arguments[0].Name.ToString());
Assert.Equal("\"id name\"", item.AsSyntaxNode().Arguments[0].Value.ToString());
});

schema.ToString().MatchSnapshot();
Expand Down
Loading

0 comments on commit 2ebdb17

Please sign in to comment.