Nested Querying with Many to Many relationships returning null

I’m trying to do a nested query with a many to many relationship, but the object nested 2 layers deep, is returning as null in GraphQL, even when the object is being correctly fetched from Prisma.

In my example, there’s an explicit many-to-many relationship between users and crews. A user can belong to many crews and a crew can have many users. I’m trying to query for all crews, and the users that are on the crews.

Here’s my relevant schema:

model User {
  id                  Int                @id @default(autoincrement())
  email               String?            @unique
  phoneNum            String?            @unique
  name                String?
  crews               UserOnCrew[]
}

model UserOnCrew {
  id     Int             @id @default(autoincrement())
  user   User            @relation(fields: [userId], references: [id])
  userId Int
  crew   Crew            @relation(fields: [crewId], references: [id])
  crewId Int
  role   UserOnCrewRole

  @@unique([userId, crewId])
}

model Crew {
  id          Int          @id @default(autoincrement())
  name        String       @default("")
  createdAt   DateTime     @default(now())
  description String       @default("")
  users       UserOnCrew[]
}

Here’s my relevant SDL files:

#users.sdl.ts
type User {
  id: Int!
  name: String
  email: String
  phoneNum: String
}

#userOnCrews.sdl.ts
type UserOnCrew {
  id: Int!
  user: User
  userId: Int!
  crew: Crew
  crewId: Int!
  role: UserOnCrewRole!
}

#crews.sdl.ts
type Crew {
  id: Int!
  name: String!
  createdAt: DateTime!
  description: String!
  users: [UserOnCrew!]!
}

type Query {
    crews: [Crew!]! @skipAuth
}

Here’s my service file

export const crews: QueryResolvers['crews'] = async () => {
  return db.crew.findMany({
    include: {
      users: { include: { crew: true, user: true } },
    },
  })
}

Here’s my query:

query MyCrews {
  crews {
    id
    users {
      id
      user {
        id
      }
    }
  }
}

which comes back on the GraphQL Playground as

{
  "data": {
    "crews": [
      {
        "id": 1,
        "users": [
          {
            "id": 1,
            "user": null
          },
          {
            "id": 8,
            "user": null
          }
        ]
      }
    ]
  }
}

The nested “user” object is null. However, when I put a breakpoint on the service, it shows that Prisma is returning the user object. However on the response, that user object is set to null. Is this a problem with GraphQL, or am I missing something very basic?

This question is similar to Many to Many relationship returning null, though that conversation hasn’t been entirely resolved. Would really appreciate any help/pointers!

Hi @richiboi and welcome to our community,

Is the service file an extract?

I feel like we should see some generated resolvers at the end of that file, at least when you scaffold your service it should happen.

Also: have you tried by selecting explicitly the fields you want to query from crew.users.user ?

Can you provide some place to reproduce?

2 Likes

Came across this as I was having the same problem. I had created the services manually rather than scaffolding them using the CLI and my resolvers weren’t quite right. I created a blank project with the same schema, ran `yarn rw g scaffold ’ and copied the resolvers generated at the bottom of my services to my main project and it worked!