Skip to content

Commit

Permalink
Fix Invalid query error when using non-hosted graph
Browse files Browse the repository at this point in the history
* as described in fudgebucket27#50 - the non-hosted graphs don't like unused
  query parameters
* removed block from query completely, was never used
* if no typename is provided, delete the lines in the query
* added new tests for GetTransactions
  • Loading branch information
modersohn committed Apr 20, 2022
1 parent 8a6274b commit 02a5f1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
39 changes: 11 additions & 28 deletions Shared/Services/LoopringGraphQLService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,15 @@ query transaction($id: ID!) {
}
}

public async Task<Transactions?> GetTransactions(int skip, int first, string? blockId = null, string? typeName = null, CancellationToken cancellationToken = default)
public async Task<Transactions?> GetTransactions(int skip, int first, string? typeName = null, CancellationToken cancellationToken = default)
{
var transactionsQuery = @"
query transactions(
$skip: Int
$first: Int
$orderBy: Transaction_orderBy
$orderDirection: OrderDirection
$block: Block_height
$where: Transaction_filter
$whereFilter: Transaction_filter
) {
proxy(id: 0) {
transactionCount
Expand All @@ -333,8 +332,7 @@ query transactions(
first: $first
orderBy: $orderBy
orderDirection: $orderDirection
block: $block
where: $where
where: $whereFilter
) {
id
internalID
Expand Down Expand Up @@ -403,26 +401,7 @@ query transactions(

var request = new RestRequest();
request.AddHeader("Content-Type", "application/json");
if (blockId != null)
{
request.AddJsonBody(new
{
query = transactionsQuery,
variables = new
{
skip = skip,
first = first,
orderBy = "internalID",
orderDirection = "desc"
,
where = new
{
block = blockId
}
}
});
}
else if (typeName != null)
if (typeName != null)
{
request.AddJsonBody(new
{
Expand All @@ -432,9 +411,8 @@ query transactions(
skip = skip,
first = first,
orderBy = "internalID",
orderDirection = "desc"
,
where = new
orderDirection = "desc",
whereFilter = new
{
typename = typeName
}
Expand All @@ -443,6 +421,11 @@ query transactions(
}
else
{
//remove unused parameter to fix "Invalid query" error with non-hosted graph
//split and re-join all lines which don't contain the word "whereFilter"
transactionsQuery = String.Join(Environment.NewLine,
transactionsQuery.Split(Environment.NewLine).Where(
(a) => !(a.Contains("whereFilter"))));
request.AddJsonBody(new
{
query = transactionsQuery,
Expand Down
17 changes: 17 additions & 0 deletions xUnitTests/LoopringGraphTests/TestTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public async void TestGetTransaction()
Assert.NotNull(transaction);
Assert.Equal(testID, transaction!.id);
}

[Theory]
[InlineData(null)]
[InlineData("Swap")]
public async void TestGetTransations(string? typeName)
{
var transactions = await service.GetTransactions(0, 5, typeName);
Assert.NotNull(transactions);
Assert.NotEmpty(transactions?.data?.transactions);
if (!String.IsNullOrEmpty(typeName))
{
foreach (var transaction in transactions?.data?.transactions!)
{
Assert.Equal(typeName, transaction.typeName);
}
}
}
}
}

0 comments on commit 02a5f1c

Please sign in to comment.