Skip to content

Commit

Permalink
Added Sui GraphQL to cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC01 committed Dec 28, 2023
1 parent 2a39a86 commit ac204c5
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/cookbook/code/aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ editLink: true

- Sui cli `sui client` will automatically generate a alias file (~/.sui/sui_config/sui.aliases) starting in version 1.16.0
- The alias file has a 1:1 mapping of alias names to the public key of the associated keypair
- The alias name must start with a letter and can contain only letters, digits, hyphens (-), or underscores (_)
- Command line caveats:
- To rename an alias you will need to edit the alias file via editor
- There is no known alias name length
Expand Down
157 changes: 157 additions & 0 deletions docs/src/cookbook/code/graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
title: Sui GraphQL RPC
contributors: true
editLink: true
---

## Facts

::: tip Fact Sheet

- Sui GraphQL RPC is currently in **_beta_**
- Sui GraphQL RPC beta operates on a snapshot of data, it is not maintaining beyond:
- testnet data timestamp: "2023-12-16T19:07:30.993Z"
- mainnet data timestamp: "2023-11-21T22:03:27.667Z"
- devnet not supported
- Sui GraphQL RPC will eventually _replace_ the JSON RPC
- PySui support for Sui GraphQL RPC:
- Release 0.50.0 includes an 'experimental' implementation, subject to change
- Provides Synchronous and asynchronous GraphQL clients
- Only 'read' queries are supported at the time of this writing
- Introduces `QueryNodes` that are the equivalent to pysui `Builders`
- Parity of QueryNodes to Builders is ongoing
- Exposes ability for developers to write their own GraphQL queries
- Must point to either `testnet` or `mainnet`
:::

## Generating GraphQL schema

::: code-tabs

@tab sui

```shell
NA at this time
```

@tab pysui

```python
from pysui.sui.sui_pgql.clients import SuiGQLClient
from pysui import SuiConfig

def main():
"""Dump Sui GraphQL Schema."""
# Initialize synchronous client (must be mainnet or testnet)
client_init = SuiGQLClient(config=SuiConfig.default_config(),write_schema=True)

print("Schema dumped to: `latest_schemaVERSION.graqhql`")

if __name__ == "__main__":
main()

```
:::

## Query example 1

For pysui there are 2 comon ways to create a query. This demonstrates **_using QueryNodes (predefined queries as part of pysui SDK)_**

::: code-tabs

@tab sui

```shell
NA at this time
```

@tab pysui

```python
#
"""Query using predefined pysui QueryNode."""

from pysui.sui.sui_pgql.clients import SuiGQLClient
import pysui.sui.sui_pgql.pgql_query as qn
from pysui import SuiConfig

def main(client: SuiGQLClient):
"""Fetch 0x2::sui::SUI (default) for owner."""
# GetCoins defaults to '0x2::sui::SUI' coin type so great for owners gas listing
# Replace <ADDRESS_STRING> with a valid testnet or mainnet address
qres = client.execute_query(
with_query_node=qn.GetCoins(
owner="<ADDRESS_STRING>"
)
)
# Results are mapped to dataclasses/dataclasses-json
print(qres.to_json(indent=2))

if __name__ == "__main__":
# Initialize synchronous client (must be mainnet or testnet)
client_init = SuiGQLClient(config=SuiConfig.default_config(),write_schema=False)
main(client_init)
```
:::

## Query example 2

For pysui there are 2 comon ways to create a query. This demonstrates **_using a query string_**

::: code-tabs

@tab sui

```shell
NA at this time
```

@tab pysui

```python
#
"""Query using a query string."""

from pysui.sui.sui_pgql.clients import SuiGQLClient
from pysui import SuiConfig

def main(client: SuiGQLClient):
"""Configuration and protocol information."""
_QUERY = """
query {
chainIdentifier
checkpointConnection (last: 1) {
nodes {
sequenceNumber
timestamp
}
}
serviceConfig {
enabledFeatures
maxQueryDepth
maxQueryNodes
maxDbQueryCost
maxPageSize
requestTimeoutMs
maxQueryPayloadSize
}
protocolConfig {
protocolVersion
configs {
key
value
}
featureFlags {
key
value
}
}
}
"""
qres = client.execute_query(with_string=_QUERY)
print(qres)

if __name__ == "__main__":
client_init = SuiGQLClient(config=SuiConfig.default_config(),write_schema=False)
main(client_init)```
:::

0 comments on commit ac204c5

Please sign in to comment.