'Field "cover" must not have a selection since type "String" has no subfields. GraphQL
I am getting error like this with GraphQL:
There was an error in your GraphQL query:
Field "cover" must not have a selection since type "String" has no subfields.
This can happen if you e.g. accidentally added { } to the field "cover". If you didn't expect "cover" to be of type "String" make sure that your input source and/or plugin is correct. However, if you expect "value" to exist, the field might be accessible in another subfield. Please try your query in GraphiQL and use the GraphiQL explorer to see which fields you can query and what shape they have.
It is recommended to explicitly type your GraphQL schema if you want to use optional fields. This way you don't have to add the mentioned "dummy content". Visit our docs to learn how you can define the schema for "undefined": https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization#creating-type-definitions
File: src/components/sections/featured.js:317:21
And here is my code in featured.js :
line 317 is from cover
const Featured = () => {
const data = useStaticQuery(graphql`
{
featured: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/featured/" } }
sort: { fields: [frontmatter___date], order: ASC }
) {
edges {
node {
frontmatter {
title
cover {
childImageSharp {
gatsbyImageData(width: 790, placeholder: BLURRED, formats: [AUTO, WEBP, AVIF])
}
}
tech
github
external
cta
}
html
}
}
}
}
`);
And here is my code in gatsby-node.js:
const path = require('path');
const _ = require('lodash');
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const postTemplate = path.resolve(`src/templates/post.js`);
const tagTemplate = path.resolve('src/templates/tag.js');
const result = await graphql(`
{
postsRemark: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/posts/" } }
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
frontmatter {
slug
}
}
}
}
tagsGroup: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create post detail pages
const posts = result.data.postsRemark.edges;
posts.forEach(({ node }) => {
createPage({
path: node.frontmatter.slug,
component: postTemplate,
context: {},
});
});
// Extract tag data from query
const tags = result.data.tagsGroup.group;
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/pensieve/tags/${_.kebabCase(tag.fieldValue)}/`,
component: tagTemplate,
context: {
tag: tag.fieldValue,
},
});
});
};
// https://www.gatsbyjs.org/docs/node-apis/#onCreateWebpackConfig
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
// https://www.gatsbyjs.org/docs/debugging-html-builds/#fixing-third-party-modules
if (stage === 'build-html' || stage === 'develop-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /scrollreveal/,
use: loaders.null(),
},
{
test: /animejs/,
use: loaders.null(),
},
{
test: /miniraf/,
use: loaders.null(),
},
],
},
});
}
actions.setWebpackConfig({
resolve: {
alias: {
'@components': path.resolve(__dirname, 'src/components'),
'@config': path.resolve(__dirname, 'src/config'),
'@fonts': path.resolve(__dirname, 'src/fonts'),
'@hooks': path.resolve(__dirname, 'src/hooks'),
'@images': path.resolve(__dirname, 'src/images'),
'@pages': path.resolve(__dirname, 'src/pages'),
'@styles': path.resolve(__dirname, 'src/styles'),
'@utils': path.resolve(__dirname, 'src/utils'),
},
},
});
};
How can I fix this error ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
