Search code examples
reactjsgraphqlgatsbyheadless-cmswp-graphql

combining page queries in GatsbyJS/GraphQL


How can I combine two page queries in GraphQL? I tried simply just combining the data I need into a single query but it keeps giving me an error "TypeError: Cannot read property 'pages' of undefined". My thought was that either my query is simply just not done right or not fetching the data correct in my box a div.

const IndexPage = (props) => {
  const { indexImage, wpgraphql } = props.data

  return (
    <Layout>
      <SEO title="Home" />
      <BackgroundImage
        className="masthead"
        fadeIn
        fluid={indexImage.childImageSharp.fluid}
      >
        <div className="black-overlay">
          <div className="content-box">
            <h1>Her skriver jeg en overskrift</h1>
            <h2>This is my sub head</h2>
          </div>
        </div>
      </BackgroundImage>
      <div
        style={{
          margin: `0 auto`,
          maxWidth: 1200,
          padding: `0 1.0875rem 1.45rem`,
        }}
      >
        <div class="wrapper">
          <div class="box a">
            {wpgraphql.pages.nodes[0].undersideACFgraphql.mainText}
          </div>
          <div class="box b">Her skriver jeg tekst 2 :D</div>
        </div>
      </div>
      <Link to="/page-2/">Gå til en anden side</Link>
    </Layout>
  )
}

export default IndexPage

export const query = graphql`
  query {
    indexImage: file(relativePath: { eq: "bolig-partner-ydelser.jpg" }) {
      childImageSharp {
        fluid(maxWidth: 1800) {
          ...GatsbyImageSharpFluid
        }
      }
    }
    wpgraphql {
        pages {
          edges {
            node {
              undersideACFgraphql {
                mainText
              }
            }
          }
        }
      }
  }
`;

When running my wpgraphql query in GraphiQL it works just fine. Here is the wpgraphql query:

query MyQuery {
  wpgraphql {
    pages(where: {id: 91}) {
      nodes {
        undersideACFgraphql {
          mainText
        }
      }
    }
  }
}

Returns data:

{
  "data": {
    "wpgraphql": {
      "pages": {
        "nodes": [
          {
            "undersideACFgraphql": {
              "mainText": "Her kan jeg skrive min tekst til mit lille grid :D:D"
            }
          }
        ]
      }
    }
  }
}

Solution

  • The reason why you're getting the error is because you're accessing wpgraphql incorrectly - you should be using props.data.wpgraphql.pages but you're doing props.wpgraphql.pages.

    Also, your GraphQL query for wpgraphl returns an object where pages.edges is an array, so you have to grab the first object in that array, if that's what you want, and then access it's node.undersideACFgraphql.mainText property.

    const IndexPage = (props) => {
      const { indexImage, wpgraphql } = props.data
    
      return (
        <Layout>
          <SEO title="Home" />
          <BackgroundImage
            className="masthead"
            fadeIn
            fluid={indexImage.childImageSharp.fluid}
          >
            <div className="black-overlay">
              <div className="content-box">
                <h1>Her skriver jeg en overskrift</h1>
                <h2>This is my sub head</h2>
              </div>
            </div>
          </BackgroundImage>
          <div
            style={{
              margin: `0 auto`,
              maxWidth: 1200,
              padding: `0 1.0875rem 1.45rem`,
            }}
          >
            <div class="wrapper">
              <div class="box a">
                {wpgraphql.pages.edges[0].node.undersideACFgraphql.mainText}
              </div>
              <div class="box b">Her skriver jeg tekst 2 :D</div>
            </div>
          </div>
          <Link to="/page-2/">Gå til en anden side</Link>
        </Layout>
      )
    }