Skip to content

Commit

Permalink
[graphql/docs] Add initial graphql docs content (MystenLabs#15366)
Browse files Browse the repository at this point in the history
## Description 

Added an initial draft of the GraphQL docs we want to have. 
`Concepts/GraphQL RPC` 
`Getting-Started/GraphQL RPC`

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

---------

Co-authored-by: ronny-mysten <[email protected]>
  • Loading branch information
stefan-mysten and ronny-mysten authored Jan 23, 2024
1 parent e15d51a commit 8e24644
Show file tree
Hide file tree
Showing 13 changed files with 1,403 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/sui-graphql-rpc/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Command {
#[clap(short, long)]
path: Option<PathBuf>,
},
GenerateDocsExamples,
GenerateSchema {
/// Path to output GraphQL schema to, in SDL format.
#[clap(short, long)]
Expand Down
32 changes: 32 additions & 0 deletions crates/sui-graphql-rpc/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,38 @@ pub fn load_examples() -> anyhow::Result<Vec<ExampleQueryGroup>> {
Ok(groups)
}

/// This generates a markdown page with all the examples, to be used in the docs site
pub fn generate_examples_for_docs() -> anyhow::Result<String> {
let groups = load_examples()?;

let mut output = BufWriter::new(Vec::new());
let mut md = Markdown::new(&mut output);
md.write(
r#"---
title: Examples
description: Query examples for working with the Sui GraphQL RPC.
---
"#,
)?;
md.write("This page showcases a number of queries to interact with the network. These examples can also be found in the [repository](https://github.com/MystenLabs/sui/tree/main/crates/sui-graphql-rpc/examples). You can use the [interactive online IDE](https://mainnet.sui.io/rpc/graphql) to run these examples.")?;
for group in groups.iter() {
let group_name = regularize_string(&group.name);
md.write(group_name.heading(2))
.map_err(|e| anyhow::anyhow!(e))?;
for query in group.queries.iter() {
let name = regularize_string(&query.name);
md.write(name.heading(3)).map_err(|e| anyhow::anyhow!(e))?;
let query = query.contents.lines().collect::<Vec<_>>().join("\n");
let content = format!("```graphql\n{}\n```", query);
md.write(content.as_str()).map_err(|e| anyhow::anyhow!(e))?;
}
}
let bytes = output.into_inner().map_err(|e| anyhow::anyhow!(e))?;
Ok(String::from_utf8(bytes)
.map_err(|e| anyhow::anyhow!(e))?
.replace('\\', ""))
}

pub fn generate_markdown() -> anyhow::Result<String> {
let groups = load_examples()?;

Expand Down
14 changes: 13 additions & 1 deletion crates/sui-graphql-rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ async fn main() {
);
}
}
Command::GenerateDocsExamples => {
let mut buf: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
// we are looking to put examples content in
// sui/docs/content/references/sui-api/graphql/examples.mdx
let filename = "docs/content/references/sui-api/graphql/examples.mdx";
buf.pop();
buf.pop();
buf.push(filename);
let content = sui_graphql_rpc::examples::generate_examples_for_docs()
.expect("Generating examples markdown file for docs failed");
std::fs::write(buf, content).expect("Writing examples markdown failed");
println!("Generated the docs example.mdx file and copied it to {filename}.");
}
Command::GenerateSchema { file } => {
let out = export_schema();
if let Some(file) = file {
Expand All @@ -43,7 +56,6 @@ async fn main() {
Command::GenerateExamples { file } => {
let new_content: String = sui_graphql_rpc::examples::generate_markdown()
.expect("Generating examples markdown failed");

let mut buf: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
buf.push("docs");
buf.push("examples.md");
Expand Down
Loading

0 comments on commit 8e24644

Please sign in to comment.