Skip to content

Commit 0faf8c7

Browse files
authored
Merge pull request #759 from howjmay/graphql
feat: Impl GraphQL client
2 parents 1722c8f + 44d01bb commit 0faf8c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+20782
-59
lines changed

clients/graphql_client.go

Lines changed: 3174 additions & 0 deletions
Large diffs are not rendered by default.

clients/graphql_client_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package clients_test
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/Khan/genqlient/graphql"
10+
"github.com/iotaledger/wasp/v2/clients"
11+
"github.com/iotaledger/wasp/v2/clients/iota-go/iotaconn"
12+
"github.com/iotaledger/wasp/v2/clients/iota-go/iotago"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestGraphQL(t *testing.T) {
17+
client := clients.NewGraphQLClient(iotaconn.TestnetGraphQLEndpointURL)
18+
19+
t.Run("Standard API Call", func(t *testing.T) {
20+
addr, err := iotago.AddressFromHex("0x7a89979774c55814f41fc1e3354e2ba38d3d62096d469d86b3132e947de1e8da")
21+
require.NoError(t, err)
22+
resp, err := client.GetAllBalances(context.TODO(), addr)
23+
require.NoError(t, err)
24+
require.NotNil(t, resp)
25+
26+
fmt.Println("All Balances:", resp)
27+
})
28+
29+
t.Run("Custom query", func(t *testing.T) {
30+
q := `
31+
query GetAllBalances($owner: SuiAddress!, $limit: Int, $cursor: String) {
32+
address(address: $owner) {
33+
balances(first: $limit, after: $cursor) {
34+
pageInfo {
35+
hasNextPage
36+
endCursor
37+
}
38+
nodes {
39+
coinType {
40+
repr
41+
}
42+
coinObjectCount
43+
totalBalance
44+
}
45+
}
46+
}
47+
}`
48+
b, err := client.Query(context.Background(), q, map[string]interface{}{
49+
"owner": "0xe25afa59deccfec819aaa67bf14f049982d2a1ca87c49c8614da5ea2dc438f72",
50+
})
51+
require.NoError(t, err)
52+
fmt.Println("raw bytes:", string(b))
53+
54+
var resp graphql.Response
55+
err = json.Unmarshal(b, &resp)
56+
require.NoError(t, err)
57+
fmt.Println("unmarshalled:", resp)
58+
})
59+
}

clients/iota-go/iotaclient/api_coin_query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c *Client) GetAllBalances(ctx context.Context, owner *iotago.Address) ([]*
1818
type GetAllCoinsRequest struct {
1919
Owner *iotago.Address
2020
Cursor *iotago.ObjectID // optional
21-
Limit uint // optional
21+
Limit int // optional
2222
}
2323

2424
// start with the first object when cursor is nil
@@ -49,9 +49,9 @@ func (c *Client) GetCoinMetadata(ctx context.Context, coinType string) (*iotajso
4949

5050
type GetCoinsRequest struct {
5151
Owner *iotago.Address
52-
CoinType *string // optional
53-
Cursor *iotago.ObjectID // optional
54-
Limit uint // optional
52+
CoinType *string // optional
53+
Cursor *string // optional
54+
Limit int // optional
5555
}
5656

5757
// GetCoins to use default iotago coin(0x2::iota::IOTA) when coinType is nil

clients/iota-go/iotaclient/api_exented.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *Client) GetDynamicFieldObject(
3434
type GetDynamicFieldsRequest struct {
3535
ParentObjectID *iotago.ObjectID
3636
Cursor *iotago.ObjectID // optional
37-
Limit *uint // optional
37+
Limit *int // optional
3838
}
3939

4040
func (c *Client) GetDynamicFields(
@@ -55,7 +55,7 @@ type GetOwnedObjectsRequest struct {
5555
Cursor *iotago.ObjectID
5656
// [optional] Limit is the maximum number of items returned per page, defaults to [QUERY_MAX_RESULT_LIMIT_OBJECTS] if not
5757
// provided
58-
Limit *uint
58+
Limit *int
5959
}
6060

6161
func (c *Client) GetOwnedObjects(
@@ -78,7 +78,7 @@ func (c *Client) GetOwnedObjects(
7878
type QueryEventsRequest struct {
7979
Query *iotajsonrpc.EventFilter
8080
Cursor *iotajsonrpc.EventId // optional
81-
Limit *uint // optional
81+
Limit *int // optional
8282
DescendingOrder bool // optional
8383
}
8484

@@ -93,7 +93,7 @@ func (c *Client) QueryEvents(
9393
type QueryTransactionBlocksRequest struct {
9494
Query *iotajsonrpc.IotaTransactionBlockResponseQuery
9595
Cursor *iotago.TransactionDigest // optional
96-
Limit *uint // optional
96+
Limit *int // optional
9797
DescendingOrder bool // optional
9898
}
9999

@@ -125,7 +125,7 @@ func (c *Client) ResolveNameServiceAddress(ctx context.Context, iotaName string)
125125
type ResolveNameServiceNamesRequest struct {
126126
Owner *iotago.Address
127127
Cursor *iotago.ObjectID // optional
128-
Limit *uint // optional
128+
Limit *int // optional
129129
}
130130

131131
func (c *Client) ResolveNameServiceNames(

clients/iota-go/iotaclient/api_write.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
type DevInspectTransactionBlockRequest struct {
1313
SenderAddress *iotago.Address
1414
TxKindBytes iotago.Base64Data
15-
GasPrice *iotajsonrpc.BigInt // optional
16-
Epoch *uint64 // optional
15+
GasPrice *iotajsonrpc.BigInt // optional
16+
Epoch *uint64 // optional
17+
Options *iotajsonrpc.IotaTransactionBlockResponseOptions // optional
1718
// additional_args // optional // FIXME
1819
}
1920

@@ -37,12 +38,17 @@ func (c *Client) DevInspectTransactionBlock(
3738
)
3839
}
3940

41+
type DryRunTransactionRequest struct {
42+
TxDataBytes iotago.Base64Data
43+
Options *iotajsonrpc.IotaTransactionBlockResponseOptions // optional
44+
}
45+
4046
func (c *Client) DryRunTransaction(
4147
ctx context.Context,
42-
txDataBytes iotago.Base64Data,
48+
req DryRunTransactionRequest,
4349
) (*iotajsonrpc.DryRunTransactionBlockResponse, error) {
4450
var resp iotajsonrpc.DryRunTransactionBlockResponse
45-
return &resp, c.transport.Call(ctx, &resp, dryRunTransactionBlock, txDataBytes)
51+
return &resp, c.transport.Call(ctx, &resp, dryRunTransactionBlock, req.TxDataBytes)
4652
}
4753

4854
type ExecuteTransactionBlockRequest struct {

clients/iota-go/iotaclient/iotaclienttest/api_coin_query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestGetAllCoins(t *testing.T) {
3333
ctx context.Context
3434
address *iotago.Address
3535
cursor *iotago.ObjectID
36-
limit uint
36+
limit int
3737
}
3838

3939
tests := []struct {

clients/iota-go/iotaclient/iotaclienttest/api_exented_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestGetOwnedObjects(t *testing.T) {
8181
ShowContent: true,
8282
},
8383
}
84-
limit := uint(10)
84+
limit := int(10)
8585
objs, err := client.GetOwnedObjects(
8686
context.Background(), iotaclient.GetOwnedObjectsRequest{
8787
Address: signer.Address(),
@@ -106,7 +106,7 @@ func TestGetOwnedObjects(t *testing.T) {
106106
ShowContent: true,
107107
},
108108
}
109-
limit := uint(9)
109+
limit := int(9)
110110
objs, err := client.GetOwnedObjects(
111111
context.Background(), iotaclient.GetOwnedObjectsRequest{
112112
Address: signer.Address(),
@@ -148,12 +148,12 @@ func TestGetOwnedObjects(t *testing.T) {
148148

149149
func TestQueryTransactionBlocks(t *testing.T) {
150150
api := l1starter.Instance().L1Client()
151-
limit := uint(10)
151+
limit := int(10)
152152
type args struct {
153153
ctx context.Context
154154
query *iotajsonrpc.IotaTransactionBlockResponseQuery
155155
cursor *iotago.TransactionDigest
156-
limit *uint
156+
limit *int
157157
descendingOrder bool
158158
}
159159
tests := []struct {

clients/iota-go/iotaclient/iotaclienttest/api_transaction_builder_test.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ func TestPay(t *testing.T) {
174174
)
175175
require.NoError(t, err)
176176

177-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
177+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
178+
TxDataBytes: txn.TxBytes,
179+
})
178180
require.NoError(t, err)
179181
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
180182
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -194,7 +196,7 @@ func TestPayAllIota(t *testing.T) {
194196
signer := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
195197
recipient := iotatest.MakeSignerWithFunds(1, l1starter.Instance().FaucetURL())
196198

197-
limit := uint(3)
199+
limit := int(3)
198200
coinPages, err := client.GetCoins(
199201
context.Background(), iotaclient.GetCoinsRequest{
200202
Owner: signer.Address(),
@@ -218,7 +220,9 @@ func TestPayAllIota(t *testing.T) {
218220
)
219221
require.NoError(t, err)
220222

221-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
223+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
224+
TxDataBytes: txn.TxBytes,
225+
})
222226
require.NoError(t, err)
223227
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
224228
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -235,7 +239,7 @@ func TestPayAllIota(t *testing.T) {
235239
}
236240
// all the input objects are merged into the first input object
237241
// except the first input object, all the other input objects are deleted
238-
require.Equal(t, limit-1, delObjNum)
242+
require.Equal(t, limit-1, int(delObjNum))
239243

240244
// one output balance and one input balance
241245
require.Len(t, simulate.BalanceChanges, 2)
@@ -254,7 +258,7 @@ func TestPayIota(t *testing.T) {
254258
recipient1 := iotatest.MakeSignerWithFunds(1, l1starter.Instance().FaucetURL())
255259
recipient2 := iotatest.MakeSignerWithFunds(2, l1starter.Instance().FaucetURL())
256260

257-
limit := uint(4)
261+
limit := int(4)
258262
coinPages, err := client.GetCoins(
259263
context.Background(), iotaclient.GetCoinsRequest{
260264
Owner: signer.Address(),
@@ -285,7 +289,9 @@ func TestPayIota(t *testing.T) {
285289
)
286290
require.NoError(t, err)
287291

288-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
292+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
293+
TxDataBytes: txn.TxBytes,
294+
})
289295
require.NoError(t, err)
290296
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
291297
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -309,7 +315,7 @@ func TestPayIota(t *testing.T) {
309315

310316
// all the input objects are merged into the first input object
311317
// except the first input object, all the other input objects are deleted
312-
require.Equal(t, limit-1, delObjNum)
318+
require.Equal(t, limit-1, int(delObjNum))
313319
// 1 for recipient1, and 2 for recipient2
314320
require.Equal(t, amountNum, createdObjNum)
315321

@@ -361,7 +367,7 @@ func TestSplitCoin(t *testing.T) {
361367
client := l1starter.Instance().L1Client()
362368
signer := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
363369

364-
limit := uint(4)
370+
limit := int(4)
365371
coinPages, err := client.GetCoins(
366372
context.Background(), iotaclient.GetCoinsRequest{
367373
Owner: signer.Address(),
@@ -386,7 +392,9 @@ func TestSplitCoin(t *testing.T) {
386392
)
387393
require.NoError(t, err)
388394

389-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
395+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
396+
TxDataBytes: txn.TxBytes,
397+
})
390398
require.NoError(t, err)
391399
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
392400
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -402,7 +410,7 @@ func TestSplitCoinEqual(t *testing.T) {
402410
client := l1starter.Instance().L1Client()
403411
signer := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
404412

405-
limit := uint(4)
413+
limit := int(4)
406414
coinPages, err := client.GetCoins(
407415
context.Background(), iotaclient.GetCoinsRequest{
408416
Owner: signer.Address(),
@@ -424,7 +432,9 @@ func TestSplitCoinEqual(t *testing.T) {
424432
)
425433
require.NoError(t, err)
426434

427-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
435+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
436+
TxDataBytes: txn.TxBytes,
437+
})
428438
require.NoError(t, err)
429439
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
430440
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -441,7 +451,7 @@ func TestTransferObject(t *testing.T) {
441451
signer := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
442452
recipient := iotatest.MakeSignerWithFunds(1, l1starter.Instance().FaucetURL())
443453

444-
limit := uint(3)
454+
limit := int(3)
445455
coinPages, err := client.GetCoins(
446456
context.Background(), iotaclient.GetCoinsRequest{
447457
Owner: signer.Address(),
@@ -462,7 +472,9 @@ func TestTransferObject(t *testing.T) {
462472
)
463473
require.NoError(t, err)
464474

465-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
475+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
476+
TxDataBytes: txn.TxBytes,
477+
})
466478
require.NoError(t, err)
467479
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
468480
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -478,7 +490,7 @@ func TestTransferIota(t *testing.T) {
478490
signer := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
479491
recipient := iotatest.MakeSignerWithFunds(1, l1starter.Instance().FaucetURL())
480492

481-
limit := uint(3)
493+
limit := int(3)
482494
coinPages, err := client.GetCoins(
483495
context.Background(), iotaclient.GetCoinsRequest{
484496
Owner: signer.Address(),
@@ -500,7 +512,9 @@ func TestTransferIota(t *testing.T) {
500512
)
501513
require.NoError(t, err)
502514

503-
simulate, err := client.DryRunTransaction(context.Background(), txn.TxBytes)
515+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
516+
TxDataBytes: txn.TxBytes,
517+
})
504518
require.NoError(t, err)
505519
require.Empty(t, simulate.Effects.Data.V1.Status.Error)
506520
require.True(t, simulate.Effects.Data.IsSuccess())

clients/iota-go/iotaclient/iotaclienttest/api_write_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestDevInspectTransactionBlock(t *testing.T) {
2020
client := l1starter.Instance().L1Client()
2121
sender := iotatest.MakeSignerWithFunds(0, l1starter.Instance().FaucetURL())
2222

23-
limit := uint(3)
23+
limit := int(3)
2424
coinPages, err := client.GetCoins(
2525
context.Background(), iotaclient.GetCoinsRequest{
2626
Owner: sender.Address(),
@@ -77,7 +77,9 @@ func TestDryRunTransaction(t *testing.T) {
7777
)
7878
require.NoError(t, err)
7979

80-
resp, err := api.DryRunTransaction(context.Background(), tx.TxBytes)
80+
resp, err := api.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
81+
TxDataBytes: tx.TxBytes,
82+
})
8183
require.NoError(t, err)
8284
require.True(t, resp.Effects.Data.IsSuccess())
8385
require.Empty(t, resp.Effects.Data.V1.Status.Error)

clients/iota-go/iotaclient/iotaclienttest/client_stake_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func TestRequestAddDelegation(t *testing.T) {
4747
)
4848
require.NoError(t, err)
4949

50-
simulate, err := client.DryRunTransaction(context.Background(), txBytes)
50+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
51+
TxDataBytes: txBytes,
52+
})
5153
require.NoError(t, err)
5254
require.Equal(t, "", simulate.Effects.Data.V1.Status.Error)
5355
require.True(t, simulate.Effects.Data.IsSuccess())
@@ -91,7 +93,9 @@ func TestRequestWithdrawDelegation(t *testing.T) {
9193
)
9294
require.NoError(t, err)
9395

94-
simulate, err := client.DryRunTransaction(context.Background(), txBytes)
96+
simulate, err := client.DryRunTransaction(context.Background(), iotaclient.DryRunTransactionRequest{
97+
TxDataBytes: txBytes,
98+
})
9599
require.NoError(t, err)
96100
require.Equal(t, "", simulate.Effects.Data.V1.Status.Error)
97101
require.True(t, simulate.Effects.Data.IsSuccess())

0 commit comments

Comments
 (0)