3

Tryign to do I am trying to Query my Products and to also show what Categories they are under.

Here is my Prisma Schema

model Product {
  id String @id @default(uuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  name String @db.VarChar(80)
  description String @db.VarChar(240)
  ingredients String[]
  price Float
  category Category[] 
  
}

model Category {
    id String @id @default(uuid())
    createdAt DateTime @default(now())
    name String 
    product Product[]
   
}

This shows that there is a relationship between the Products and Categories

However, when I query in Apollo Studio, it doesn't show up.

enter image description here

Here is my schema in my Prisma Studio as well to show the relationship

enter image description here

Any information would be greatly appreciated!

Here is also the resolver.

export const resolvers = {
    Query: { 
        allCategories:(_parent:any, _args:any, context: Context) => {
            return context.prisma.category.findMany()
        },
        allProducts:(_parent:any, _args:any, context:Context) => {
            return context.prisma.product.findMany()
        },
       productById:(_parent: any, {id}, context: Context) => {
           return context.prisma.product.findUnique({
               where: {
                   id
               }
           })
       },
       categoryById:(_parent:any, {id}, context: Context) => {
           return context.prisma.category.findUnique({
               where: {
                   id
               }
           })
       },
       productsByCategory:(_parent:any, {category}, context: Context) => {
        return context.prisma.product.findMany({
            select: {
                category: true
            }
        })
       }
    },
Esore
  • 243
  • 2
  • 9

1 Answers1

0

The relationship in the Prisma schema doesn't automatically map to the relationship in GraphQL. You will need to define another relationship in your GraphQL schema and then also implement the corresponding type resolvers.

Here is an example for how you can define the relationship in your GraphQL schema (often called schema.graphql if it lives in its own file).

Here is an example for a many-to-many relationship with User and Post models:

Prisma schema

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id        Int    @id @default(autoincrement())
  authors   User[]
}

GraphQL SDL

type User {
  id: Int!
  posts: [Post!]!
}


type Post {
  id: Int!
  authors: [User!]!
}

Resolvers

const resolvers = {
  Post: {
    author: (parent, _args, context: Context) => {
      return context.prisma.post
        .findUnique({
          where: { id: parent?.id },
        })
        .authors()
    },
    },
  User: {
    posts: (parent, _args, context: Context) => {
      return context.prisma.user
        .findUnique({
          where: { id: parent?.id },
        })
        .posts()
    },
  } 
}

You can see a runnable version of this (though as a one-to-many relationship) here: https://github.com/prisma/prisma-examples/tree/latest/typescript/graphql-sdl-first

nburk
  • 22,409
  • 18
  • 87
  • 132