Probably you found a solution by now.
If not, I was able to get remote images to work with gatsby-image. I tried gatsby-plugin-remote-images
as well but couldn't get it to work. What did the job for me was creating a resolver for my remote API (my remote cms source ingatsby-source-graphql
, I use Webiny CMS) like so:
Ingatsby-config.js
:
plugins: [
{
resolve: 'gatsby-source-filesystem',
options: {
name: 'images',
path: '${__dirname}/src/images',
},
},
{
resolve: 'gatsby-source-graphql',
options: {
typeName: "Webiny",
fieldName: "webinyHeadlessCms",
url: "https://XXXXXXXXXXX.cloudfront.net/cms/read/production",
headers: {
authorization: "XXXXXXXXXXXXXXXXXXXX"
},
refetchInterval: 60,
},
},
],
And my gatsby-node.js
:
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.createResolvers = ({actions,cache,createNodeId,createResolvers,store,reporter,}) => {
const { createNode } = actions
createResolvers({
Webiny_BlogPost: {
imageFile: {
type: `File`,
resolve(source, args, context, info) {
return createRemoteFileNode({
url: source.headerImage,
store,
cache,
createNode,
createNodeId,
reporter,
})
},
},
},
})
}
Now I can query images to use with gatsby-image now like so:
export const query = graphql`{
webinyHeadlessCms {
listBlogPosts {
data {
headerImage
imageFile {
childImageSharp {
fluid (quality: 100, maxWidth: 1920) {
...GatsbyImageSharpFluid_withWebp
}
}
}
}
}
}
}`
It is important to really understand the GraphQL Schema of your remote API. Have a look at it in GraphiQL. In my case the resolver creates a new node named imageFile
at Webiny_BlogPost
. In general this should be done at typeName_SchemaSubType
, where the part before the underscore is your typeName
defined in gatsby-source-graphql
and the part after the underscore is the schema subtype within that source, you want to create your image (File) node in.
When you query it, keep in mind, that you need to query the node with the remote image url as well (in my case headerImage
) to make it work.