“文件”类型的 GraphQL 错误字段“图像”必须有一个子字段选择。您指的是“image { ... }”吗?

G. *_*ll. 10 graphql gatsby netlify netlify-cms

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

我不断收到“文件”类型的“GraphQL 错误字段”图像,必须选择子字段。您的意思是“图像 { ... }”吗?尝试部署到 Netlify 时出错。一切都在本地主机上完美运行,但图像似乎有些失败。我查看了 Netlify CMS 页面上提供的示例,发现有人具有完全相同的设置,一个列表小部件(充当画廊),里面有一个图像和描述,here

配置文件

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"}`
Run Code Online (Sandbox Code Playgroud)

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
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

/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
---
Run Code Online (Sandbox Code Playgroud)

Sco*_*irs 4

我没有使用过 Netlify CMS,但看起来您的 Image 可能是一个带有子字段的集合(例如:image { source, id .. },在这种情况下您应该重写您的查询,类似于:

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