如何解决 GraphQL 错误,字段“x”不能有选择,因为类型“String”没有子字段

Cha*_*son 5 graphql gatsby

我正在使用 Gatsby 和 GraphQL,而且我是 GraphQL 的新手。

我有以下架构定义:

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = `
    type MarkdownRemark implements Node {
      frontmatter: Frontmatter
    }
    type Frontmatter {
      title: String!
      products: [Product]
    }
    type Product @dontInfer {
      name: String!
      price(price: Int = 1): Float
      description: String
      images: [ProductImage]
    }
    type ProductImage {
      url: String
    }
  `;
  createTypes(typeDefs);
};
Run Code Online (Sandbox Code Playgroud)

然后在我的页面上我使用以下查询:

query {
  markdownRemark(fileRelativePath: { eq: "/content/pages/products.md" }) {
    ...TinaRemark
    frontmatter {
      title
      products {
        name
        price
        description
        images {
          url {
            childImageSharp {
              fluid(maxWidth: 1920) {
                ...GatsbyImageSharpFluid_withWebp
              }
            }
          }
        }
      }
    }
    html
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:

Field "url" must not have a selection since type "String" has no subfields.
Run Code Online (Sandbox Code Playgroud)

有人对如何解决此错误有任何建议吗?

另外,什么是childImageSharp?我想知道用什么术语来定义它。它是 GraphQL“选择器”还是“函数”?

小智 3

它应该是

query {
  markdownRemark(fileRelativePath: { eq: "/content/pages/products.md" }) {
    ...TinaRemark
    frontmatter {
      title
      products {
        name
        price
        description
        images {
          url 
        }
      }
    }
    html
  }
}
Run Code Online (Sandbox Code Playgroud)

因为你的定义是

 type ProductImage {
      url: String
    }
Run Code Online (Sandbox Code Playgroud)

url显然没有子字段。