All Articles

Invariant Violation: Encountered an error trying to infer a GraphQL type for contentModel. Here's a fix.

If you’re reading this, you probably saw this github issue

https://github.com/LekoArts/gatsby-starter-prismic/issues/19

and running

gatsby clean

did not solve your issue.

error in contentful

I’m using Contentful as my headless CMS and I was able to fix this by removing one of the references in the content model.

I have two content models, let’s name it Model A and Model B.

Model A has a one-to-many relationship with Model B.

Unfortunately, Model B has no idea that it has any relationship with Model A so you have to add another property and connect it to Model A. This is one of the limitation of Contentful . So

Model B has a one-to-one relationship with Model A.

Here’s a quote from one of their forums

The workaround for two-way links is- create reference fields in both entries. So both entries will become parent and child for each other.

That’s sort of many-to-many as mentioned.

This is all good when you’re NOT using Gatsby.js!

So to actually fix this, I just have to remove the property in Model B because the contentful plugin will take care of the reverse reference for you. That’s neat.

Well this was actually written in their README file :(

When using reference fields, be aware that this source plugin will automatically create the reverse reference. You do not need to create references on both content types