Brandon Powell
Brandon Powell

Reputation: 83

Issue with GraphQL Site and siteMetadate query

I'm debugging some code that cannot figure out so I have been stuck on for past 2 weeks.

My code is telling me that I have:

  1. Cannot query field "siteMetadate" on type "Site". Did you mean "siteMetadata"?

I have want on GraphiQL Database to redo but still don't see social or twitter under the site explorer. What tips or suggests might you can give that can help move forward to learning on to debugged the issue.

There was an error in your GraphQL query:

Cannot query field "siteMetadate" on type "Site".

If you don't expect "siteMetadate" to exist on the type "Site" it is most likely a typo. However, if you expect "siteMetadate" to exist there are a couple of solutions to common problems:

import { graphql, useStaticQuery } from "gatsby"
import * as React from "react"
import PropTypes from "prop-types"
import { Helmet } from "react-helmet"

const Seo = ({ description, lang, meta, title }) => {

  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadate {
            description
            title
            social {
              twitter
            }
          }
        }
      }
    `
  )
  // const { site } = useStaticQuery(
  //   graphql`
  //     query {
  //       site {
  //         siteMetadata {
  //           title
  //           description
  //           social {
  //             twitter
  //           }
  //         }
  //       }
  //     }
  //   `
  // )

  

  const metaDescription = description || site.siteMetadata.description
  const defaultTitle = site.siteMetadata.title

  return (
    <Helmet
      htmlAttributes={{
        lang,
      }}
      title={title}
      titleTemplate={defaultTitle ? `%s | ${defaultTitle}` : null}
      meta={[
        {
          name: `description`,
          content: metaDescription,
        },
        {
          property: `og:title`,
          content: title,
        },
        {
          property: `og:description`,
          content: metaDescription,
        },
        {
          property: `og:type`,
          content: `website`,
        },
        {
          name: `twitter:card`,
          content: `summary`,
        },
        {
          name: `twitter:creator`,
          content: site.siteMetadata.social.twitter || ``,
        },
        {
          name: `twitter:title`,
          content: title,
        },
        {import { graphql, useStaticQuery } from "gatsby"
import * as React from "react"
import PropTypes from "prop-types"
import { Helmet } from "react-helmet"

const Seo = ({ description, lang, meta, title }) => {

  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadate {
            description
            title
            social {
              twitter
            }
          }
        }
      }
    `
  )
  // const { site } = useStaticQuery(
  //   graphql`
  //     query {
  //       site {
  //         siteMetadata {
  //           title
  //           description
  //           social {
  //             twitter
  //           }
  //         }
  //       }
  //     }
  //   `
  // )

  

  const metaDescription = description || site.siteMetadata.description
  const defaultTitle = site.siteMetadata.title

  return (
    <Helmet
      htmlAttributes={{
        lang,
      }}
      title={title}
      titleTemplate={defaultTitle ? `%s | ${defaultTitle}` : null}
      meta={[
        {
          name: `description`,
          content: metaDescription,
        },
        {
          property: `og:title`,
          content: title,
        },
        {
          property: `og:description`,
          content: metaDescription,
        },
        {
          property: `og:type`,
          content: `website`,
        },
        {
          name: `twitter:card`,
          content: `summary`,
        },
        {
          name: `twitter:creator`,
          content: site.siteMetadata.social.twitter || ``,
        },
        {
          name: `twitter:title`,
          content: title,
        },
        {
          name: `twitter:description`,
          content: metaDescription,
        },
      ].concat(meta)}
    />
  )
}

Seo.defaultProps = {
  lang: `en`,
  meta: [],
  description: ``,
}

Seo.propTypes = {
  description: PropTypes.string,
  lang: PropTypes.string,
  meta: PropTypes.arrayOf(PropTypes.object),
  title: PropTypes.string.isRequired,
}

export default Seo
          name: `twitter:description`,
          content: metaDescription,
        },
      ].concat(meta)}
    />
  )
}

Seo.defaultProps = {
  lang: `en`,
  meta: [],
  description: ``,
}

Seo.propTypes = {
  description: PropTypes.string,
  lang: PropTypes.string,
  meta: PropTypes.arrayOf(PropTypes.object),
  title: PropTypes.string.isRequired,
}

export default Seo

Upvotes: 1

Views: 567

Answers (1)

Ferran Buireu
Ferran Buireu

Reputation: 29315

siteMetadata (not siteMetadate, check the spelling) is an object defined in the gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: `Gatsby`,
  },
  plugins: [
    `gatsby-transform-plugin`,
    {
      resolve: `gatsby-plugin-name`,
      options: {
        optionA: true,
        optionB: `Another option`,
      },
    },
  ],
}

In this case, siteMetadata will expose a title field so you can fetch it using the useStaticQuery hook.

I recommend you check the spelling of siteMetadata against siteMetadate, all names must match the original configuration:

module.exports = {
  siteMetadata: {
    title: `Gatsby`,
    siteUrl: `https://www.gatsbyjs.com`,
    description: `Blazing fast modern site generator for React`,
    social: {
      twitter: 'some twitter'
    }
  },
} 

Extend it to add your Twitter and other social data if needed.

Upvotes: 1

Related Questions