11

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

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

配置.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
        }
      }
    }
  }
`;

/服务/摄影.md

---
templateKey: 'gallery'
title: photography
grid:
  - band: chris-cordle
    image: /img/chris-cordle-lg.jpg
  - band: 'chirp '
    image: /img/chirp-lg-1-.jpg
---
4

2 回答 2

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
        }
      }
    }
  }
`;
于 2019-02-15T04:33:06.310 回答
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
        }
      }
    }
  }
`
于 2019-12-29T02:55:44.180 回答