I am starting to use Prisma with mongoDB, one of the heaviest approaches, in my opinion, are the relationships between a category and a subcategory, since the category is not referenced within the subcategory despite using the @relation (link: INLINE ). Anyway: I am using the User collection that contains a Pet array.
I am trying to get the User in the Pet documents on my local server (localhost:8000/graphql), but I can't, it shows me NULL. As far as I know: in theory the User is not stored on the side of the Pet document (mongoDB); and therefore Prisma makes an expensive search in each User document comparing it with each object of the Pets array, but in spite of that, it does not work.
There is something that confuses me too much, in the Prisma-server image(localhost:4466) that runs on Docker is POSSIBLE!! (gets the User and shows it to me), while on my local server (localhost:8000/graphql) it doesn't.
LOCAL SERVER (localhost:8000/graphql):
SERVER PRISMA ON DOCKER (localhost:4466):
This is how schemas look in 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
}
Resolvers:
async function feed(parent, args, context, info) {
return await context.prisma.pets({
...args
})
}
async function postPet(parent, args, context, 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
})
}
Here is an example in the DB:
I have also tried to include the User as a non-NULL in the Pet Schema, but at the time of making the query, I get an error "Cannot return null for non-nullable field Pet.owner.", because the User(owner) does not is declared in mongoDB
Pet {
... //the others are not shown for practical purposes
owner: User!
}
It would be helpful if you can help me with this big problem. Thank you!!
Pd: Does anyone know why Prism does not allow saving both references? UserCollection and PetsCollection