I am trying to define the correct schema for a gatsby graphql query so that it can return null (to make the content optional). From what I understand I need to predefine the schema in the gatsby-node.js file so that gatsby doesn't have to infer the type through the data. However I am not sure what the schema definition needs to be since we are using a gatsby fragment for querying the data. This is (part of) the query:
... on ContentfulWorkPage {
comingSoon {
id
title
image {
fluid(quality: 92, maxWidth: 1280) {
...GatsbyContentfulFluid_withWebp_noBase64
}
}
comingSoonText
}
}
The problem is that I am not sure how to define the GatsbyContentfulFluid_withWebp_noBase64
what I am trying:
const types = `
type ContentfulWorkPage implements Node {
comingSoon: ComingSoon
}
type ComingSoon {
id: String
title: String
comingSoonText: String
image: Image
}
type Image {
fluid: Fluid
}
type Fluid {
quality: Int
maxWidth: Int
title: String
}
`
createTypes(types)
even though I think I am on the right track I am still getting the following errors:
There was an error in your GraphQL query:
Unknown argument "quality" on field "fluid" of type "Image".
GraphQL request:70:21
69 | image {
70 | fluid(quality: 92, maxWidth: 1280) {
| ^
71 | ...GatsbyContentfulFluid_withWebp_noBase64
File: src/templates/mainPages/work.js:105:21
ERROR #85901 GRAPHQL
There was an error in your GraphQL query:
Unknown argument "maxWidth" on field "fluid" of type "Image".
GraphQL request:70:34
69 | image {
70 | fluid(quality: 92, maxWidth: 1280) {
| ^
71 | ...GatsbyContentfulFluid_withWebp_noBase64
File: src/templates/mainPages/work.js:105:34
ERROR #85901 GRAPHQL
There was an error in your GraphQL query:
Fragment "GatsbyContentfulFluid_withWebp_noBase64" cannot be spread here as objects of type "Fluid" can never be of type "ContentfulFluid".
GraphQL request:71:17
70 | fluid(quality: 92, maxWidth: 1280) {
71 | ...GatsbyContentfulFluid_withWebp_noBase64
| ^
72 | }
So I managed to solve this after digging around in the gatsby documentation (https://www.gatsbyjs.com/docs/actions/#printTypeDefinitions). I found that inside the
gatsby-node.js
file you can use aprintTypeDefinitions
function to print your data types, which you can then use to pre define the optional properties:This will create a
typeDefs.txt
file in your root folder where all infered types are described. So if you are looking for more complicated type defs like I was so that they can be made optional in contentful you can:createSchemaCustomization
example (
comingSoon
definition was copied from the typeDevs file):Extra tip: if you need to just specify one property to be optional but the rest can be infered. Remove the
@dontInfer
annotation and just copy the nullable property instead of copying the whole type from the file.