简体   繁体   中英

prisma2: how to fetch nested fields?

In prisma 1 I have used fragment to fetch the nested fields.

For example:

const mutations = {
  async createPost(_, args, ctx) {
    const user = await loginChecker(ctx);
    const post = await prisma.post
      .create({
        data: {
          author: {
            connect: {
              id: user.id,
            },
          },
          title: args.title,
          body: args.body,
          published: args.published,
        },
      })
      .$fragment(fragment);

    return post;
  },
};

but seems like in prisma2 it is not supported. because by running this on playground,

mutation CREATEPOST {
  createPost(
    title: "How to sleep?"
    body: "Eat, sleep, repaet"
    published: true
  ) {
    title
    body
    published
    author {
      id
    }
  }
}

I am getting,

"prisma.post.create(...).$fragment is not a function",

The include option is used to eagerly load relations in Prisma 2.

Example from docs:

const result = await prisma.user.findOne({
  where: { id: 1 },
  include: { posts: true },
})

Assuming a user table with a one to many posts relation, this will return back the user object with the posts field as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM