gatsby-source-s3
gatsby-source-s3 copied to clipboard
Handle empty bucket
Great plugin, thanks for creating it!
Sometimes I don't want to process any images - for example in my CI pipeline. An easy way to accomplish that is to do something like in gatsby-config.js:
{
buckets: process.env.GATSBY_SKIP_IMAGE_PROCESSING
? []
: ["my-s3-bucket"],
...
}
and then do GATSBY_SKIP_IMAGE_PROCESSING=1 gatsby build to pretend there are no images.
This is much easier than trying to make your GraphQL queries adapt to the environment variable, because Gatsby graphQL queries are pretty static.
However, when I do that, I get a Gatsby error:
ERROR #85923 GRAPHQL
There was an error in your GraphQL query:
Cannot query field "allS3Object" on type "Query".
If you don't expect "allS3Object" to exist on the type "Query" it is most likely a typo.
However, if you expect "allS3Object" to exist there are a couple of solutions to common problems:
- If you added a new data source and/or changed something inside gatsby-node.js/gatsby-config.js, please try a restart of your development server
- 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
- You want to optionally use your field "allS3Object" and right now it is not used anywhere. Therefore Gatsby can't infer the type and add it to the GraphQL schema. A quick fix is to add a least one entry with that field (
"dummy content")
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 "Query":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions
This makes sense, but it would be nice if it Just Worked, or alternatively, if there was documentation in this plugin's README explaining how to add the missing type information.
For now, I'm working around this by creating a dedicated alternative S3 bucket with a single dummy file inside, and doing this in gatsby-config.js:
{
buckets: process.env.GATSBY_SKIP_IMAGE_PROCESSING
? ["my-dummy-bucket"],
: ["my-s3-bucket"],
...
}
Hey @patspam, thanks for this issue and sorry for my late reply!
I never thought about this, but it makes a lot of sense, particularly when working with a large bucket that takes a while to process.
As the error explains, Gatsby expect to find a type of allS3Object in your GraphQL queries, but the query will return null if we "skip" the sourcing of images with the env var trick you mentioned.
I'm guessing that a solution to this issue is to look into making this field optional in development (it should probably still throw an error in production builds):
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 "Query":
https://www.gatsbyjs.org/docs/schema-customization/#creating-type-definitions
I haven't looked into this yet. If it can be done in the plugin directly, let's do it, and otherwise we can add instructions in the README, as you suggested.
Do you want to open a PR for this?
Thanks for your interest in this project. This plugin is moving into the Gatsby User Collective and this repo will be archived. Please open an issue in that repository, submit a PR if you'd like to see this implemented, or Join us on Discord if you have questions!