Hi guys, I would really appreciate some help. I di...
# orm-help
e
Hi guys, I would really appreciate some help. I did a graphql course on udemy that unfortunately used prisma 1. After migrating to prisma 2 (or actually starting a whole new project), I'm having a lot of problems trying to get it to work. Using graphql-yoga and the new PrismaClient(), I keep getting the "cannot return null on non-nullable field", even though I believe I have done everything correctly (well obviously not everything). Anyone have any ideas? This is my index.ts file:
Copy code
import { GraphQLServer } from "graphql-yoga";
import { resolvers } from "./resolvers/index";
import { PrismaClient } from "@prisma/client";
import "reflect-metadata";

export interface Context {
  prisma: PrismaClient;
  request: any;
}

const main = async () => {
  const prisma = new PrismaClient();

  const server = new GraphQLServer({
    typeDefs: "./src/schema.graphql",
    resolvers,
    context: ({ request }) => ({ request, prisma } as Context),
  });

  server.start({ port: process.env.PORT || 4000 }, () => {
    console.log("The server is up!");
  });
};

main().catch(console.error);
If any more information is needed, please say so
r
Depends on what you’re querying for. It’s possible that you have kept something in GraphQL that is non-nullable but is null in the database. So it would be great if you could share your resolver.
e
Copy code
import getUserId from "../utils/getUserId";
import {
  makeOrderByPrisma2Compatible,
  makeWherePrisma2Compatible,
} from "@prisma/binding-argument-transform";
import { Context } from "..";

export class Query {
  async users(parent, args, { prisma }, info) {
    const { where, orderBy, skip, first, last, after, before } = args;
    const prisma2Where = makeWherePrisma2Compatible(where)
    const prisma2OrderBy = makeOrderByPrisma2Compatible(orderBy)
    const skipValue = skip || 0
    const prisma2Skip = Boolean(before) ? skipValue + 1 : skipValue
    const prisma2Take = Boolean(last) ? -last : first
    const prisma2Before = { id: before }
    const prisma2After = { id: after }
    const prisma2Cursor =
      !Boolean(before) && !Boolean(after)
        ? undefined
        : Boolean(before)
        ? prisma2Before
        : prisma2After

    return await prisma.user.findMany({
      where: prisma2Where,
      orderBy: prisma2OrderBy,
      skip: prisma2Skip,
      cursor: prisma2Cursor,
      take: prisma2Take,
    });
  }
}
I'm not a big fan of all that code for the pagination but it was on the prisma migration tutorial and I just went with it
r
And what is the GraphQL type?
Copy code
type Query {
  users(): [User!]!
}
What’s the
User
type here? Are you getting all the fields from the database that you have marked as required in your GraphQL schema?
e
Copy code
type User implements Node {
    id: ID!
    name: String!
    email: String
    password: String!
    updatedAt: String!
    createdAt: String!
}
I mean I dont have any users in my db at the moment, but it should still return an empty array
r
Yes it should. Logging this part returns something?
Copy code
const users = await prisma.user.findMany({
      where: prisma2Where,
      orderBy: prisma2OrderBy,
      skip: prisma2Skip,
      cursor: prisma2Cursor,
      take: prisma2Take,
    });
console.log(users);
e
I dont know why but the console is not outputting anything at all. Like not even an empty string, its like its skipping it
Seems like there is some issue there
r
I think your resolver is not hitting. Do you have any middleware or logic that sits b/w the resolver?
n
try putting another console log before and after that one, see if those show up, might be easier to tell what’s between them
e
Nope no middleware. I just import the resolvers to the index.ts file.
Although I'm seeing some odd behavior that seems to be related to prisma 1. Not sure. But if I remove /generated/prisma.graphql, the server wont start since it cant find it. But as I understood, that generated file is not needed anymore?
r
Do you have another
graphql
file someplace?
e
well there is the /generated/prisma.graphql one in /src/
but i removed all of that now, the server is running fine but still get the null error
so the only
graphql
file I have is the schema one in src
r
Is the
prisma.graphql
file being referenced someplace still?
You need a GraphQL file though. Without that your app will not run.
e
no prisma.graphql is not referenced anywhere and yes I know, I have the schema.graphql file with my schema
r
That’s strange then. @nikolasburk wdyt?
e
Its probably something I messed up in the migration process to prisma 2 (and maybe to Typescript but probably not because why should that create problems) but I just cant see it
r
If it’s not. hitting the resolver then I’m sure that the GraphQL file or something is messed up.
e
I even found a couple of boilerplate projects on GH doing the same thing but nothing. Some are using
type-graphql
but I dont feel like learning that as well, since this setup should work
n
If the problem is that a relation doesn't get resolved, did you maybe miss implementing the _type resolvers_ to resolve the relations, Edmir?
e
well I kind of did that I think. I got a little confused by it and thought I would just try to get it to work. Plus at the moment I dont have any relations. I only have a User model
So i thought it wouldnt be needed
n
Ah you're right, if there are no relations then that's not needed indeed 👍 stepping back a little: What is your goal at the moment? Are you building a real project or are you mostly learning Prisma and/or GraphQL? If it's the latter, other resources might be more better suited for you 🙂
e
Well the end goal is to build a backend server for a project a couple of months down the road. I'm just kind of preparing/learning GraphQL and Prisma so that I can at least pretend to know what I'm doing later heh...
but the recent course I did on udemy did go through the whole process of developing and deploying to production. Although it was prisma 1 so there are some gaps in my knowledge still
n
Is it the course from Andrew? Prisma 1 and Prisma 2 are very different. If your plan is to start a new project with Prisma 2, I'd definitely recommend you to start with Prisma 2 directly, otherwise you're in for a very bumpy ride to understand all of the moving pieces 😌
Here's another more up-to-date course you might want to take instead: https://courses.codemochi.com/end-to-end-react-with-prisma-2/
e
Yes it is exactly that course and you are indeed right.. it has been a VERY bumpy road... I realized that the course was outdated a bit too late so I just went with it hoping I would figure it out later... guess I was wrong
👍 1
n
Also, @Mahmoud is currently working on another course that covers Prisma and GraphQL, so if you keep an eye out for announcements here on Slack and on the Prisma blog, you'll get access to that over the next couple of weeks as well 🙂
🔥 1
it has been a VERY bumpy road... I realized that the course was outdated a bit too late so I just went with it hoping I would figure it out later... guess I was wrong
Sorry for the confusion here! 😞
e
But I still feel like I understand the concept of most of the pieces and I just kind of need to put them together. Not really looking for another course that will take a couple of days/weeks to finish.
👍 1
It feels like this issue is very trivial and should be solved easily. I just need to be kicked in the right direction. Although I do appreciate the recommendations
n
Not really looking for another course that will take a couple of days/weeks to finish.
Fair enough 👍 if you feel like you have a good understanding of the main concepts already, you should try to remove this code from your resolvers:
Copy code
const prisma2Where = makeWherePrisma2Compatible(where)
    const prisma2OrderBy = makeOrderByPrisma2Compatible(orderBy)
    const skipValue = skip || 0
    const prisma2Skip = Boolean(before) ? skipValue + 1 : skipValue
    const prisma2Take = Boolean(last) ? -last : first
    const prisma2Before = { id: before }
    const prisma2After = { id: after }
    const prisma2Cursor =
      !Boolean(before) && !Boolean(after)
        ? undefined
        : Boolean(before)
        ? prisma2Before
        : prisma2After
The reason why that was included in the upgrade guide was to make sure that people don't need to adjust their SDL-first GraphQL schema from Prisma 1. However, if you're starting/learning, you can still shape your SDL in the way you like and don't need these (horrible 🙈) "transformations".
Maybe our examples could also help you in some way?
e
Alright, will look into it. Thanks for answering. I'll get back to you if I run in to any issues if thats ok
👍 1
If anyone is interested, I found the issue. As I was migrating to Typescript, I changed my resolvers to classes with functions for each operation. This did not work as I assume I would need to refactor the code a bit in order to make it work. I changed it back to normal JS objects like:
Copy code
export const Query = {
  async users(parent, args, { prisma }, info) {
    const { where, orderBy, skip, first, last, after, before } = args;
 

    const users = await prisma.user.findMany();

    console.log(users)
    return users;
  },
}
While on the subject, is there any benefit to use the TS OOP functionality here like I did initially or should I just keep it with the JS way?