graphql 的嵌套解析器棱镜查询
任何人都可以建议如何创建一个嵌套解析器来显示通过 graphql 保存在父对象中的对象数组。
在本例中,我的 schema.prisma 文件有两个模型 GiftCard 和 Transaction,其中每个 GiftCard 都有一个分类帐(交易数组):
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?
}
我的 graphql 类型定义:
type Query {
cards: [GiftCard!]!
transactions: [Transaction!]!
}
type GiftCard {
id: ID!
number: String!
ledger: [Transaction!]!
}
type Transaction {
id: ID!
value: Float!
}
并且我的解析器:
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...
},
},
};
如果我在没有请求“分类帐”字段的情况下查询卡或交易,则查询从apollo studio可以完美运行。
任何帮助都会很棒!
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好消息!我在 prisma 文档中找到了解决方案,它不是嵌套解析器。
默认情况下,Prisma 不会在解析器内部启用相互关系。您必须强制它包含它们,以便我的解析器现在看起来像这样,并且我的所有事务都显示在 graphql 查询中:
Good news! Buried in the prisma docs I found the solution and it is not a nested resolver.
Prisma does not enable mutual relationships by default inside resolvers. You have to force it to include them so my resolver now looks like this and all my transactions are showing in the graphql query: