Skip to content

Commit

Permalink
Updated GraphQL code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC01 committed Dec 29, 2023
1 parent ac204c5 commit c105bb5
Showing 1 changed file with 61 additions and 11 deletions.
72 changes: 61 additions & 11 deletions docs/src/cookbook/code/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ editLink: true
::: 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 beta operates on a snapshot of data with timestamps:
- testnet: "2023-12-16T19:07:30.993Z"
- mainnet: "2023-11-21T22:03:27.667Z"
- devnet not currently supported
- Sui GraphQL RPC will eventually _replace_ the JSON RPC
- Sui support and constraints defined [Here](https://docs.sui.io/references/sui-api/beta-graph-ql#using-sui-graphql-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`
- SuiConfiguration must point to either Sui's `testnet` or `mainnet` RPC URLs
- pysui GraphQL documentation is in the [Docs](https://pysui.readthedocs.io/en/latest/graphql.html)
:::

## Generating GraphQL schema
Expand All @@ -31,7 +33,7 @@ editLink: true
@tab sui

```shell
NA at this time
NA
```

@tab pysui
Expand All @@ -55,7 +57,7 @@ NA at this time

## 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)_**
For pysui there are 3 comon ways to create a query. This demonstrates **_using QueryNodes (predefined queries as part of pysui SDK)_**

::: code-tabs

Expand Down Expand Up @@ -96,14 +98,25 @@ NA at this time

## Query example 2

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

::: code-tabs

@tab sui

```shell
NA at this time
# basic query
curl -X POST https://graphql-beta.mainnet.sui.io \
--header "Content-Type: application/json" \
--data '{
"query": "query { epoch { referenceGasPrice } }"
}'
# query with variables
curl -X POST https://graphql-beta.mainnet.sui.io \
--header "Content-Type: application/json" \
--data '{
"query": "query ($epochID: Int!) { epoch(id: $epochID) { referenceGasPrice } }", "variables": { "epochID": 123 }
}'
```

@tab pysui
Expand All @@ -116,7 +129,7 @@ NA at this time
from pysui import SuiConfig

def main(client: SuiGQLClient):
"""Configuration and protocol information."""
"""Execute a static string query."""
_QUERY = """
query {
chainIdentifier
Expand Down Expand Up @@ -153,5 +166,42 @@ NA at this time

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

```
:::

## Query example 3

For pysui there are 3 comon ways to create a query. This demonstrates **_using [gql](https://github.com/graphql-python/gql) the underlying GraphQL library_** to generate a DocumentNode

::: code-tabs

@tab sui

```shell
NA
```

@tab pysui

```python
#
"""Query using gql DocumentNode."""
from gql import gql
from pysui.sui.sui_pgql.clients import SuiGQLClient
from pysui import SuiConfig

def main(client: SuiGQLClient):
"""Execute a compiled string into DocumentNode."""
_QUERY = # Same query string as used Query example 2
qres = client.execute_query(with_document_node=gql(_QUERY))
print(qres)

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

```
:::

0 comments on commit c105bb5

Please sign in to comment.