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

时间:2019-02-14 23:06:21

标签: graphql gatsby netlify netlify-cms

我正在使用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
---

2 个答案:

答案 0 :(得分: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
        }
      }
    }
  }
`;

答案 1 :(得分: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
        }
      }
    }
  }
`