Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many-to-many relations generate type not is origin model #250

Open
reaink opened this issue Jan 25, 2022 · 0 comments
Open

Many-to-many relations generate type not is origin model #250

reaink opened this issue Jan 25, 2022 · 0 comments

Comments

@reaink
Copy link

reaink commented Jan 25, 2022

doc: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/many-to-many-relations

schema.prisma

model Post {
  id         Int                 @id @default(autoincrement())
  title      String
  categories CategoriesOnPosts[]
}

model Category {
  id    Int                 @id @default(autoincrement())
  name  String
  posts CategoriesOnPosts[]
}

model CategoriesOnPosts {
  post       Post     @relation(fields: [postId], references: [id])
  postId     Int // relation scalar field (used in the `@relation` attribute above)
  category   Category @relation(fields: [categoryId], references: [id])
  categoryId Int // relation scalar field (used in the `@relation` attribute above)
  assignedAt DateTime @default(now())
  assignedBy String

  @@id([postId, categoryId])
}

post type

import { objectType } from 'nexus'

export const Post = objectType({
  nonNullDefaults: {
    output: true,
    input: false,
  },
  name: 'Post',
  definition(t) {
    t.int('id')
    t.string('title')
    t.list.field('categories', {
      type: 'CategoriesOnPosts',   // this is Category but it is CategoriesOnPosts
      args: {
        where: 'CategoriesOnPostsWhereInput',
        orderBy:
          'CategoriesOnPostsOrderByWithRelationAndSearchRelevanceInput',
        cursor: 'CategoriesOnPostsWhereUniqueInput',
        take: 'Int',
        skip: 'Int',
        distinct: 'CategoriesOnPostsScalarFieldEnum',
      },
      resolve(root: any) {
        return root.categories
      },
    })
  },
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant