RelationResolver aren't recognized if i put them by myself

hey !

i’ve updated my prisma.schema and added some relations for a table. when i’m looking at the table .sdl, there’s no relation resolvers. So i decided to add them by myself but it shows me an error. i’ve tried to regenerate the type but it didn’t worked. what can i do ?

Hey @alaborde29 could you provide more details about the relations you added? And just so I have things straight in my head, you added a relation, tried to manually add the SDL, then tried re-generating the SDL using the generator via the --force flag?

The relation resolvers are in the service files. They normally look like this:

export const Post: PostRelationResolvers = {
  author: (_obj, { root }) => {
    return db.post.findUnique({ where: { id: root?.id } }).author()
  },
}

Where the Prisma schema is…

model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id       Int  @id @default(autoincrement())
  author   User @relation(fields: [authorId], references: [id])
  authorId Int // relation scalar field  (used in the `@relation` attribute above)
}

I didn’t used the --force flag because i didn’t know it existed.

Btw, i’ve managed to resolve this issue with some tricky actions.

i’ve copied the entier sdl file and associated service file in another one in order to not lose what i added.

Then i’ve tried to rescaffold the table, then adding my work back and it worked.

I don’t like that because it’s a workaround. i don’t know if a cleaner solution exist.

Howdy! I think I have a similar issue where I’ve added some custom relationship names and its not generating the graphql types in the schema.graphql file correctly nor are the TypeScript types being generated correctly in the graphql.d.ts file.

Here’s my prisma schema:

model Organization {
  id   Int    @id @default(autoincrement())
  name String

  memberships   Membership[] @relation("OrgMemberships")
  loggedInUsers User[]       @relation("LoggedInUsers")
}

model Membership {
  id     Int            @id @default(autoincrement())

  organization   Organization @relation("OrgMemberships", fields: [organizationId], references: [id])
  organizationId Int

  user          User?  @relation("UserMemberships", fields: [userId], references: [id])
  userId        Int?
  loggedInUsers User[] @relation("LoggedInUsersMem")

  @@unique([organizationId, invitedEmail])
  @@unique([organizationId, userId])
}

model User {
  id                   Int           @id @default(autoincrement())
  ...
  memberships          Membership[]  @relation("UserMemberships")
  loggedInOrg          Organization? @relation("LoggedInUsers", fields: [loggedInOrgId], references: [id])
  loggedInOrgId        Int?
  loggedInMembership   Membership?   @relation("LoggedInUsersMem", fields: [loggedInMembershipId], references: [id])
  loggedInMembershipId Int?
}

Here’s what Redwood is generating in the graphql.d.ts file

export type OrganizationResolvers<ContextType = RedwoodGraphQLContext, ParentType extends ResolversParentTypes['Organization'] = ResolversParentTypes['Organization']> = {
  id: OptArgsResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
  name: OptArgsResolverFn<ResolversTypes['String'], ParentType, ContextType>;
  users: OptArgsResolverFn<Array<Maybe<ResolversTypes['User']>>, ParentType, ContextType>;
  __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type OrganizationRelationResolvers<ContextType = RedwoodGraphQLContext, ParentType extends ResolversParentTypes['Organization'] = ResolversParentTypes['Organization']> = {
  id?: RequiredResolverFn<ResolversTypes['Int'], ParentType, ContextType>;
  name?: RequiredResolverFn<ResolversTypes['String'], ParentType, ContextType>;
  users?: RequiredResolverFn<Array<Maybe<ResolversTypes['User']>>, ParentType, ContextType>;
  __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Here’s what is in the schema.graphql file

type Organization {
  id: Int!
  name: String!
  users: [User]!
}

And finally the error when I try to add the relation resolver to the SDL file:

export const Organization: OrganizationRelationResolvers = {
  memberships: (_obj, { root }) => {
    return db.organization.findUnique({ where: { id: root?.id } }).memberships()
  },
  loggedInUsers: (_obj, { root }) => {
    return db.organization
      .findUnique({ where: { id: root?.id } })
      .loggedInUsers()
  },
}

error:

Type '{ memberships: (_obj: any, { root }: { root: any; }) => PrismaPromise<(GetResult<{ id: number; role: MembershipRole; active: boolean; organizationId: number; invitedName: string; invitedEmail: string; userId: number; }, unknown> & {})[]>; loggedInUsers: (_obj: any, { root }: { ...; }) => PrismaPromise<...>; }' is not assignable to type 'OrganizationRelationResolvers'.
  Object literal may only specify known properties, and 'memberships' does not exist in type 'OrganizationRelationResolvers'.ts(2322)

@dom any ideas? I apologize if this isn’t enough or this is a dumb question. Pretty new to Redwood & GraphQL

Disregard. I think I figured it out. I had gotten so used to Redwood generating my .sdl.ts files for me, when I changed the name of the relation, I failed to go back and update that. Going to try that and confirm that’s the missing puzzle piece

Of course that was the issue. For anyone that may run into a similar problem:

in api/src/graphql/organizations.sdl.ts:

export const schema = gql`
  type Organization {
    id: Int!
    name: String!
    loggedInUsers: [User]!
    memberships: [Membership]!
  }
...
1 Like