Skip to content

Commit e437eeb

Browse files
authored
Added directive middleware integration tests (#330)
1 parent 8865cc9 commit e437eeb

File tree

8 files changed

+103
-10
lines changed

8 files changed

+103
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using ChilliCream.Testing;
2+
using HotChocolate.Execution;
3+
using HotChocolate.Types;
4+
using Xunit;
5+
6+
namespace HotChocolate.Integration.Directives
7+
{
8+
public class ConstantDirectiveType
9+
: DirectiveType
10+
{
11+
protected override void Configure(IDirectiveTypeDescriptor descriptor)
12+
{
13+
descriptor.Name("constant");
14+
descriptor.Argument("value").Type<StringType>();
15+
16+
descriptor.Location(DirectiveLocation.Object)
17+
.Location(DirectiveLocation.FieldDefinition)
18+
.Location(DirectiveLocation.Field);
19+
20+
descriptor.Middleware(next => context =>
21+
{
22+
context.Result = context.Directive.GetArgument<string>("value");
23+
return next(context);
24+
});
25+
}
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using ChilliCream.Testing;
2+
using HotChocolate.Execution;
3+
using Xunit;
4+
5+
namespace HotChocolate.Integration.Directives
6+
{
7+
public class DirectiveIntegrationTests
8+
{
9+
[Fact]
10+
public void UniqueDirectives_OnFieldLevel_OverwriteOnesOnObjectLevel()
11+
{
12+
// arrange
13+
ISchema schema = CreateSchema();
14+
15+
// act
16+
IExecutionResult result = schema.Execute("{ bar baz }");
17+
18+
// assert
19+
result.Snapshot();
20+
}
21+
22+
[Fact]
23+
public void UniqueDirectives_FieldSelection_OverwriteTypeSystemOnes()
24+
{
25+
// arrange
26+
ISchema schema = CreateSchema();
27+
28+
// act
29+
IExecutionResult result = schema.Execute(
30+
"{ bar baz @constant(value: \"baz\") }");
31+
32+
// assert
33+
result.Snapshot();
34+
}
35+
36+
private static ISchema CreateSchema()
37+
{
38+
return Schema.Create(
39+
"type Query @constant(value: \"foo\") " +
40+
"{ bar: String baz: String @constant(value: \"bar\") }",
41+
c => c.RegisterDirective<ConstantDirectiveType>());
42+
}
43+
}
44+
}

src/Core.Tests/Integration/StarWarsCodeFirst/Human.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Threading.Tasks;
3+
using HotChocolate.Types;
34

45
namespace HotChocolate.Integration.StarWarsCodeFirst
56
{

src/Core.Tests/Integration/StarWarsCodeFirst/StarWarsCodeFirstTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,15 @@ private static Schema CreateSchema(int executionDepth)
699699
return Schema.Create(c =>
700700
{
701701
c.Options.MaxExecutionDepth = executionDepth;
702+
702703
c.RegisterServiceProvider(serviceProvider.Object);
704+
703705
c.RegisterDataLoader<HumanDataLoader>();
706+
704707
c.RegisterQueryType<QueryType>();
705708
c.RegisterMutationType<MutationType>();
706709
c.RegisterSubscriptionType<SubscriptionType>();
710+
707711
c.RegisterType<HumanType>();
708712
c.RegisterType<DroidType>();
709713
c.RegisterType<EpisodeType>();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Data": {
3+
"bar": "foo",
4+
"baz": "baz"
5+
},
6+
"Errors": null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Data": {
3+
"bar": "foo",
4+
"baz": "bar"
5+
},
6+
"Errors": null
7+
}

src/Core/Execution/Utilities/DirectiveCollector.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,25 @@ private IReadOnlyCollection<IDirective> CollectDirectives(
8484
HashSet<string> processed = new HashSet<string>();
8585
List<IDirective> directives = new List<IDirective>();
8686

87-
CollectInheritedDirectives(processed, directives, field);
88-
CollectSelectionDirectives(processed, directives, fieldSelection);
87+
CollectTypeSystemDirectives(processed, directives, field);
88+
CollectQueryDirectives(processed, directives, fieldSelection);
8989

9090
return directives.AsReadOnly();
9191
}
9292

93-
private void CollectSelectionDirectives(
93+
private void CollectQueryDirectives(
9494
HashSet<string> processed,
9595
List<IDirective> directives,
9696
FieldNode fieldSelection)
9797
{
9898
foreach (IDirective directive in
9999
GetFieldSelectionDirectives(fieldSelection))
100100
{
101-
if (processed.Add(directive.Name))
101+
if (!processed.Add(directive.Name))
102102
{
103-
directives.Add(directive);
103+
directives.Remove(directive);
104104
}
105+
directives.Add(directive);
105106
}
106107
}
107108

@@ -122,17 +123,18 @@ private IEnumerable<IDirective> GetFieldSelectionDirectives(
122123
}
123124
}
124125

125-
private void CollectInheritedDirectives(
126+
private void CollectTypeSystemDirectives(
126127
HashSet<string> processed,
127128
List<IDirective> directives,
128129
ObjectField field)
129130
{
130131
foreach (IDirective directive in field.ExecutableDirectives)
131132
{
132-
if (processed.Add(directive.Name))
133+
if (!processed.Add(directive.Name))
133134
{
134-
directives.Add(directive);
135+
directives.Remove(directive);
135136
}
137+
directives.Add(directive);
136138
}
137139
}
138140
}

src/Types/Types/ObjectField.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ private void AddExectableDirectives(
127127
foreach (IDirective directive in
128128
directives.Where(t => t.IsExecutable))
129129
{
130-
if (processed.Add(directive.Name))
130+
if (processed.Contains(directive.Name))
131131
{
132-
_executableDirectives.Add(directive);
132+
_executableDirectives.Remove(directive);
133133
}
134+
_executableDirectives.Add(directive);
134135
}
135136
}
136137

0 commit comments

Comments
 (0)