Closed aunruh closed 9 months ago
It would be best to use variables instead of a multiline string for the query.
If you check the docs, there's a complete example on how you should do it.
import { GraphQLClient, gql } from 'graphql-request';
import { htmlToSlateAST } from '@graphcms/html-to-slate-ast';
const client = new GraphQLClient(`${process.env.HYGRAPH_ENDPOINT}`, {
headers: {
Authorization: `Bearer ${process.env.HYGRAPH_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));
Please notice the "Create a RichText object from the AST" part. That's how you should be doing it.
You got the error because the "ast object" you got back is not an object, it's an array and that's what you're sending. Instead, you should create an object like this:
const content = {
children: astArray,
};
And then send the content to the mutation.
When transforming my html to ast, when I upsert my data to Hygraph, I get the error:
"ClientError: expected RichText raw value to be object"
The ast object I get back is:
This is my code. I use
text_html: ${{ children: data.text_html }}
to insert the ast object.This is the whole error:
my package json: