Graphql 在Prisma数据库上工作的OneToMany关系,但在服务器上不工作

Graphql 在Prisma数据库上工作的OneToMany关系,但在服务器上不工作,graphql,react-apollo,apollo-server,prisma,prisma-graphql,Graphql,React Apollo,Apollo Server,Prisma,Prisma Graphql,在创建帖子的过程中,我使用以下代码: 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 = awa

在创建帖子的过程中,我使用以下代码:

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

}
这会将文章连接到数据库中的作者。我是通过数据库知道这一点的

但是,当I 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!
}

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

const post=wait context.prisma.createPost{ 作者:{ 连接:{id:context.request.userId} }, 类别:{ 集合:args.categories, }, 状态:args.status, 数据 }, `{ 作者{ 身份证件 } }` 航海日志 此外,模式中似乎没有输入Post,您可能需要再次生成它。

您需要将info作为第二个参数传递给Prisma绑定的createPost函数

有可能是为了响应savePost变异而从客户端查询author字段,但由于没有第二个参数传递给createPost,prisma只返回标量字段。由于author字段在Post类型中定义为required,所以如果从客户端查询author但解析程序未返回,GraphQL验证将失败

更改此项:


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

致:


类型post位于生成的prisma.graphql中每次我创建post时,它都在创建post,但给出了一个错误:错误:对于不可为null的字段post.author,无法返回null。
const post = await context.prisma.createPost({
    author: {
      connect: { id: context.request.userId }
    },
    categories: {
      set: args.categories,
    },
    status: args.status,
    ...data
  }, info)