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
20 changes: 20 additions & 0 deletions LinQL.GraphQL.Client.Tests/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public async Task TestSendQuery()
result.Data.Should().Be(12345);
}

[Fact]
public async Task TestSendProjectionQuery()
{
var result = await this.client.SendAsync((TestQuery q) => q.ExecuteEchoOperation("echo").Project(e => new { e.Echo, e.Number }));

result.Errors.Should().BeNull();
result.Data.Number.Should().Be(42);
result.Data.Echo.Should().Be("echo");
}

[Fact]
public async Task TestSendMutation()
{
Expand All @@ -60,8 +70,12 @@ public void Dispose()
public class TestQueryType
{
public int GetNumber() => 12345;

public TestObjectType EchoOperation(string toEcho) => new(toEcho, 42);
}

public record TestObjectType(string Echo, int Number);

public class TestMutationType
{
public NumberResult SetNumber([GraphQLArgument(GQLType = "Int")] int input) => new() { Number = input };
Expand All @@ -79,6 +93,12 @@ public class TestQuery : RootType<TestQuery>

[GraphQLOperation, GraphQLField(Name = "number")]
public int GetNumber() => this.Number;

[GraphQLOperation, GraphQLField(Name = "echoOperation")]
public TestObjectType ExecuteEchoOperation([GraphQLArgument(GQLType = "String!")] string toEcho)
=> this.EchoOperation;

public TestObjectType EchoOperation { get; set; }
}

[OperationType(RootOperationType.Mutation)]
Expand Down
14 changes: 14 additions & 0 deletions LinQL.Tests/Translation/TranslationProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public void SelectInterfaceArrayMultipleTypesSelectAll()

private record Projection(int Number, string? Text);

private record NestedProjection(float Float, Projection Projection);

[Fact]
public void ProjectArrayOfInterface()
=> this.Test<InterfaceRootType, object>(x => x.ArrayOfInterfaces.Select(a => new Projection(a.Number, a.Text)));
Expand All @@ -205,6 +207,18 @@ private void TestInclude<TRoot, TData>(Expression<Func<TRoot, TData>> expression
Snapshot.Match(request.Query);
}

[Fact]
public void Project()
=> this.Test<NestedOperationType, Projection>(x => x.Operation.GetNumber("project me").Project(y => new Projection(y.Number, y.Text)));

[Fact]
public void ProjectAcrossMultipleLevels()
=> this.Test<NestedOperationType, Projection>(x => new Projection(x.Number, x.Operation.GetNumber("project me").Project(y => y.Text)));

[Fact]
public void ProjectSameOperationMultipleTimes()
=> this.Test<NestedOperationType, NestedProjection>(x => new NestedProjection(x.Operation.Float, x.Operation.GetNumber("project me").Project(y => new Projection(y.Number, y.Text))));

private void Test<TRoot, TData>(Expression<Func<TRoot, TData>> expression)
where TRoot : RootType<TRoot>
=> this.TestInclude(expression, _ => { });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query linql(
$var1: String!
)
{
operation {
getNumber(
value: $var1
) {
number
text
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query linql(
$var1: String!
)
{
number
operation {
getNumber(
value: $var1
) {
text
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query linql(
$var1: String!
)
{
operation {
float
getNumber(
value: $var1
) {
number
text
}
}
}
10 changes: 10 additions & 0 deletions LinQL/SelectExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public static OneOf<T, TResult> On<T, TMaybe, TResult>(this OneOf<T, TResult> th
/// <param name="that">The type.</param>
/// <returns>The type.</returns>
public static T SelectAll<T>(this T that) => that;

/// <summary>
/// Instruct the query to get all scalar fields on this type.
/// </summary>
/// <typeparam name="T">The type to select from.</typeparam>
/// <typeparam name="TResult">The type to project to.</typeparam>
/// <param name="that">The type.</param>
/// <param name="projection">The expression required to create <typeparamref name="TResult"/> from <typeparamref name="T"/>.</param>
/// <returns>The projection.</returns>
public static TResult Project<T, TResult>(this T that, Expression<Func<T, TResult>> projection) => projection.CompileFast()(that);
}
Loading