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

Fixes and issue with custom directives and directive variables. #6168

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixes and issue with custom directives and directive variables
  • Loading branch information
michaelstaib committed May 19, 2023
commit b4cf1229c23c4e498475b2183697fc8693943d3f
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@ protected override FieldNode RewriteField(
FieldNode node,
Context context)
{
var directives = node.Directives;

if (directives.Count > 0)
{
List<DirectiveNode>? temp = null;

foreach (var directive in directives)
{
if (BuiltInTypes.IsBuiltInType(directive.Name.Value))
{
temp ??= new List<DirectiveNode>(directives);
temp.Remove(directive);
}
}

if (temp is not null)
{
directives = temp;
}
}

if (context.TypeContext is IComplexOutputType type &&
type.Fields.TryGetField(node.Name.Value, out var field))
{
Expand All @@ -133,7 +112,7 @@ protected override FieldNode RewriteField(
}

var required = RewriteNodeOrDefault(node.Required, cloned);
directives = RewriteList(directives, cloned);
var directives = RewriteList(node.Directives, cloned);
var arguments = RewriteList(node.Arguments, cloned);
var selectionSet = node.SelectionSet;

Expand Down Expand Up @@ -164,11 +143,6 @@ protected override FieldNode RewriteField(
return OnRewriteField(node, cloned);
}

if (!ReferenceEquals(directives, node.Directives))
{
return node.WithDirectives(directives);
}

return node;
}

Expand Down Expand Up @@ -224,6 +198,20 @@ protected override SelectionSetNode RewriteSelectionSet(
return current;
}

protected override DirectiveNode? RewriteDirective(DirectiveNode node, Context context)
{
if (node.Arguments.Count > 0)
{
var arguments = RewriteList(node.Arguments, context);
if (!ReferenceEquals(arguments, node.Arguments))
{
return node.WithArguments(arguments);
}
}

return base.RewriteDirective(node, context);
}

private SelectionSetNode OnRewriteSelectionSet(
SelectionSetNode node,
Context context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,14 @@ public IDictionary<string, string> AddQuery(

foreach (var variable in operation.VariableDefinitions)
{
if (!_variables.ContainsKey(variable.Variable.Name.Value))
{
_variables.Add(variable.Variable.Name.Value, variable);
}
_variables.TryAdd(variable.Variable.Name.Value, variable);
}

_fields.AddRange(operation.SelectionSet.Selections.OfType<FieldNode>());

foreach (var fragment in rewritten.Definitions
.OfType<FragmentDefinitionNode>())
foreach (var fragment in rewritten.Definitions.OfType<FragmentDefinitionNode>())
{
if (!_fragments.ContainsKey(fragment.Name.Value))
{
_fragments.Add(fragment.Name.Value, fragment);
}
_fragments.TryAdd(fragment.Name.Value, fragment);
}

return _aliases;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,55 @@ public async Task AutoMerge_AddLocal_Field_Execute()
// act
var result = await executor.ExecuteAsync(
@"{
me {
id
name
reviews {
body
product {
upc
}
me {
id
name
reviews {
body
product {
upc
}
}
local
}");
}
local
}");

// assert
result.ToJson().MatchSnapshot();
}

[Fact]
public async Task Skip_Field_With_Variable()
{
// arrange
var httpClientFactory = CreateDefaultRemoteSchemas();

var executor =
await new ServiceCollection()
.AddSingleton(httpClientFactory)
.AddGraphQL()
.AddQueryType(d => d.Name("Query").Field("local").Resolve("I am local."))
.AddRemoteSchema(_accounts)
.AddRemoteSchema(_inventory)
.AddRemoteSchema(_products)
.AddRemoteSchema(_reviews)
.BuildRequestExecutorAsync();

// act
var result = await executor.ExecuteAsync(
@"query ($if: Boolean! = true) {
me {
id
name @skip(if: $if)
reviews @skip(if: $if) {
body
product {
upc
}
}
}
local
}");

// assert
result.ToJson().MatchSnapshot();
Expand Down