Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep DRY when fetching many images using GraphQL in Gatsby

Tags:

graphql

gatsby

Let's say I want to include 50 images in one article on a blog made on Gatsby. Images are fetched using GraphQL. I end up writing very repetitive queries for each of the 50 images, like so:

export const query = graphql`
  query ArticleImageQuery {
    coverImage: imageSharp(id: {regex: "/cover-image/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    artisan1: imageSharp(id: {regex: "/artisan1/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    artisan2: imageSharp(id: {regex: "/artisan2/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    artisan3: imageSharp(id: {regex: "/artisan3/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    johannystrom1: imageSharp(id: {regex: "/johan-nystrom1/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    johannystrom2: imageSharp(id: {regex: "/johan-nystrom2/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    johannystrom3: imageSharp(id: {regex: "/johan-nystrom3/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    johannystrom4: imageSharp(id: {regex: "/johan-nystrom4/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    fratello1: imageSharp(id: {regex: "/fratello/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    booksAntiques1: imageSharp(id: {regex: "/books-antiques1/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    booksAntiques2: imageSharp(id: {regex: "/books-antiques2/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    pastor1: imageSharp(id: {regex: "/pastor1/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    pastor2: imageSharp(id: {regex: "/pastor2/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    pastor3: imageSharp(id: {regex: "/pastor3/" }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
    libertyOrDeath1: imageSharp(id: {regex: "/liberty-death1/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    libertyOrDeath2: imageSharp(id: {regex: "/liberty-death2/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    kaisla1: imageSharp(id: {regex: "/kaisla1/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    blockDylan1: imageSharp(id: {regex: "/block-dylan1/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    blockDylan2: imageSharp(id: {regex: "/block-dylan2/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    blockDylan3: imageSharp(id: {regex: "/block-dylan3/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    blockDylan4: imageSharp(id: {regex: "/block-dylan4/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },
    blockDylan5: imageSharp(id: {regex: "/block-dylan5/" }) {
      sizes(maxWidth: 700, maxHeight: 525, cropFocus: CENTER) {
        ...GatsbyImageSharpSizes
      }
    },

    /* The list goes on, you get the idea... */
  }
`;

This goes heavily against the DRY (Don't Repeat Yourself) principle. I've tried to fiddle with fragments, but I being a noob GraphQL user I just can't figure out how can I reuse this repetitive code and write it just once, instead of 50 times?

Is there anything I can do to improve this?

Ideally, I'd love to have a code where I could write the repetitive part once and then reuse it for each image that I'm fetching. For example:

/* NOTE: FICTIONAL PSEUDOCODE */

const imageQuery = graphql`
  query ImageQuery($path: String!) {
    imageSharp(id: {regex: $path }) {
      sizes(maxWidth: 700, maxHeight: 525) {
        ...GatsbyImageSharpSizes
      }
    },
  }
`;

export const query = graphql`
  query ArticleImageQuery {
    coverImage: ImageQuery("/cover-image/"),
    artisan1: ImageQuery("/artisan1/"),
    artisan2: ImageQuery("/artisan2/"),
    artisan3: ImageQuery("/artisan3/"),
    johannystrom1: ImageQuery("/johan-nystrom1/"),'
    /* ... AND SO ON */
  }
`;
like image 889
Alex Avatar asked Oct 29 '22 16:10

Alex


1 Answers

GraphQL Fragments do not have parameters. See this github issue for more info.

When I have a problem similar to yours I select all the images that I need with one query and then process them in the view. For example with such query :

export const query = graphql`
{
  allFile(filter: {absolutePath: {regex: "/img/o-meni/im/"}}) {
    edges {
      node {
        id
        name
        publicURL
        childImageSharp {
          sizes(maxWidth: 700, maxHeight: 525) {
              ...GatsbyImageSharpSizes
          }
        }
      }
    }
  }
}
`

And then you can filter for the specific image like artisan1 or whichever in the React component. To display them all you can use code like this :

{
  this.props.data.allFile.edges.map(({ node }) =>
    <a href={node.publicURL} key={node.id}>
      <Img sizes={node.childImageSharp.sizes} alt={node.name} title={node.name} />
    </a>
  )
}
like image 118
Saša Šijak Avatar answered Nov 08 '22 08:11

Saša Šijak