Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL query in component directory #3991

Closed
exrishu opened this issue Feb 12, 2018 · 4 comments
Closed

GraphQL query in component directory #3991

exrishu opened this issue Feb 12, 2018 · 4 comments
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation

Comments

@exrishu
Copy link

exrishu commented Feb 12, 2018

Hi All,
I have made a component inside src/components and used graphQl for rendering the content.
But when I am calling component in src/pages it is showing the error.

// in src/components/service.js

import React from 'react'
import Link from 'gatsby-link'

class Service extends React.PureComponent{
   render(){
      const data = this.props
       return(
        <section className="services">
        <div class="container">
          <div class="row">
              <div class="col-lg-12 text-center">
                  <h2 class="section-heading">{data.allServiceYaml.edges[0].node.heading}</h2>
                  <hr class="primary"/>
              </div>
          </div>
      </div>
      
      <div className="container">
       <div className="row">
        <div className="col-lg-3 col-md-6 text-center">
          <div className="service-box">
           <i className="fa fa-4x"></i>
              <h3></h3>
              <p className="text-muted"></p>
           </div>
          </div>
        </div>
      </div>
      
  </section>
     )

   }
}
export default Service
export const query = graphql`
query ServiceQuery{
    allServiceYaml{
      edges{
        node{
          heading
          list{
            title
            icon
            description
          }
        }
      }
    }
  }

`
// in src/pages/index.js  

import React from 'react'
import Link from 'gatsby-link'
import Banner from '../components/Banner'
import Service from '../components/Service'


const IndexPage = ({data}) => (
  <div>
    <Banner/>
    <Service data = {data} />
  </div>
)

export default IndexPage

IS there is anyway I can pass the data along with component

@jquense
Copy link
Contributor

jquense commented Feb 12, 2018

Hey there, queries aren't supported in components that aren't "top level" ones, you need to use fragment composition, we recently just improved the documentation and error around this, check out: #3786

@jquense jquense closed this as completed Feb 12, 2018
@jquense jquense added GraphQL type: documentation An issue or pull request for improving or updating Gatsby's documentation labels Feb 12, 2018
@jquense
Copy link
Contributor

jquense commented Feb 12, 2018

@exrishu
Copy link
Author

exrishu commented Feb 14, 2018

@jquense Thanks, Implemented and worked :)

@franva
Copy link

franva commented May 8, 2018

Hi @exrishu can you please share how did you do it?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

No branches or pull requests

3 participants