From 02a5f1c4d0a38e85df7bb6bf863d9736e4739cf4 Mon Sep 17 00:00:00 2001 From: Sebastian Modersohn Date: Wed, 20 Apr 2022 13:20:14 +0200 Subject: [PATCH 1/2] Fix Invalid query error when using non-hosted graph * as described in #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 --- Shared/Services/LoopringGraphQLService.cs | 39 ++++++------------- .../LoopringGraphTests/TestTransaction.cs | 17 ++++++++ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Shared/Services/LoopringGraphQLService.cs b/Shared/Services/LoopringGraphQLService.cs index 20ae7e7..72d543b 100644 --- a/Shared/Services/LoopringGraphQLService.cs +++ b/Shared/Services/LoopringGraphQLService.cs @@ -298,7 +298,7 @@ query transaction($id: ID!) { } } - public async Task GetTransactions(int skip, int first, string? blockId = null, string? typeName = null, CancellationToken cancellationToken = default) + public async Task GetTransactions(int skip, int first, string? typeName = null, CancellationToken cancellationToken = default) { var transactionsQuery = @" query transactions( @@ -306,8 +306,7 @@ query transactions( $first: Int $orderBy: Transaction_orderBy $orderDirection: OrderDirection - $block: Block_height - $where: Transaction_filter + $whereFilter: Transaction_filter ) { proxy(id: 0) { transactionCount @@ -333,8 +332,7 @@ query transactions( first: $first orderBy: $orderBy orderDirection: $orderDirection - block: $block - where: $where + where: $whereFilter ) { id internalID @@ -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 { @@ -432,9 +411,8 @@ query transactions( skip = skip, first = first, orderBy = "internalID", - orderDirection = "desc" - , - where = new + orderDirection = "desc", + whereFilter = new { typename = typeName } @@ -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, diff --git a/xUnitTests/LoopringGraphTests/TestTransaction.cs b/xUnitTests/LoopringGraphTests/TestTransaction.cs index 07f790d..1b24a56 100644 --- a/xUnitTests/LoopringGraphTests/TestTransaction.cs +++ b/xUnitTests/LoopringGraphTests/TestTransaction.cs @@ -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); + } + } + } } } From 6b16378c5bb3c33f629bb2a66af8d5cde9266b63 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 21 Apr 2022 08:27:46 +1000 Subject: [PATCH 2/2] fix for block transactions not showing and new unit test for blocks --- Lexplorer/Pages/BlockDetails.razor | 2 +- Shared/Services/LoopringGraphQLService.cs | 25 +++++++++++++++++-- .../LoopringGraphTests/TestTransaction.cs | 11 +++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Lexplorer/Pages/BlockDetails.razor b/Lexplorer/Pages/BlockDetails.razor index 4699619..e125e7e 100644 --- a/Lexplorer/Pages/BlockDetails.razor +++ b/Lexplorer/Pages/BlockDetails.razor @@ -153,7 +153,7 @@ isTransactionLoading = true; string transactionDatacacheKey = $"blockDetailTransactions-{blockId}-page{gotoPage}"; var transactionData = await AppCache.GetOrAddAsyncNonNull(transactionDatacacheKey, - async () => await LoopringGraphQLService.GetTransactions((gotoPage - 1) * pageSize, pageSize, blockNumber, cancellationToken: localCTS.Token), + async () => await LoopringGraphQLService.GetTransactions((gotoPage - 1) * pageSize, pageSize, blockId: blockNumber, cancellationToken: localCTS.Token), DateTimeOffset.UtcNow.AddMinutes(5)); transactions = transactionData?.data?.transactions ?? new List(); isTransactionLoading = false; diff --git a/Shared/Services/LoopringGraphQLService.cs b/Shared/Services/LoopringGraphQLService.cs index 72d543b..dbb9573 100644 --- a/Shared/Services/LoopringGraphQLService.cs +++ b/Shared/Services/LoopringGraphQLService.cs @@ -298,7 +298,7 @@ query transaction($id: ID!) { } } - public async Task GetTransactions(int skip, int first, string? typeName = null, CancellationToken cancellationToken = default) + public async Task GetTransactions(int skip, int first, string? blockId = null, string? typeName = null, CancellationToken cancellationToken = default) { var transactionsQuery = @" query transactions( @@ -306,6 +306,7 @@ query transactions( $first: Int $orderBy: Transaction_orderBy $orderDirection: OrderDirection + $block: Block_height $whereFilter: Transaction_filter ) { proxy(id: 0) { @@ -332,6 +333,7 @@ query transactions( first: $first orderBy: $orderBy orderDirection: $orderDirection + block: $block where: $whereFilter ) { id @@ -401,7 +403,26 @@ query transactions( var request = new RestRequest(); request.AddHeader("Content-Type", "application/json"); - if (typeName != null) + if (blockId != null) + { + request.AddJsonBody(new + { + query = transactionsQuery, + variables = new + { + skip = skip, + first = first, + orderBy = "internalID", + orderDirection = "desc" + , + whereFilter = new + { + block = blockId + } + } + }); + } + else if (typeName != null) { request.AddJsonBody(new { diff --git a/xUnitTests/LoopringGraphTests/TestTransaction.cs b/xUnitTests/LoopringGraphTests/TestTransaction.cs index 1b24a56..7fc943f 100644 --- a/xUnitTests/LoopringGraphTests/TestTransaction.cs +++ b/xUnitTests/LoopringGraphTests/TestTransaction.cs @@ -31,7 +31,7 @@ public async void TestGetTransaction() [InlineData("Swap")] public async void TestGetTransations(string? typeName) { - var transactions = await service.GetTransactions(0, 5, typeName); + var transactions = await service.GetTransactions(0, 5, typeName: typeName); Assert.NotNull(transactions); Assert.NotEmpty(transactions?.data?.transactions); if (!String.IsNullOrEmpty(typeName)) @@ -42,6 +42,15 @@ public async void TestGetTransations(string? typeName) } } } + + [Theory] + [InlineData("19527")] + public async void TestGetBlockTransations(string? blockId) + { + var transactions = await service.GetTransactions(0, 5, blockId: blockId); + Assert.NotNull(transactions); + Assert.NotEmpty(transactions?.data?.transactions); + } } }