简体   繁体   English

使用 Gatsby/Contentful 从 Graphql 获取参考数据

[英]Getting reference data from Graphql using Gatsby/Contentful

I'm struggling to get the data from the reference part of my Graphql.我正在努力从我的 Graphql 的参考部分获取数据。 I got a Contentful content model that includes references as you can see in the query below.我得到了一个内容丰富的内容 model,其中包含参考,您可以在下面的查询中看到。

Now, I got a simple page like this, where I query data from Contentful:现在,我得到了一个像这样的简单页面,我在其中从 Contentful 查询数据:

import React from "react"
import { graphql } from "gatsby"

const PageTemplate = ({
    data: {
        islands: {
            id,
            title,
            featuredActivities: { id, title },
        },
    },
}) => {


    return (
                                    <article key={id}>
                                        <div>
                                            <h3>{title}</h3>
                                        </div>
                                    </article>

                                    <article key={featuredActivities.id}>
                                        <div>
                                            <h3>{featuredActivities.title}</h3>
                                        </div>
                                    </article>
    )
}

export const query = graphql`
query GetSinglePage($slug: String) {
  islands: contentfulPage (slug: {eq: $slug}) {
        id
        title
        featuredActivities {
            id
            title
            category
            price
            image {
              fluid {
                ...GatsbyContentfulFluid
              }
            }
          }
      }
  }
`
export default PageTemplate

And now I want to get the data for this part of the query:现在我想获取这部分查询的数据:

featuredActivities {
            id
            title
            category
            price
            image {
              fluid {
                ...GatsbyContentfulFluid
              }
            }
          }

I've tried this:我试过这个:

const PageTemplate = ({
    data: {
        islands: {
            featuredActivities: { id },
            featuredActivities: { title },
        },
    },

and added it like this into the:并将其添加到:

<article key={featuredActivities.id}>
  <h3>{featuredActivities.title}</h3>
</article>

But it's not working, does someone knows what I'm doing wrong?但它不起作用,有人知道我做错了什么吗?

Thanks in advance提前致谢

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

And now I want to get the data for this part of the query现在我想获取这部分查询的数据

You can't destructure all items of the array like this:您不能像这样解构数组的所有项目:

const PageTemplate = ({
    data: {
        islands: {
            featuredActivities: { id },
            featuredActivities: { title },
        },
    },

Why?为什么? Because it's an array of objects and the structure ( {} ) doesn't match the type of the nested item.因为它是一个对象数组,并且结构 ( {} ) 与嵌套项的类型不匹配。 And, in case it was, and you will need to enter each specific position.而且,如果是这样,您将需要输入每个特定的 position。

You may need to:您可能需要:

const PageTemplate = ({
    data: {
        islands: [{
            featuredActivities: { id },
            featuredActivities: { title },
        }],
    },

Notice the wrapping square brackets ( [] ).注意环绕方括号 ( [] )。

However, as I said, it's not ideal since you need to print each specific position of the array of an unknown length.但是,正如我所说,这并不理想,因为您需要打印未知长度数组的每个特定 position。 The best and optimal solution is to destructure until featuredActivities and loop through all elements:最好和最优的解决方案是解构直到featuredActivities并循环遍历所有元素:

const PageTemplate = ({
    data: {
        islands: {
            id,
            title,
            featuredActivities,
        },
    },
}) => {


    return <>
     {featuredActivities.map(({title, id})=>{
       return <div key={id}>{title}</div>
     })}
    </>
}

In that way, you can destructure inside the same loop in ({title, id}) , since you are getting for each specific position ( featuredActivity , the iterable variable), the title and the id (and so on for the rest of the needed fields).通过这种方式,您可以在({title, id})中的同一循环内进行解构,因为您正在获取每个特定的 position( featuredActivity ,可迭代变量)、 titleid (等等)的 rest必填字段)。

Assuming that Islands and featuredActivities are not array or objects and you are getting the correct data from GraphQL query, you just need to correct how you destructure and use the values.假设IslandsfeaturedActivities不是数组或对象,并且您从 GraphQL 查询中获得正确的数据,您只需要更正解构和使用这些值的方式。 Also since you cannot have clashing variable names, you will have to rename the variables from featuredActivities此外,由于您不能有冲突的变量名称,您将不得不从featuredActivities重命名变量

const PageTemplate = ({
    data: {
        islands: {
            id, title
            featuredActivities: { id: featuredId , title: featuredTitle },
        },
    },
    ...

    <article key={featuredId}>
      <h3>{featuredTitle}</h3>
    </article>

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

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