简体   繁体   English

Gatsby GraphQL错误:变量“ $ slug”从未在操作“ BlogPostQuery”中使用

[英]Gatsby GraphQL error: Variable “$slug” is never used in operation “BlogPostQuery”

I am unable to pull in the data of my Ghost blog using Gatsby. 我无法使用Gatsby提取我的Ghost博客的数据。 I am using Ghost as my back end and I am using a package to get the Ghost blog as a source. 我正在使用Ghost作为后端,并且正在使用一个软件包来获取Ghost博客作为源。 The problem is just getting the individual posts on the page. 问题只是在页面上获取单个帖子。 Here is blog-post.js : 这是blog-post.js

import React from "react";

export default ({ data }) => {
//   const post = data.allGhostPost.edges;
  return (
    <div>
      {/* <h1>{post.title}</h1> */}
      {/* <div dangerouslySetInnerHTML={{ __html: post.html }} /> */}
    </div>
  );
};

export const query = graphql`
  query BlogPostQuery($slug: String!) {
    allGhostPost {
        edges {
          node {
            id
            slug
            title
            html
            published_at
          }
        }
      }
  }
`;

Here is my gatsby node file: 这是我的gatsby节点文件:

exports.createPages = ({ graphql, boundActionCreators}) => {
    const {createPage} = boundActionCreators

    return new Promise((resolve, reject) => {
        const blogPostTemplate = path.resolve(`src/templates/blog-post.js`)

        resolve(
            graphql(
                `
                {
                    allGhostPost(sort: { order: DESC, fields: [published_at] }) {
                      edges {
                        node {
                          id
                          slug
                          title
                          html
                          published_at      
                        }
                      }
                    }
                  }
                `
            )
            .then(result => {
                result.data.allGhostPost.edges.forEach(edge => {
                    createPage({
                        path: edge.node.slug,
                        component: blogPostTemplate,
                        context: {
                            slug: edge.node.slug
                        }
                    })
                })
                return;
            })
        )
    })
}

I figured out my problem and it was a problem with my Queries. 我发现了问题,这是我的查询存在问题。 For anyone working with the Ghost API. 适用于使用Ghost API的任何人。 This is the answer you will need: 这是您需要的答案:

query BlogPostQuery($slug: String!) {
  allGhostPost(filter: {slug: {eq: $slug}}) {
    edges {
      node {
        id
        slug
        title
        html
        published_at
      }
    }
  }
}

Let me explain my answer. 让我解释一下我的答案。

The issue was that my GraphQL query was not working because the $slug field was not being used within the query. 问题是我的GraphQL查询无法正常工作,因为查询中未使用$slug字段。 It was just being passed in. That being said, I had to learn a bit of GraphQL to get to my final conclusion. 它只是被传递过来。也就是说,我必须学习一些GraphQL才能得出我的最终结论。

Using the GraphiQL I was able to find that the allGhostPost had a filter method. 使用GraphiQL我发现allGhostPost具有filter方法。 Using that I was able to pull in the right result. 使用它,我可以得出正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM