Edmir Suljic
06/22/2021, 1:41 PMimport { 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);
Edmir Suljic
06/22/2021, 1:43 PMRyan
06/22/2021, 1:48 PMEdmir Suljic
06/22/2021, 1:48 PMimport 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,
});
}
}
Edmir Suljic
06/22/2021, 1:49 PMRyan
06/22/2021, 1:52 PMtype 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?Edmir Suljic
06/22/2021, 1:53 PMtype User implements Node {
id: ID!
name: String!
email: String
password: String!
updatedAt: String!
createdAt: String!
}
Edmir Suljic
06/22/2021, 1:53 PMRyan
06/22/2021, 2:02 PMconst users = await prisma.user.findMany({
where: prisma2Where,
orderBy: prisma2OrderBy,
skip: prisma2Skip,
cursor: prisma2Cursor,
take: prisma2Take,
});
console.log(users);
Edmir Suljic
06/22/2021, 2:06 PMEdmir Suljic
06/22/2021, 2:06 PMRyan
06/22/2021, 2:08 PMNichita Z
06/22/2021, 2:08 PMEdmir Suljic
06/22/2021, 2:10 PMEdmir Suljic
06/22/2021, 2:12 PMRyan
06/22/2021, 2:16 PMgraphql
file someplace?Edmir Suljic
06/22/2021, 2:17 PMEdmir Suljic
06/22/2021, 2:18 PMEdmir Suljic
06/22/2021, 2:19 PMgraphql
file I have is the schema one in srcRyan
06/22/2021, 2:20 PMprisma.graphql
file being referenced someplace still?Ryan
06/22/2021, 2:20 PMEdmir Suljic
06/22/2021, 2:22 PMRyan
06/22/2021, 2:23 PMEdmir Suljic
06/22/2021, 2:24 PMRyan
06/22/2021, 2:24 PMEdmir Suljic
06/22/2021, 2:24 PMtype-graphql
but I dont feel like learning that as well, since this setup should worknikolasburk
Edmir Suljic
06/22/2021, 2:28 PMEdmir Suljic
06/22/2021, 2:28 PMnikolasburk
Edmir Suljic
06/22/2021, 2:34 PMEdmir Suljic
06/22/2021, 2:35 PMnikolasburk
nikolasburk
Edmir Suljic
06/22/2021, 2:38 PMnikolasburk
nikolasburk
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 wrongSorry for the confusion here! 😞
Edmir Suljic
06/22/2021, 2:40 PMEdmir Suljic
06/22/2021, 2:41 PMnikolasburk
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:
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".nikolasburk
Edmir Suljic
06/22/2021, 2:49 PMEdmir Suljic
06/23/2021, 7:08 AMEdmir Suljic
06/23/2021, 7:08 AMexport 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;
},
}
Edmir Suljic
06/23/2021, 7:09 AM