DEV Community

Cover image for Variable "$slug" of required type "String!" was not provided.
yassineJoudi
yassineJoudi

Posted on

Variable "$slug" of required type "String!" was not provided.

I receive this error when executing: gatsby build
ERROR #85920 GRAPHQL

There was an error in your GraphQL query:

Variable "$slug" of required type "String!" was not provided
query homeyassineContainersmyBlogsrcpagestemplatehealthJs3338664333($slug: String!) {

  contentfulBlogHealth(slug: {eq: $slug}) {
    title,
    date(formatString: "MMMM Do, YYYY"),
    body {
      json
    },
    id
 }

}

Top comments (7)

Collapse
 
itsjzt profile image
Saurabh Sharma

Are you sure the slug variable is passed correctly. It seems like this query isnt getting slug variable which is mandatory to run this query

Collapse
 
yassinejoudi profile image
yassineJoudi

Good evening, and thanks ahead of time for any help.
when run "gatsby develop", I received the all data without these errors, but when use "gatsby build" I received this error "Variable "$slug" of required type "String!" was not provided.".

Collapse
 
rajeshroyal profile image
Rajesh Royal • Edited

I found the solution

The issue is you should not put template files in src/pages folder.

It so happens that I have already moved my single-post.js, post.js and other CPT .js files into a new, custom folder (/src/templates).

If this didn't work then remove ! mark from ($slug: String)

Thank you!

Thread Thread
 
lparolari profile image
Luca Parolari

Hello, I'm experiencing the same issue as the author of the post. I found your solution works, but I would like to know why this is happening and why moving the template file to another directory solves the problem.

Thank you!

Thread Thread
 
rajeshroyal profile image
Rajesh Royal

Don't know brother, It's been awhile since I'm using gatsby.

Thread Thread
 
greenjohn profile image
John Yellow

Hi Luca and Rajesh, you guys really saved me here. I had been 2 days already with this error, I even asked in the gatsby Discord, but you guys were eventually the saviors!

I think I know what's going on. Gatsby tries to render all the files in src/pages as pages. When it gets to the templates that are used by createPage, it freaks out because the context that you passed from is not there (createPage hasn't been called at this point). So when you move these files to another directory, Gatsby no longer tries to render your templates as pages and only invokes them after createPage has been called.

Does that make sense?

Collapse
 
rajeshroyal profile image
Rajesh Royal

di you find the answer, I really need help