Prisma binding Nested filtering

524 views Asked by At

I'm working on a food order platform with Prisma, Prisma-binding and Apollo Server on the backend. A customer can choose a restaurant in his neighbourhood and add one or more dishes to his cart. It is possible that when a dish from restaurant x is already added, the customer decides to order from another restaurant, restaurant y. Therefore I need to filter the added dishes when making an order based on the customer id and the final chosen restaurant in the backend first before creating the order and payment url.

I've got three data types inside my prisma datamodel: Customer, CartItem and Dish

type Customer {
    id: ID! @id
    createdAt: DateTime! @createdAt
    updatedAt: DateTime! @updatedAt
    name: String
    email: String
    phone: String
    user: User
    cart: [CartItem]!
    orders: [Order]!
}

type CartItem {
    id: ID! @id
    quantity: Int! @default(value: 1)
    dish: Dish!
    customer: Customer! @relation(link: INLINE)
}

type Dish {
    id: ID! @id
    name: String!
    price: String!
    description: String!
    isAvailable: Boolean! @default(value: true)
    category: String
    restaurant: Restaurant!
}

In the Prisma GraphQL playground that is directly connected to the database I can filter the cartItems that I need to create the order like this:

query {
  customer(where: { id: "ck8zwslgs00da0712cq88e3oh" } ) {
    id
    cart(where: { dish: { restaurant: { id: "ck904gwl400mz0712v0azegm3" } } }) {
      quantity
      dish {
        name
        price
        restaurant {
          id
          name
        }
      }
    }
  }
}

output:

{
  "data": {
    "customer": {
      "id": "ck8zwslgs00da0712cq88e3oh",
      "cart": [
        {
          "quantity": 2,
          "dish": {
            "name": "Nachos Plate Hawaii",
            "price": "1150",
            "restaurant": {
              "id": "ck904gwl400mz0712v0azegm3",
              "name": "Taco Bell"
            }
          }
        },
        {
          "quantity": 1,
          "dish": {
            "name": "Nachos Plate Vulcano",
            "price": "1250",
            "restaurant": {
              "id": "ck904gwl400mz0712v0azegm3",
              "name": "Taco Bell"
            }
          }
        }
      ]
    }
  }
}

So far so good but now I need the same query in the Apollo Server using prisma-binding. I tried a few things but none of them are working. The first two are returning an error "Field \"cart\" is not defined by type CustomerWhereUniqueInput". The last two are just returning every cartItem without the restaurant filter.

const data = await ctx.db.query.customer({
    where: {
        AND: [
            { 
                id: args.customerID 
            },
            {
                cart: {
                    dish : {
                        restaurant: {
                            id: args.restaurantID
                        }
                    }
                }
            }
        ]
    }
}, info);


const data = await ctx.db.query.customer({
    where: {
        id: args.customerID
        cart: {
            dish : {
                restaurant: {
                    id: args.restaurantID
                }
            }
        }
    }
}, info);


const data = await ctx.db.query.customer({
    where: {
        id: args.customerID
    },
    cart: {
        where: {
            dish : {
                restaurant: {
                    id: args.restaurantID
                }
            }
        }
    }
}, info);


const data = await ctx.db.query.customer({
    where: {
        id: args.customerID
    },
    cart: {
        dish : {
            restaurant: {
                where: {
                    id: args.restaurantID
                }
            }
        }
    }
}, info);

Can someone help me out with the right way to filter on the customer id and the restaurant id?

0

There are 0 answers