plugins
                                
                                 plugins copied to clipboard
                                
                                    plugins copied to clipboard
                            
                            
                            
                        feat(gatsby-source-strapi): Generated Types
The Readme states the following limitation:
At the moment, fields that do not have at least one populated instance will not be created in the GraphQL schema. This can break your site when field values get removed. You may workaround with an extra content entry with all fields filled out.
The fact that a site can break (does not build successfully) in those situations has turned out to be very annoying to me, even if it can be avoided by the mentioned workaround. And it makes optional fields on single types practically infeasible.
I think it should be feasible and would greatly improve the quality of the plugin. I didn't find any feature request or discussion on this topic, so I am raising it here.
Describe the solution you'd like
The plugin could explicitly generate data types based on the strapi schemas (which are already fetched here I think) before generating the nodes, e.g. using the createTypes hook (see Gatsby docs).
Describe alternatives you've considered
The alternative suggested in the Readme (create "an extra content entry with all fields filled out") feels like a hack. It doesn't work for single-types. It would feel more intuitive, if an optional field in Strapi, could be treated as an optional field in Gatsby, too.
We'd love to see this, and agree it can be very annoying. A PR is welcome.
I also did not like to add extra content, and found a workable solution @nimame. The first step is filling in all the fields once. Then, using the code below, generate a typeDefs.txt file. Copy and paste everything, or just problematic ones, from the typeDefs.txt file in the const typeDefs.
import * as fs from 'fs';
exports.sourceNodes = ({ actions }) => {
	if (fs.existsSync('./typeDefs.txt')) {
		fs.rmSync('./typeDefs.txt');
	}
	actions.printTypeDefinitions({ path: './typeDefs.txt' });
	const { createTypes } = actions;
	const typeDefs = ``;
	createTypes(typeDefs);
}
Works with gatsby 5.7.0
Thanks for the suggestion @fgroenendijk. Would be cool if something like that could be integrated into the plugin and handled behind the scenes automatically. I had a look at the source of the plugin but I don't think I'm smart enough to do a PR myself :)
Myself, I actually manually create all the types via createSchemaCustomization. The "just create extra content" is extremely hacky.
I would also love to see the schema auto generated fully. Im pretty sure its possible from Strapi. If anyone is up for the job to create something like this I would be happy to get it merged in
I've used a lot of source plugins that use graphql. This is the first one that does this. I don't know how it ever got released in this state...
Honestly the strapi plugin is so bad for so many different reasons it would probably be easier to fork the directus plugin and adapt the API.
Any and all contributions would be welcome @robclancy. Strapi has passed the buck on maintenance and donated the plugin to us so folks could make improvements as needed. If you want to take charge on a major update I'm happy to support that effort.