'Issues with Gatsby & Strapi optional data
I have a model made in Strapi which contains a specific component which can be used to add social media links. Each link contains a text field and a link field. Everything works as expected, except when I leave it empty. If there are 0 links I get an error which is shown below.
This is how the component looks inside Strapi:
Gatsby GraphQL trying to access the links:
strapiWebsiteSetting {
footerSocialLinks {
text
link
}
footerOtherLinks {
text
link
}
}
The error I get when there is 0 links added:
Is there a way of making GraphQL work even if there is 0 links added. I've tried
adding the following code to gatsby-node.js
but that didn't work:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type STRAPI__COMPONENT_LINK_FOOTER_OTHER_LINK implements Node {
id: ID!
parent: Node
children: [Node!]!
internal: Internal!
text: String
link: String
strapi_id: Int
}
type STRAPI__COMPONENT_LINK_FOOTER_SOCIAL_MEDIA_LINK implements Node {
id: ID!
parent: Node
children: [Node!]!
internal: Internal!
text: String
link: String
strapi_id: Int
}
`;
createTypes(typeDefs);
};
Solution 1:[1]
So, I managed to get it working. Creating the custom types in the gatsby-node.js
probably did the trick, but I haven't really tested it, I just know it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dantcho |