Reputation: 163
This is the function I have been using create a post in Hashnode via there GraphQL APIs referring this blog Introducing Hashnode GraphQL API - Public Beta
def post_to_hashnode():
URL = "https://api.hashnode.com"
headers = {
'Authorization':HASHNODE_KEY,
'Content-Type': 'application/json'
}
query = """{
mutation {
createStory(
input: {
title: "The hashnode GraphQL API is here"
contentMarkdown: "<h1> Ahoy </h1>"
tags: [
{
_id: "56744721958ef13879b94c7e"
name: "General Programming"
slug: "programming"
}
]
}
) {
message
post{
title
}
}
}
}
"""
response = requests.post(URL, json={'query':query}, headers=headers)
return response.text
And the error it throws out is
{"errors":[{"message":"Cannot query field "mutation" on type "Query".","locations":[{"line":2,"column":3}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}
Tried another way of querying after looking at some more examples in the blog
def post_to_hashnode():
URL = "https://api.hashnode.com"
headers = {
'Authorization':HASHNODE_KEY,
'Content-Type': 'application/json'
}
body = json.dumps({
"query": "mutation createStory($input: CreateStoryInput!){ createStory(input: $input){ title } }",
"variables": {
"input": {
"title": "What are the e2e testing libraries you use ?",
"contentMarkdown": "I was wondering what e2e testing libaries do you use",
"tags": [
{
"_id": "56744723958ef13879b9549b",
"slug": "testing",
"name": "Testing"
}
],
"coverImage": "https://cdn.hashnode.com/res/hashnode/image-dev/upload/v1562665620141/tc-h-erqF.jpeg",
}
}
})
response = requests.post(URL, data=body, headers=headers)
return response.text
which still results in a different error though
{"errors":[{"message":"Cannot query field "title" on type "CreatePostOutput".","locations":[{"line":1,"column":78}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}
Upvotes: 1
Views: 529
Reputation: 299
"coverImage": "https://cdn.hashnode.com/res/hashnode/image-dev/upload/v1562665620141/tc-h-erqF.jpeg",
needs to be replaced with
"coverImageURL": "https://cdn.hashnode.com/res/hashnode/image-dev/upload/v1562665620141/tc-h-erqF.jpeg",
Upvotes: 0
Reputation: 154
You are right, need to rearrange a little bit.
import requests
def post_to_hashnode():
URL = "https://api.hashnode.com"
headers = {
'Authorization': "YOUR_KEY_HERE",
'Content-Type': 'application/json'
}
query = """
mutation createStory($input: CreateStoryInput!) {
createStory(input: $input) {
code
success
message
}
}
"""
response = requests.post(URL, json={
"query": query,
"variables": {
"input": {
"title": "Test posts here",
"contentMarkdown": "<h1> Ahoy </h1>",
"tags": [
{
"_id": "56744721958ef13879b94c7e",
"name": "General Programming",
"slug": "programming"
}
]
}
}
}, headers=headers)
return response.text
test = post_to_hashnode()
print(test)
Upvotes: 0