Gatsby is a static web site framework that’s based on React.
We can use it to create static websites from external data sources and more.
In this article, we’ll look at how to create a site with Gatsby.
Filtering with GraphQL
We can filter items in our results with our GraphQL queries.
For example, we can write:
{
allSitePage(filter: {path: {eq: "/pokemon"}}) {
edges {
node {
id
path
}
}
}
}
Then we get the path
that equals to to /pokemon
.
eq
means equals.
GraphQL Query Aliases
We can add GraphQL query aliases.
For example, we can write:
{
fileCount: allFile {
totalCount
}
filePageInfo: allFile {
pageInfo {
currentPage
}
}
}
fileCount
and filePageInfo
are the aliases.
And the expression after the colon are the queries.
GraphQL Query Fragments
GraphQL query fragments are shareable chunks of a query that can be reused.
For example, we can create a fragment and make a query with it by writing:
src/pages/index.js
import React from "react"
import { graphql } from "gatsby"
export const query = graphql`
fragment SiteInformation on SiteSiteMetadata {
title
description
}
`
export const pageQuery = graphql`
query SiteQuery {
site {
siteMetadata {
...SiteInformation
}
}
}
`
export default function Home({ data }) {
return <div>{data.site.siteMetadata.title}</div>
}
We create the fragment with the query
query.
We create the fragment for the SiteSiteMetadata
type, which has the website’s metadata fields.
Then the pageQuery
uses the fragment we just created.
Querying Data Client-Side with fetch
We can query data on the client-side with the Fetch API.
For instance, we can write:
import React, { useState, useEffect } from "react"
const IndexPage = () => {
const [starsCount, setStarsCount] = useState(0)
useEffect(() => {
fetch(`https://api.github.com/repos/gatsbyjs/gatsby`)
.then(response => response.json())
.then(resultData => {
setStarsCount(resultData.stargazers_count)
})
}, [])
return (
<section>
<p>Gatsby start count: {starsCount}</p>
</section>
)
}
export default IndexPage
to get the number of Github stars for the Gatsby project and display it.
Images
We can add images into our Gatsby project.
For example, we can write:
src/pages/index.js
import React from "react"
import LaptopImg from "../assets/laptop.jpg"
const IndexPage = () => {
return (
<section>
<img src={LaptopImg} alt="laptop" />
</section>
)
}
export default IndexPage
We import the image from the /assets
folder and set the src
prop to the imported image.
Reference an Image from the static
Folder
Also, we can reference an image from the static
folder by its path.
For example, we can write:
import React from "react"
const IndexPage = () => {
return (
<section>
<img src={`laptop.jpg`} alt="laptop" />
</section>
)
}
export default IndexPage
given that we have the image in the static
folder.
Optimizing and Querying Local Images with gatsby-image
We can use the gatsby-image
to add an image.
To install the packages required packages, we run:
npm i gatsby-plugin-sharp gatsby-transformer-sharp
To do this, we write:
gatsby-config.js
const path = require('path');
module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: path.join(__dirname, `src`, `images`),
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
],
}
Then we can get the images on our page by writing:
src/pages/index.js
import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"
const IndexPage = () => {
const data = useStaticQuery(graphql`
query {
allFile(
filter: {
extension: { regex: "/(jpg)|(png)|(jpeg)/" }
relativeDirectory: { eq: "" }
}
) {
edges {
node {
base
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`)
return (
<div>
{data.allFile.edges.map(image => (
<Img
fluid={image.node.childImageSharp.fluid}
alt={image.node.base.split(".")[0]}
/>
))}
</div>
)
}
export default IndexPage
We get the files with the allFile
query.
We get the images from the src/images
folder as we specified in gatsby-config.js
.
Conclusion
We can filter items with GraphQL and we can get images and display them with a query with GraphQL.