0
votes

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):
localhost:8000/graphql
SERVER PRISMA ON DOCKER (localhost:4466):
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:
enter image description here
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

1

1 Answers

1
votes

Apparently I made a big mistake! I missed implementing these two functions in the Resolvers:

function pets(parent, args, context) {
   return context.prisma.user({ id: parent.id }).pets()
}

function owner(parent, args, context) {
   return context.prisma.pet({ id: parent.id }).owner() 
}

These two fields of our GraphQL scheme that cannot be solved in the same way: "owner" in Pet and "pets" in User. These fields must be explicitly implemented because our GraphQL server cannot infer where to get that data.

In the owner function (resolver), first are fetching the Link using the prisma client instance and then invoke "owner" on it. Notice that the resolver needs to be called "owner" because it resolves the "owner" field from the Pet type in schema.graphql. We can resolve the pets relation in a similar way.

Sources:
https://github.com/prisma/prisma/issues/4143#issuecomment-474880883 https://www.howtographql.com/graphql-js/6-authentication/