GraphQL错误字段类型为“文件”的“图像”必须具有子字段的选择。您是说“图像{…}”吗?

问题描述 投票:2回答:2

我正在使用gatsby和Netlify CMS构建站点。我使用了Gatsby Site Starter。

我不断收到类型为“文件”的“ GraphQL错误字段“图像”,必须选择子字段。您是说“图像{...}”吗?”尝试部署到Netlify时出错。一切都可以在localhost上完美运行,但是映像似乎出了点问题。我查看了Netlify CMS页面上提供的示例,发现有人具有完全相同的设置,一个列表小部件(充当图库),其中带有图像和说明,here

config.yml

backend:
  name: git-gateway
  repo: grantballmer/gatsby-starter-netlify-cms
  branch: master

media_folder: static/img
public_folder: /img

collections:

  - name: "gallery"
    label: "Gallery"
    folder: "src/pages/services"
    create: true
    fields: 
      - { label: "Template Key", name: "templateKey", widget: "hidden", default: "gallery" }
      - {label: "Title", name: "title", widget: "string"}
      - label: "Grid"
        name: "grid"
        widget: "list"
        fields: 
          - {label: "Image", name: "image", widget: "image"}
          - {label: "Band Name", name: "band", widget: "string"}`

gallery.js(模板文件)

import React from 'react';
import { graphql } from 'gatsby';
import Layout from "../components/Layout";
import PhotoGrid from "../components/services/PhotoGrid";

const GalleryTemplate = ({ data }) => {
  const { markdownRemark: gallery } = data;
  const images = gallery.frontmatter.grid;

  return (
    <Layout>
      <PhotoGrid images={images} />
    </Layout>
  );
};

export default GalleryTemplate;

export const galleryQuery = graphql `
  query Gallery($id: String!) {
    markdownRemark(id: { eq: $id }) {
      html
      frontmatter {
        title
        grid {
          image 
          band
        }
      }
    }
  }
`;

/ services / photography.md

---
templateKey: 'gallery'
title: photography
grid:
  - band: chris-cordle
    image: /img/chris-cordle-lg.jpg
  - band: 'chirp '
    image: /img/chirp-lg-1-.jpg
---
graphql gatsby netlify netlify-cms
2个回答
1
投票

我还没有使用过Netlify CMS,但是您的图片可能是带有子字段的集合(例如:image { source, id .. },在这种情况下,您应该像这样重写查询:

export const galleryQuery = graphql `
  query Gallery($id: String!) {
    markdownRemark(id: { eq: $id }) {
      html
      frontmatter {
        title
        grid {
          image { 
             id
             source
             ..
          }
          band
        }
      }
    }
  }
`;

0
投票

尝试使用插件gastby-plugin-sharp添加一些东西。

与此相关的内容:

export const galleryQuery = graphql`
  query Gallery($id: String!) {
    markdownRemark(id: { eq: $id }) {
      html
      frontmatter {
        title
        grid {
          image {
            childImageSharp {
              fluid(maxWidth: 2048, quality: 100) {
                ...GatsbyImageSharpFluid
              }
            }
          }
          band
        }
      }
    }
  }
`
© www.soinside.com 2019 - 2024. All rights reserved.