Skip to content
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
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ The `GraphQlController` can be tested using the [ASP.NET Integration tests](http
[UsesVerify]
public class GraphQlControllerTests
{
static HttpClient client = null!;
static HttpClient client;
static ClientQueryExecutor clientQueryExecutor;
static WebSocketClient webSocket = null!;
static WebSocketClient webSocket;

static GraphQlControllerTests()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;NU5104;CS1573</NoWarn>
<Version>19.2.0</Version>
<Version>19.2.1</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>EntityFrameworkCore, EntityFramework, GraphQL</PackageTags>
<ImplicitUsings>true</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ public static IQueryable<TItem> ApplyGraphQlArguments<TItem>(
var predicate = ExpressionBuilder<TItem>.BuildPredicate(keyName, Comparison.In, values);
queryable = queryable.Where(predicate);
}

if (ArgumentReader.TryReadId(GetArguments, out var value))
{
var keyName = GetKeyName(keyNames);
var predicate = ExpressionBuilder<TItem>.BuildPredicate(keyName, Comparison.Equal, new[] {value});
queryable = queryable.Where(predicate);
}
}

if (ArgumentReader.TryReadWhere(GetArguments, out var wheres))
Expand Down
53 changes: 25 additions & 28 deletions src/GraphQL.EntityFramework/Where/ArgumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,45 @@ public static bool TryReadWhere(Func<Type, string, object?> getArgument, out IEn

public static IEnumerable<OrderBy> ReadOrderBy(Func<Type, string, object?> getArgument) => getArgument.ReadList<OrderBy>("orderBy");

public static bool TryReadIds(Func<Type, string, object?> getArgument, [NotNullWhen(returnValue: true)] out string[]? expression)
public static bool TryReadIds(Func<Type, string, object?> getArgument, [NotNullWhen(returnValue: true)] out string[]? result)
{
var argument = getArgument(typeof(object), "ids");
if (argument is null)
string ArgumentToExpression(object argument)
{
expression = null;
return false;
return argument switch
{
long l => l.ToString(CultureInfo.InvariantCulture),
int i => i.ToString(CultureInfo.InvariantCulture),
string s => s,
_ => throw new($"TryReadId got an 'id' argument of type '{argument.GetType().FullName}' which is not supported.")
};
}

if (argument is IEnumerable<object> objCollection)
var idsArgument = getArgument(typeof(object), "ids");
var idArgument = getArgument(typeof(object), "id");
if (idsArgument is null && idArgument is null)
{
expression = objCollection.Select(o => o.ToString()).ToArray()!;
return true;
result = null;
return false;
}

throw new($"TryReadIds got an 'ids' argument of type '{argument.GetType().FullName}' which is not supported.");
}
var expressions = new List<string>();

public static bool TryReadId(Func<Type, string, object?> getArgument, [NotNullWhen(returnValue: true)] out string? expression)
{
var argument = getArgument(typeof(object), "id");
if (argument is null)
if (idArgument is not null)
{
expression = null;
return false;
expressions.Add( ArgumentToExpression(idArgument));
}

switch (argument)
if (idsArgument is not null)
{
case long l:
expression = l.ToString(CultureInfo.InvariantCulture);
break;
case int i:
expression = i.ToString(CultureInfo.InvariantCulture);
break;
case string s:
expression = s;
break;
default:
throw new($"TryReadId got an 'id' argument of type '{argument.GetType().FullName}' which is not supported.");
if (idsArgument is not IEnumerable<object> objCollection)
{
throw new($"TryReadIds got an 'ids' argument of type '{idsArgument.GetType().FullName}' which is not supported.");
}

expressions.AddRange(objCollection.Select(ArgumentToExpression));
}

result = expressions.ToArray();
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/SampleWeb.Tests/GraphQlControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
[UsesVerify]
public class GraphQlControllerTests
{
static HttpClient client = null!;
static HttpClient client;
static ClientQueryExecutor clientQueryExecutor;
static WebSocketClient webSocket = null!;
static WebSocketClient webSocket;

static GraphQlControllerTests()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
target:
{
"data": {
"parentEntities": [
{
"property": "Value1",
"children": [
{
"property": "Child1"
}
]
}
]
}
},
sql: [
{
HasTransaction: false,
Text:
SELECT [p].[Id], [p].[Property], [c].[Id], [c].[Nullable], [c].[ParentId], [c].[Property]
FROM [ParentEntities] AS [p]
LEFT JOIN [ChildEntities] AS [c] ON [p].[Id] = [c].[ParentId]
ORDER BY [p].[Id]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
target:
{
"data": {
"parentEntities": [
{
"property": "Value1",
"children": [
{
"property": "Child2"
},
{
"property": "Child1"
}
]
}
]
}
},
sql: [
{
HasTransaction: false,
Text:
SELECT [p].[Id], [p].[Property], [c].[Id], [c].[Nullable], [c].[ParentId], [c].[Property]
FROM [ParentEntities] AS [p]
LEFT JOIN [ChildEntities] AS [c] ON [p].[Id] = [c].[ParentId]
WHERE [p].[Id] = 'Guid_1'
ORDER BY [p].[Id]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
target:
{
"data": {
"parentEntities": [
{
"property": "Value1",
"children": [
{
"property": "Child1"
}
]
}
]
}
},
sql: [
{
HasTransaction: false,
Text:
SELECT [p].[Id], [p].[Property], [c].[Id], [c].[Nullable], [c].[ParentId], [c].[Property]
FROM [ParentEntities] AS [p]
LEFT JOIN [ChildEntities] AS [c] ON [p].[Id] = [c].[ParentId]
WHERE [p].[Id] = 'Guid_1'
ORDER BY [p].[Id]
}
]
}
113 changes: 113 additions & 0 deletions src/Tests/IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,119 @@ public async Task Parent_child()
await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
}

[Fact]
public async Task Parent_child_with_id()
{
var parent = new ParentEntity
{
Property = "Value1"
};
var child1 = new ChildEntity
{
Property = "Child1",
Parent = parent
};
parent.Children.Add(child1);
var child2 = new ChildEntity
{
Property = "Child2",
Parent = parent
};
parent.Children.Add(child2);

var query = $@"
{{
parentEntities
{{
property
children(id:'{child1.Id}' )
{{
property
}}
}}
}}";
await using var database = await sqlInstance.Build();
await RunQuery(database, query, null, null, false, new object[] { parent, child1, child2 });
}

[Fact(Skip = "fix order")]
public async Task Parent_with_id_child()
{
var parent1 = new ParentEntity
{
Property = "Value1"
};
var parent2 = new ParentEntity
{
Property = "Value2"
};
var child1 = new ChildEntity
{
Property = "Child1",
Parent = parent1
};
parent1.Children.Add(child1);
var child2 = new ChildEntity
{
Property = "Child2",
Parent = parent1
};
parent1.Children.Add(child2);

var query = $@"
{{
parentEntities(id:'{parent1.Id}')
{{
property
children
{{
property
}}
}}
}}";
await using var database = await sqlInstance.Build();
await RunQuery(database, query, null, null, false, new object[] { parent1, parent2, child1, child2 });
}

[Fact]
public async Task Parent_with_id_child_with_id()
{
var parent1 = new ParentEntity
{
Property = "Value1"
};
var parent2 = new ParentEntity
{
Property = "Value2"
};
var child1 = new ChildEntity
{
Property = "Child1",
Parent = parent1
};
parent1.Children.Add(child1);
var child2 = new ChildEntity
{
Property = "Child2",
Parent = parent1
};
parent1.Children.Add(child2);

var query = $@"
{{
parentEntities(id:'{parent1.Id}')
{{
property
children(id:'{child1.Id}' )
{{
property
}}
}}
}}";
await using var database = await sqlInstance.Build();
await RunQuery(database, query, null, null, false, new object[] { parent1, parent2, child1, child2 });
}

[Fact]
public async Task Many_children()
{
Expand Down