OneToMany 关系在 Prisma 数据库上工作,但不在服务器上
Posted
技术标签:
【中文标题】OneToMany 关系在 Prisma 数据库上工作,但不在服务器上【英文标题】:OneToMany relation working on Prisma Database, but not on server 【发布时间】:2019-08-12 10:37:55 【问题描述】:在创建帖子的突变中,我正在使用以下代码:
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 了解了这一点。
但是,当我控制台记录 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!
【问题讨论】:
【参考方案1】:您需要将info
作为第二个参数传递给Prisma 绑定的createPost
函数。
有可能author
字段正在从客户端查询以响应savePost
突变,但由于没有第二个参数传递给createPost
,prisma 仅返回标量字段。由于author
字段在Post
类型中是按要求定义的,因此如果从客户端查询author
但解析器未返回,则GraphQL 验证将失败。
改变这个:
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)
【讨论】:
【参考方案2】:据我所知,如果不指定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
,您可能需要再次生成它。
【讨论】:
类型帖子在生成的 prisma.graphql 中 每次我创建帖子时,都会创建帖子,但是会报错:Error: Cannot return null for non-nullable field Post.author.
以上是关于OneToMany 关系在 Prisma 数据库上工作,但不在服务器上的主要内容,如果未能解决你的问题,请参考以下文章
Symfony 3 - Doctrine 2 - 在 oneToMany 关系上的 orderBy 不起作用