Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql query for optional field for childImageSharp #23499

Closed
Defite opened this issue Apr 26, 2020 · 4 comments
Closed

Graphql query for optional field for childImageSharp #23499

Defite opened this issue Apr 26, 2020 · 4 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@Defite
Copy link
Contributor

Defite commented Apr 26, 2020

I have a blog with posts and previews for this posts. In my blog listing I query for this images like that:

edges {
				node {
					fields {
						slug
						langKey
					}
					frontmatter {
						date(formatString: "DD.MM.YY", locale: $langKey)
						excerpt
						title
						templateKey
						published
						path
						coverImg {
							childImageSharp {
								fluid(maxWidth: 700) {
									...GatsbyImageSharpFluid
								}
							}
						}
					}
				}
			}

And in my post I'm using fields like that:

title: Some title
...
coverImg: '/path/to/my/image.png'

All is working except case when none of my posts has coverImg field with valid path to the file in it. Gatsby throws an error:

There was an error in your GraphQL query:

Field "coverImg" must not have a selection since type "String" has no subfields.

This can happen if you e.g. accidentally added { } to the field "coverImg". If you didn't expect "coverImg" to be of type "String" make sure that your input source and/or plugin is correct.

I've related issues about Contentful, but didn't understand how to define properly type to make Graphql return null for coverImg field instead of this error.

Forgot to mention that I'm using gatsby theme as plugin, so this might be my issue: #15544 (comment)

I did some research and added this to my content site's gatsby-node.js:

exports.createSchemaCustomization = ({ actions }) => {
	const { createTypes } = actions;

	const typeDefs = [
	       `type MarkdownRemark implements Node {
			frontmatter: Frontmatter
		}`,
		`type Frontmatter @infer {
		         coverImg: [File!]! @fileByRelativePath,
		  }`,
	];

	createTypes(typeDefs);
};

And now Gatsby throws an error:

There was an error in your GraphQL query:

Cannot return null for non-nullable field Frontmatter.coverImg.

The field "Frontmatter.coverImg." was explicitly defined as non-nullable via the schema customization API (by yourself or a plugin/theme). This means that this field is not optional and you have to define a value. If this is not your desired behavior and you defined the schema yourself, go to "createTypes" in gatsby-node.js. If you're using a plugin/theme, you can learn more here on how to fix field types:
https://www.gatsbyjs.org/docs/schema-customization/#fixing-field-types

You can test it yourself in my theme repo: https://github.com/Defite/gatsby-theme-defite/tree/theme-v2

@Defite Defite added the type: question or discussion Issue discussing or asking a question about Gatsby label Apr 26, 2020
@Defite Defite changed the title Graphql query for optional field Graphql query for optional field for childImageSharp Apr 26, 2020
@lannonbr
Copy link
Contributor

In your type definition in createSchemaCustomization, change the type of coverImg to just File. Having the [] around it say it's an array which I don't believe you want, and the ! means that field is mandatory, so having just File (and keeping the @fileByRelativePath extension) makes it completely optional which is what you want. Then in your markup you can just check for it existing by doing something like:

{node.frontmatter.coverImg && <Img ... />}

@Defite
Copy link
Contributor Author

Defite commented Apr 26, 2020

@lannonbr, yeah, just figured this out, thank you! Moving this code to gatsby-node.js to my site dir works just fine. And is there any way to get rid of this message:

warn Plugin `@defite/gatsby-theme-defite` tried to define the GraphQL type `MarkdownRemark`, which has already been defined by the plugin

Gatsby throw it if I change gatsby-node.js in gatsby-theme-defite.

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label May 17, 2020
@Defite
Copy link
Contributor Author

Defite commented May 19, 2020

Let it be hacks then...

@Defite Defite closed this as completed May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants