简体   繁体   English

在 Gatsby 中允许可选的 GraphQL 数据

[英]Allow optional GraphQL data in Gatsby

I'm trying to build a Type in my gatsby-node.js file that supports an optional value.我正在尝试在我的gatsby-node.js文件中构建一个支持可选值的类型。 Which I think is done with [String!]!我认为这是用[String!]! . .

How can I load the new Type that I've created inside gatsby-node.js on home.js ?如何在gatsby-node.jshome.js我在gatsby-node.js创建的新类型?

gatsby-node.js: gatsby-node.js:

const path = require('path');
exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions;
    const typeDefs = `
        type markdownRemark implements Node {
            frontmatter: Features
        }
        type Features {
            title: [String!]!
            description: [String!]!
        }
    `;
    createTypes(typeDefs);
};

pages/home/home.js:页面/主页/home.js:

export const query = graphql`
    query HomeQuery($path: String!) {
        markdownRemark(frontmatter: { path: { eq: $path } }) {
            html
            frontmatter {
                features {
                    title
                    description
                }
            }
        }
    }
`;

home.md:主页.md:

---
path: "/"
features:
    - title: Barns
      description: Praesent commodo cursus magna vel scelerisque nisl consectetur et. Nullam id dolor id nibh ultricies vehicula ut id elit.
    - title: Private Events
      description: Praesent commodo cursus magna vel scelerisque nisl consectetur et. Nullam id dolor id nibh ultricies vehicula ut id elit.
    - title: Food and Drinks
      description: Praesent commodo cursus magna vel scelerisque nisl consectetur et. Nullam id dolor id nibh ultricies vehicula ut id elit.
    - title: Spa
      description: Praesent commodo cursus magna vel scelerisque nisl consectetur et. Nullam id dolor id nibh ultricies vehicula ut id elit.
---

This needs to work so that if the features array inside home.md 's front matter is empty, then GraphQL doesn't throw an error.这需要起作用,以便如果home.md的前端内容中的features数组为空,则 GraphQL 不会抛出错误。

Please don't tell me to always include at least one value in the array, because this isn't practical, my solution needs to support no values in my array.请不要告诉我总是在数组中至少包含一个值,因为这不切实际,我的解决方案需要在我的数组中不支持任何值。

I've spent two hours going through documentation/issues in circles trying to find a working solution, please can someone save me!我花了两个小时在圈子里浏览文档/问题试图找到一个可行的解决方案,请有人救我!

From the GraphQL docs :来自GraphQL 文档

  • String! means that the field is non-nullable , meaning that the GraphQL service promises to always give you a value when you query this field.表示该字段不可为空,这意味着 GraphQL 服务承诺在您查询该字段时始终为您提供一个值。 In the type language, we'll represent those with an exclamation mark .在类型语言中,我们将用感叹号表示那些
  • [Episode!]! represents an array of Episode objects.表示一个Episode对象数组。 Since it is also non-nullable, you can always expect an array (with zero or more items) when you query the field.由于它也是不可为空的,因此当您查询该字段时,您总是可以期待一个数组(具有零个或多个项目)。 And since Episode!Episode! is also non-nullable, you can always expect every item of the array to be an Episode object.也是不可为空的,您总是可以期望数组的每个项目都是一个Episode对象。

The exclamation point !感叹号! in GraphQL means non-nullable , so [String!]!在 GraphQL 中意味着不可为空,所以[String!]! means that there is a non-null array of non-null strings.意味着有一个非空字符串的非空数组。

If you want a field to be optional, just leave it as it is without exclamation points !如果您希望某个字段是可选的,请保持原样,不带感叹号! . . For example [String] means that the array can be null, or any of the string values inside it can be null.例如, [String]表示该数组可以为空,或者其中的任何字符串值都可以为空。

I'm also not sure that you want to be using an array in the first place, since title and description of a feature should surely just be a single string?我也不确定您是否想首先使用数组,因为功能的titledescription肯定应该只是一个字符串?

As per the Gatsby docs , I think what you're looking for is this:根据Gatsby docs ,我认为您要寻找的是:

const typeDefs = `
    type markdownRemark implements Node {
        // Use custom frontmatter type
        frontmatter: Frontmatter
    }
    // Define custom frontmatter type
    type FrontMatter {
      // Nullable array of Feature elements
      features: [Feature]
    }
    // Feature has nullable fields title and description
    type Feature {
        title: String
        description: String
    }
`;

This means that frontmatter has a field called features that can be null (optional) and if it does exist, is an array of Feature objects.这意味着 frontmatter 有一个称为features的字段,该字段可以为 null(可选),并且如果它确实存在,则是一个Feature对象数组。 It can be empty, but if any Feature objects exist, each Feature has a nullable (optional) title and description field.它可以为空,但如果存在任何Feature对象,则每个Feature都有一个可为空(可选)的titledescription字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM