Redwood: GraphQL Error in tutorial after running yarn rw g scaffold post

Created on 1 Jun 2020  ·  3Comments  ·  Source: redwoodjs/redwood

I have followed the tutorial up to part 2 creating a post editor. The last command I ran was yarn rw g scaffold post which scaffolds out the following.

✔ Generating scaffold files...
    ✔ Writing `./api/src/graphql/posts.sdl.js`...
    ✔ Writing `./api/src/services/posts/posts.test.js`...
    ✔ Writing `./api/src/services/posts/posts.js`...
    ✔ Writing `./web/src/scaffold.css`...
    ✔ Writing `./web/src/layouts/PostsLayout/PostsLayout.js`...
    ✔ Writing `./web/src/pages/EditPostPage/EditPostPage.js`...
    ✔ Writing `./web/src/pages/PostPage/PostPage.js`...
    ✔ Writing `./web/src/pages/PostsPage/PostsPage.js`...
    ✔ Writing `./web/src/pages/NewPostPage/NewPostPage.js`...
    ✔ Writing `./web/src/components/EditPostCell/EditPostCell.js`...
    ✔ Writing `./web/src/components/Post/Post.js`...
    ✔ Writing `./web/src/components/PostCell/PostCell.js`...
    ✔ Writing `./web/src/components/PostForm/PostForm.js`...
    ✔ Writing `./web/src/components/Posts/Posts.js`...
    ✔ Writing `./web/src/components/PostsCell/PostsCell.js`...
    ✔ Writing `./web/src/components/NewPost/NewPost.js`...

In my console I have the following errors when I visit the /posts page

index.js:1 Error: GraphQL error: 
Invalid `prisma.post.findMany()` invocation in
/Users/anderskitson/sites/redwoodblog/api/src/services/posts/posts.js:4:18

&

react-dom.development.js:14348 Uncaught Error: Query(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

This is what the posts.js file looks like

import { db } from 'src/lib/db'

export const posts = () => {
  return db.post.findMany()
}

export const post = ({ id }) => {
  return db.post.findOne({
    where: { id },
  })
}

export const createPost = ({ input }) => {
  return db.post.create({
    data: input,
  })
}

export const updatePost = ({ id, input }) => {
  return db.post.update({
    data: input,
    where: { id },
  })
}

export const deletePost = ({ id }) => {
  return db.post.delete({
    where: { id },
  })
}

This is what my schema.prisma file looks like

datasource DS {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = env("BINARY_TARGET")
}

// Define your own datamodels here and run `yarn redwood db save` to create
// migrations for them.

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  body      String
  createdAt DateTime @default(now())
}

Not quite sure what is wrong at this point, I'm not familiar with prisma and that seems to be where the error is being generated from. Thanks.

Most helpful comment

@wispyco Did you run the yarn rw db save and yarn rw db up commands? See Migrations

All 3 comments

@wispyco Did you run the yarn rw db save and yarn rw db up commands? See Migrations

Hi @wispyco thanks for checking in here with your question.

And I definitely want to 👍what @Terris wrote above. When I see that error, it's often because I didn't _first_ make a new migration of my DB with the added table (... db save) and then apply/migrate that change to my local development DB (... db up). Best practice is to do that before you generate the scaffold CRUD files.

Lastly, _definitely_ make sure you STOP the dev-server and then re-start with yarn rw dev.

Yes working now. I think I missed one of the commands, they were both in my history when I cycled through, but maybe it was the order of the scaffold before the up command. Thanks both of you. Looking forward to seeing what I can do with redwood and seeing how it evolves.

Was this page helpful?
0 / 5 - 0 ratings