Could anyone suggest how to go about creating a nested resolver to show an array of objects that are held in a parent object through graphql.
In this case, my schema.prisma file has two models GiftCard and Transaction where each GiftCard has a ledger (array of transactions):
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model GiftCard {
id Int
number String
ledger Transaction[]
}
model Transaction {
id Int
value Decimal
giftCard GiftCard? @relation(fields: [giftCardId], references: [id])
giftCardId Int?
}
My typedef for graphql:
type Query {
cards: [GiftCard!]!
transactions: [Transaction!]!
}
type GiftCard {
id: ID!
number: String!
ledger: [Transaction!]!
}
type Transaction {
id: ID!
value: Float!
}
And my resolvers:
export const resolvers = {
Query: {
cards: async (parent, args, context) => {
return context.prisma.GiftCard.findMany();
},
vendors: async (parent, args, context) => {
return context.prisma.Vendor.findMany();
},
transactions: async (parent, args, context) => {
return context.prisma.Transaction.findMany();
},
},
GiftCard: {
ledger: async (parent, args, context) => {
// return transaction associated with gift card here...
},
},
};
The query works perfectly from apollo studio if I query Cards or Transaction without the "ledger" field requested.
Any help would be great!
Thankyou.