Skip to content

Instantly share code, notes, and snippets.

@henrikwirth
Created August 7, 2019 16:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save henrikwirth/4ba900b7f89b9e28ec81497466b12710 to your computer and use it in GitHub Desktop.
Save henrikwirth/4ba900b7f89b9e28ec81497466b12710 to your computer and use it in GitHub Desktop.
Extending WP GraphQL images with gatsby-image functionality
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
// Note that WPGraphQL_MediaItem depends on your gatsby-source-graphql typeName which is set in the config
exports.createResolvers = ({
actions,
cache,
createNodeId,
createResolvers,
store,
reporter,
}) => {
const { createNode } = actions
createResolvers({
WPGraphQL_MediaItem: {
imageFile: {
type: `File`,
resolve(source, args, context, info) {
return createRemoteFileNode({
url: source.sourceUrl,
store,
cache,
createNode,
createNodeId,
reporter,
})
},
},
},
})
}
// Note: Make sure to query sourceUrl, otherwise gatsby-image doesn't work
export const pageQuery = graphql`
query($id: ID!) {
WPGraphQL {
pageBy(id: $id) {
featuredImage {
sourceUrl
imageFile {
childImageSharp {
fixed {
...GatsbyImageSharpFixed
}
}
}
}
}
}
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment