Skip to content

Instantly share code, notes, and snippets.

@jpedroschmitz
Created March 7, 2022 20:41
Show Gist options
  • Save jpedroschmitz/fadf1cac858396f5d18dce7fe97c0f91 to your computer and use it in GitHub Desktop.
Save jpedroschmitz/fadf1cac858396f5d18dce7fe97c0f91 to your computer and use it in GitHub Desktop.
import { GraphQLClient, gql } from 'graphql-request'
import { htmlToSlateAST } from '@graphcms/html-to-slate-ast';
const client = new GraphQLClient(`${process.env.GRAPHCMS_ENDPOINT}`, {
headers: {
Authorization: `Bearer ${process.env.GRAPHCMS_TOKEN}`,
},
});
const newArticleQuery = gql`
mutation newArticle($title: String!, $content: RichTextAST) {
createArticle(data: { title: $title, content: $content }) {
id
title
content {
html
raw
}
}
}
`
async function main() {
const htmlString = '<ul><li>Hey <a href="thing">link text</a> here</li></ul>';
const ast = await htmlToSlateAST(htmlString);
// Create a RichText object from the AST
const content = {
children: ast
}
const data = await client.request(newArticleQuery, {
title: 'Example title for an article',
content // Pass the RichText object as the content
})
console.log(data)
}
main()
.then(() => process.exit(0))
.catch((e) => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment