Skip to content

Commit 5fa94e0

Browse files
authored
Added a basic requirements planing. (#8243)
1 parent 7528098 commit 5fa94e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2056
-358
lines changed

.editorconfig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ rulers = 120
1212
# ReSharper properties
1313
resharper_align_multiline_statement_conditions = false
1414
resharper_braces_for_ifelse = not_required
15-
resharper_wrap_object_and_collection_initializer_style = chop_always
15+
resharper_keep_existing_initializer_arrangement = false
16+
resharper_trailing_comma_in_multiline_lists = false
17+
resharper_wrap_object_and_collection_initializer_style = wrap_if_long
1618

1719
# Microsoft .NET properties
18-
csharp_new_line_before_members_in_object_initializers = true
20+
csharp_new_line_before_members_in_object_initializers = false
1921

2022
[*.md]
2123
trim_trailing_whitespace = false

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Completion/CompositeSchemaBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private static CompositeSchemaContext CreateTypes(DocumentNode schema)
7878
break;
7979
}
8080
}
81-
8281
break;
8382
}
8483
}
@@ -342,11 +341,11 @@ private static SourceObjectFieldCollection BuildSourceObjectFieldCollection(
342341
argumentsBuilder.Add(new RequiredArgument(argument.Name.Value, argument.Type));
343342
}
344343

345-
var fieldsBuilder = ImmutableArray.CreateBuilder<FieldPath>();
344+
var fieldsBuilder = ImmutableArray.CreateBuilder<FieldPath?>();
346345

347346
foreach (var field in requireDirective.Map)
348347
{
349-
fieldsBuilder.Add(FieldPath.Parse(field));
348+
fieldsBuilder.Add(field is not null ? FieldPath.Parse(field) : null);
350349
}
351350

352351
var arguments = argumentsBuilder.ToImmutable();

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Directives/DirectiveTools.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public static IImmutableList<DirectiveNode> GetUserDirectives(
2525
continue;
2626
}
2727

28+
// TODO : Remove once we have a better way to handle built-in directives.
29+
if (FusionBuiltIns.IsBuiltInDirective(directiveNode.Name.Value))
30+
{
31+
continue;
32+
}
33+
2834
builder.Add(directiveNode);
2935
}
3036

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Directives/LookupDirective.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ internal sealed class LookupDirective(
1515
string schemaName,
1616
SelectionSetNode key,
1717
FieldDefinitionNode field,
18-
ImmutableArray<string> map)
18+
ImmutableArray<string> map,
19+
ImmutableArray<string> path)
1920
{
2021
public string SchemaName { get; } = schemaName;
2122

@@ -24,4 +25,6 @@ internal sealed class LookupDirective(
2425
public FieldDefinitionNode Field { get; } = field;
2526

2627
public ImmutableArray<string> Map { get; } = map;
28+
29+
public ImmutableArray<string> Path { get; } = path;
2730
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Directives/LookupDirectiveParser.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static LookupDirective Parse(DirectiveNode directive)
1515
SelectionSetNode? key = null;
1616
FieldDefinitionNode? field = null;
1717
ImmutableArray<string>? map = null;
18+
ImmutableArray<string>? path = null;
1819

1920
foreach (var argument in directive.Arguments)
2021
{
@@ -25,7 +26,14 @@ public static LookupDirective Parse(DirectiveNode directive)
2526
break;
2627

2728
case "key":
28-
key = Utf8GraphQLParser.Syntax.ParseSelectionSet(((StringValueNode)argument.Value).Value);
29+
var keySourceText = ((StringValueNode)argument.Value).Value.Trim();
30+
31+
if (!keySourceText.StartsWith('{'))
32+
{
33+
keySourceText = $"{{ {keySourceText} }}";
34+
}
35+
36+
key = Utf8GraphQLParser.Syntax.ParseSelectionSet(keySourceText);
2937
break;
3038

3139
case "field":
@@ -36,6 +44,13 @@ public static LookupDirective Parse(DirectiveNode directive)
3644
map = ParseMap(argument.Value);
3745
break;
3846

47+
case "path":
48+
if (argument.Value is StringValueNode pathValueNode)
49+
{
50+
path = pathValueNode.Value.Trim().Split('.').ToImmutableArray();
51+
}
52+
break;
53+
3954
default:
4055
throw new DirectiveParserException(
4156
$"The argument `{argument.Name.Value}` is not supported on @lookup.");
@@ -66,7 +81,7 @@ public static LookupDirective Parse(DirectiveNode directive)
6681
"The `map` argument is required on the @lookup directive.");
6782
}
6883

69-
return new LookupDirective(schemaName, key, field, map.Value);
84+
return new LookupDirective(schemaName, key, field, map.Value, path ?? ImmutableArray<string>.Empty);
7085
}
7186

7287
private static ImmutableArray<string> ParseMap(IValueNode value)

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Directives/RequireDirective.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace HotChocolate.Fusion.Types.Directives;
77
directive @fusion__requires(
88
schema: fusion__Schema!
99
field: fusion__FieldDefinition!
10-
map: [fusion__FieldSelectionMap!]!
10+
map: [fusion__FieldSelectionMap]!
1111
) repeatable on FIELD_DEFINITION
1212
*/
1313
internal class RequireDirective(
1414
string schemaName,
1515
FieldDefinitionNode field,
16-
ImmutableArray<string> map)
16+
ImmutableArray<string?> map)
1717
{
1818
/// <summary>
1919
/// Gets the name of the source schema that has requirements. for a field.
@@ -28,5 +28,5 @@ internal class RequireDirective(
2828
/// <summary>
2929
/// Gets the paths to the field that are required.
3030
/// </summary>
31-
public ImmutableArray<string> Map { get; } = map;
31+
public ImmutableArray<string?> Map { get; } = map;
3232
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Directives/RequiredDirectiveParser.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static RequireDirective Parse(DirectiveNode directiveNode)
1313
{
1414
string? schemaName = null;
1515
FieldDefinitionNode? field = null;
16-
ImmutableArray<string>? map = null;
16+
ImmutableArray<string?>? map = null;
1717

1818
foreach (var argument in directiveNode.Arguments)
1919
{
@@ -58,27 +58,36 @@ public static RequireDirective Parse(DirectiveNode directiveNode)
5858
return new RequireDirective(schemaName, field, map.Value);
5959
}
6060

61-
private static ImmutableArray<string> ParseMap(IValueNode value)
61+
private static ImmutableArray<string?> ParseMap(IValueNode value)
6262
{
63-
if (value is ListValueNode listValue)
63+
switch (value)
6464
{
65-
var fields = ImmutableArray.CreateBuilder<string>();
66-
67-
foreach (var item in listValue.Items)
68-
{
69-
fields.Add(((StringValueNode)item).Value);
70-
}
71-
72-
return fields.ToImmutable();
65+
case ListValueNode listValue:
66+
{
67+
var fields = ImmutableArray.CreateBuilder<string?>();
68+
69+
foreach (var item in listValue.Items)
70+
{
71+
if (item is StringValueNode stringValue)
72+
{
73+
fields.Add(stringValue.Value);
74+
}
75+
else
76+
{
77+
fields.Add(null);
78+
}
79+
}
80+
81+
return fields.ToImmutable();
82+
}
83+
84+
case StringValueNode stringValue:
85+
return [stringValue.Value];
86+
87+
default:
88+
throw new DirectiveParserException(
89+
"The value is expected to be a list of strings or a string.");
7390
}
74-
75-
if (value is StringValueNode stringValue)
76-
{
77-
return ImmutableArray<string>.Empty.Add(stringValue.Value);
78-
}
79-
80-
throw new DirectiveParserException(
81-
"The value is expected to be a list of strings or a string.");
8291
}
8392

8493
public static ImmutableArray<RequireDirective> Parse(

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/FusionBuiltIns.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ internal static class FusionBuiltIns
55
public const string FieldDefinition = "fusion__FieldDefinition";
66
public const string FieldSelectionMap = "fusion__FieldSelectionMap";
77
public const string FieldSelectionSet = "fusion__FieldSelectionSet";
8+
public const string FusionSelectionPath = "fusion__FieldSelectionPath";
89

910
public const string Type = "fusion__type";
1011
public const string Field = "fusion__field";
1112
public const string InputField = "fusion__inputField";
1213
public const string Requires = "fusion__requires";
1314
public const string Lookup = "fusion__lookup";
15+
public const string Implements = "fusion__implements";
16+
public const string UnionMember = "fusion__unionMember";
17+
public const string EnumValue = "fusion__enumValue";
1418

1519
public static bool IsBuiltInType(string typeName)
1620
{
1721
if(typeName == FieldDefinition ||
1822
typeName == FieldSelectionMap ||
19-
typeName == FieldSelectionSet)
23+
typeName == FieldSelectionSet ||
24+
typeName == FusionSelectionPath)
2025
{
2126
return true;
2227
}
@@ -30,7 +35,10 @@ public static bool IsBuiltInDirective(string directiveName)
3035
directiveName == Field ||
3136
directiveName == InputField ||
3237
directiveName == Requires ||
33-
directiveName == Lookup)
38+
directiveName == Lookup ||
39+
directiveName == Implements ||
40+
directiveName == UnionMember ||
41+
directiveName == EnumValue)
3442
{
3543
return true;
3644
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Metadata/FieldRequirements.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace HotChocolate.Fusion.Types;
66
public sealed class FieldRequirements(
77
string schemaName,
88
ImmutableArray<RequiredArgument> arguments,
9-
ImmutableArray<FieldPath> fields,
9+
ImmutableArray<FieldPath?> fields,
1010
SelectionSetNode selectionSet)
1111
{
1212
/// <summary>
@@ -22,7 +22,7 @@ public sealed class FieldRequirements(
2222
/// <summary>
2323
/// Gets the paths to the field that are required.
2424
/// </summary>
25-
public ImmutableArray<FieldPath> Fields { get; } = fields;
25+
public ImmutableArray<FieldPath?> Fields { get; } = fields;
2626

2727
/// <summary>
2828
/// Gets the selection set that represents the field requirements.

src/HotChocolate/Fusion-vnext/src/Fusion.Execution.Types/Utilities/FieldSelectionMapUtilities.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ public static FieldNode ToFieldNode(this FieldPath path)
2424
return current;
2525
}
2626

27+
#nullable disable
2728
public static SelectionSetNode ToSelectionSetNode(this ImmutableArray<FieldPath> paths)
2829
{
29-
var selections = new ISelectionNode[paths.Length];
30+
var selections = new List<ISelectionNode>();
3031

31-
for (var i = 0; i < paths.Length; i++)
32+
foreach (var path in paths)
3233
{
33-
selections[i] = paths[i].ToFieldNode();
34+
if (path is null)
35+
{
36+
continue;
37+
}
38+
39+
selections.Add(path.ToFieldNode());
3440
}
3541

3642
return new SelectionSetNode(selections);
3743
}
44+
#nullable restore
3845
}

0 commit comments

Comments
 (0)