0

在创建帖子的突变中,我正在使用以下代码:

async function savePost(parent, args, context, info){

  let data = {...args}
  delete data.categories
  delete data.status

  if (!context.request.userId) {
    throw new Error('Please SignIn to continue.')
  }

  const post = await context.prisma.createPost({
    author: {
      connect: { id: context.request.userId }
    },
    categories: {
      set: args.categories,
    },
    status: args.status,
    ...data
  })

  return post

}

这会将帖子连接到数据库中的作者。我通过使用 Database Playground 了解了这一点。

但是,当我 console.log postToUpdate 时,它​​返回`author:null!当我在客户端使用 gql 时,也会发生同样的事情。帖子没有作者!

尽管作者已保存在数据库中,但我在客户端和服务器端都看不到他/她。

作为参考,这是数据模型

enum Previledge {
  SUPERADMIN
  ADMIN
  MODERATOR
  AUTHOR
  READER
}

enum Category {
  TECH
  FIN
  DIGIMARK
  CODING
  TUTORIAL
  HOWTO
  WRITING
  INSPIRE
  SCIENCE
  POLITICS
  LIFESTYLE
  FOOD
  BUSINESS
  ENTREPRENEUR
  HISTORY
  HEALTH
  PET
  PARENTHOOD
  TRAVEL
  INDIA
  CHINA
  US
  UK
  WORLD
  NEWS
  REVIEW
}

enum PostStatus {
  PUBLISHED
  DRAFT
  DELETED
}

type Post {
  id: ID! @unique
  title: String!
  editorSerializedOutput: Json!
  editorCurrentContent: Json!
  editorHtml: String!
  updatedAt: DateTime!
  createdAt: DateTime!
  author: User! @relation(name: "PostsAndUser")
  categories: [Category]!
  thumbnail: Json!
  status: PostStatus!
}

type User {
  id: ID! @unique
  socialId: String! @unique
  fname: String!
  lname: String!
  name: String!
  phone: String @unique
  email: String! @unique
  gender: String
  birthday: String
  bio: String
  posts: [Post]! @relation(name: "PostsAndUser")
  profilePicture: String!
  followers: [User]!
  previledge: [Previledge]!
  signUpMethod: String!
  accessToken: String!
  updatedAt: DateTime!
  createdAt: DateTime!
}

这是整个架构:

# import * from './generated/prisma.graphql'

scalar DateTime

type Message {
  code: Int
  message: String
}

type Mutation {
  signIn(
    socialId: String!
    fname: String!
    lname: String!
    name: String!
    phone: String
    email: String!
    gender: String
    birthday: String
    bio: String
    profilePicture: String!
    signUpMethod: String!
    accessToken: String!
  ): User!
  signOut: Message
  savePost(
    title: String!
    editorSerializedOutput: Json!
    editorCurrentContent: Json!
    editorHtml: String!
    categories: [Category]!
    thumbnail: Json!
    status: PostStatus!
  ): Post!
}

type Query {
  users: [User]!
  me: User
  canUpdatePost(id: ID!): Post
}

type User {
  id: ID!
  fname: String!
  lname: String!
  name: String!
  phone: String
  email: String!
  gender: String
  birthday: String
  bio: String
  posts(where: PostWhereInput, orderBy: PostOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [Post!]
  profilePicture: String!
  followers(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User!]
  previledge: [Previledge]!
  signUpMethod: String!
  updatedAt: String!
  createdAt: String!
}
4

2 回答 2

1

您需要info作为第二个参数传递给createPostPrisma 绑定函数。

有可能author从客户端查询字段以响应savePost突变,但由于没有传递给的第二个参数createPost,prisma 仅返回标量字段。由于author字段在类型中定义为要求,如果从客户端查询但解析器未返回Post,则 GraphQL 验证将失败。author

改变这个:


const post = await context.prisma.createPost({
    author: {
      connect: { id: context.request.userId }
    },
    categories: {
      set: args.categories,
    },
    status: args.status,
    ...data
  })

至:

const post = await context.prisma.createPost({
    author: {
      connect: { id: context.request.userId }
    },
    categories: {
      set: args.categories,
    },
    status: args.status,
    ...data
  }, info)

于 2019-03-22T09:44:22.223 回答
0

据我所知,如果不指定 的第二个参数createPost,它只会返回标量字段。尝试:

  const post = await context.prisma.createPost({
    author: {
      connect: { id: context.request.userId }
    },
    categories: {
      set: args.categories,
    },
    status: args.status,
    ...data
  }, `{
   author {
     id
   }
  }`)

  console.log(post)

此外,Post您的模式中似乎没有类型,您可能需要再次生成它。

于 2019-03-21T18:44:36.023 回答