Search code examples
gatsbyairtable

Airtable gatsby source plugin with gatsby image


So I'm using gatsby-source-airtable to pull images from my airtable.

In my gastby-config I've mapped my attachment column as filenode:

mapping: {'image':fileNode},

In GraphiQL gatsby image plugins seem to be working This query:

{
airtable(table: {
    eq: "table-1"
}, data: {
    slug: {
        eq: "test-1"
    }
}) {
    data {
        image {
            localFiles {
                childImageSharp {
                    fluid(maxWidth: 400) {
                        src
                    }
                }
            }
        }
    }
}

}

Provides this response:

{
"data": {
    "airtable": {
        "data": {
            "image": {
                "localFiles": [{
                    "childImageSharp": {
                        "fluid": {
                            "src": "/static/08baa0d1735184a4d0dd141d90f564d4-28158c2eb0b0b748efeabc0ec551c623-7eb65.jpg"
                        }
                    }
                }]
            }
        }
    }
}

}

and then going to that src generates the image and appears in browser.

However when i try use this with gatsby-image:

<Img fluid={post.data.image.localFiles.childImageSharp.fluid} />

    export const query = graphql query PostQuery {
    airtable(table: {
        eq: "table-1"
    }, data: {
        slug: {
            eq: "test-1"
        }
    }) {
        data {
            image {
                localFiles {
                    childImageSharp {
                        fluid(maxWidth: 400) { ...GatsbyImageSharpFluid
                        }
                    }
                }
            }
        }
    }
}

I get this error:

WebpackError: TypeError: Cannot read property 'fluid' of undefined

What am i doing something wrong? Any assistant would be appreciated


Solution

  • Querying the localFiles field on image will give you an array. Test the query in GraphiQL:

    Note that you'll have to replace the ...GatsbyImageSharpFluid fragment (not supported by GraphiQL) with another field like src

    airtable(table: {
      eq: "table-1"
    }, data: {
      slug: {
        eq: "test-1"
      }
    }) {
      data {
        image {
          localFiles {
            childImageSharp {
              fluid(maxWidth: 400) {
                src
              }
            }
          }
        }
      }
    }
    

    You should get something like:

    {
      "data": {
        "airtable": {
          "data": {
            "image": {
              "localFiles": [
                {
                  "childImageSharp": {
                    "fluid": {
                      "src": "/static/8a6a13a2664ef8330843b7855ad2c5e2/d278e/o.jpg"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    As you see, localFiles is an array, so in your component you should write:

    <Img fluid={post.data.image.localFiles[0].childImageSharp.fluid} />