Display hash node posts in your portfolio website using Flask

Define a route to fetch and display the 3 most recent posts

# Define the GraphQL query as a constant
graphql_query = """
{
  user(username: "andrewadhikari") {
    publication {
      posts(page: 0) {
        slug
        title
        brief
        coverImage
        readTime
        dateAdded
      }
      description
    }
    username
    photo
  }
}
# Define a route to fetch and display the 3 most recent posts
@app.route('/recent_posts', methods=['GET'])
def recent_posts():
  try:
    response = requests.post('https://api.hashnode.com/',
                             json={'query': graphql_query})
    data = response.json()
    user_data = data.get('data', {}).get('user', {})
    posts = user_data.get('publication', {}).get('posts', [])
    description = user_data.get('publication', {}).get('description', '')
    username = user_data.get('username', '')
    photo = user_data.get('photo', '')

    # Limit to the 3 most recent posts
    recent_posts = posts[:3]

    return render_template('blog.html',
                           user={
                               'description': description,
                               'username': username,
                               'photo': photo,
                               'recent_posts': recent_posts,
                           })
  except Exception as e:
    return jsonify({'error': str(e)})