我开始将Prisma与mongoDB结合使用,在我看来,最重的方法之一是类别和子类别之间的关系,因为尽管使用@relation,但在子类别中未引用类别(链接:INLINE )。无论如何:我正在使用包含Pet数组的User集合。
我试图在本地服务器(localhost:8000 / graphql)上的Pet文档中获取用户,但是我不能,它显示为NULL。据我所知:理论上,用户不存储在Pet文档(mongoDB)的一侧;因此Prisma会在每个User文档中进行一次昂贵的搜索,将其与Pets数组的每个对象进行比较,但是尽管如此,它还是行不通的。
在Docker上运行的Prisma服务器映像(localhost:4466)可能使我感到困惑。 (获取用户并将其显示给我),而在我的本地服务器(localhost:8000 / graphql)上却没有。
本地服务器(localhost:8000 / graphql):
DOCKER上的PRISMA服务器(localhost:4466):
模式在datamodel.prisma中的外观如下:
type User {
id: ID! @id
name: String!
email: String! @unique
password: String!
pets: [Pet!]! @relation(link: INLINE)
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
type Pet {
id: ID! @id
name: String!
description: String!
sex: String!
owner: User
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
typeDefs:
type Query {
feed(where: PetWhereInput,skip: Int,first: Int,orderBy: PetOrderByInput): [Pet]!
}
type Mutation {
postPet(data: PetCreateInput!): Pet
}
解析器:
async function feed(parent,args,context,info) {
return await context.prisma.pets({
...args
})
}
async function postPet(parent,info) {
const userId = getUserId(context) //Validate User
return await context.prisma.createPet({
...args.data,owner: { connect: { id: userId }} //Create the relationship between User and Pet
})
}
这是数据库中的一个示例:
我也尝试过在Pet模式中将User作为非NULL包括在内,但是在进行查询时,出现错误“无法为非null字段Pet.owner返回null。”,因为User(所有者)未在mongoDB中声明
Pet {
... //the others are not shown for practical purposes
owner: User!
}
如果您可以帮助我解决这个大问题,将对您有所帮助。谢谢!!
Pd:有谁知道为什么Prism不允许保存两个引用? UserCollection和PetsCollection