Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
feat(review): add movie resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
UtopiaBeam committed Aug 19, 2021
1 parent 4f48e0c commit 5c0133c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 2 additions & 4 deletions lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import { PrismaClient } from '../generated/prisma'
import { Request } from 'express'

export interface Context {
isAuthenticated: boolean
prisma: PrismaClient
}

export function createContext(req: Request) {
if (!req.headers.authorization) {
throw new Error('Unauthorized')
}
console.log(req.headers.authorization)

return {
isAuthenticated: Boolean(req.headers.authorization),
prisma: new PrismaClient(),
}
}
7 changes: 7 additions & 0 deletions services/movie/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ export const resolvers: Resolvers<Context> = {
}) as unknown as Promise<Movie>
},
},
Movie: {
__resolveReference: async (parent, ctx) => {
return ctx.prisma.movie.findUnique({
where: { id: parent.id },
}) as unknown as Promise<Movie>
},
},
}
17 changes: 13 additions & 4 deletions services/reviews/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Resolvers } from '../../generated/types'
import { Movie, Resolvers, Review } from '../../generated/types'
import { Context } from '../../lib/context'

export const resolvers: Resolvers<Context> = {
Query: {
reviews: async (_parent, _args, ctx) => {
return ctx.prisma.review.findMany()
return ctx.prisma.review.findMany() as unknown as Promise<Review[]>
},
},
Mutation: {
createReview: async (_parent, { input }, ctx) => {
return ctx.prisma.review.create({ data: input })
return ctx.prisma.review.create({
data: input,
}) as unknown as Promise<Review>
},
},
Movie: {
Expand All @@ -22,7 +24,14 @@ export const resolvers: Resolvers<Context> = {
return result._avg.rating ?? 0
},
reviews: async (parent, _args, ctx) => {
return ctx.prisma.review.findMany({ where: { movieId: parent.id } })
return ctx.prisma.review.findMany({
where: { movieId: parent.id },
}) as unknown as Promise<Review[]>
},
},
Review: {
movie: (parent, _args, _ctx) => {
return { __typename: 'Movie', id: parent.id } as Movie
},
},
}
1 change: 1 addition & 0 deletions services/reviews/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const typeDefs = gql`
id: ID!
rating: Int!
comment: String
movie: Movie!
}
extend type Movie @key(fields: "id") {
Expand Down

0 comments on commit 5c0133c

Please sign in to comment.