Graphql 普里斯玛:场。。。未在…中定义。。。输入类型或类型。。。应为,但已提交非对象
我用的是Prisma 1.34。fro API开发。通过localhost操场测试它。 抱歉提前发了这么长的短信,我不明白我哪里出错了 我有以下表示层次结构的方案脚本模板由卡片模板组成,卡片包括任务模板:Graphql 普里斯玛:场。。。未在…中定义。。。输入类型或类型。。。应为,但已提交非对象,graphql,graphql-js,prisma,prisma-graphql,Graphql,Graphql Js,Prisma,Prisma Graphql,我用的是Prisma 1.34。fro API开发。通过localhost操场测试它。 抱歉提前发了这么长的短信,我不明白我哪里出错了 我有以下表示层次结构的方案脚本模板由卡片模板组成,卡片包括任务模板: type ScriptTemplate { id: ID! name: String! cards: [CardTemplate!] input: String! output: String! } type CardTemplate { id: ID! tit
type ScriptTemplate {
id: ID!
name: String!
cards: [CardTemplate!]
input: String!
output: String!
}
type CardTemplate {
id: ID!
title: String!
description: String
tasks: [TaskTemplate!]
}
input ExistingCardTemplateInput {
id: ID!
}
input NewCardTemplateInput {
title: String!
description: String!
tasks: [NewTaskTemplateInput!]
}
type TaskTemplate {
id: ID!
label: String!
description: String!
cards: [CardTemplate!]
}
input ExistingTaskTemplateInput {
id: ID!
}
input NewTaskTemplateInput {
label: String!
description: String!
}
相应的突变是:
type Mutation {
createScriptTemplate(name: String!, input: String!, output: String!, cards: [ExistingCardTemplateInput!], new_cards: [NewCardTemplateInput!]): ScriptTemplate
createCardTemplate(title: String!, description: String! tasks:[ExistingTaskTemplateInput!], new_tasks:[NewTaskTemplateInput!]): CardTemplate
createTaskTemplate(label: String!, description: String! cards:[ExistingCardTemplateInput!]): TaskTemplate
}
所以基本上,如果我尝试使用createTaskTemplate
mutation或createCardTemplate
mutation,一切都正常。我可以创建这些实体,包括创建包含新任务的新卡或绑定现有任务。或将现有卡添加到新创建的任务。这就是明确定义输入类型的原因:ExistingTaskTemplateInput
、NewTaskTemplateInput
和NewCardTemplateInput
。
当我尝试创建一个包含新卡的新脚本或将其连接到现有卡时,一切都按预期进行。
然而,若我试图创建脚本、卡片并在其中包含新任务,我会在上面看到错误消息
{
"data": {
"createScriptTemplate": null
},
"errors": [
{
"message": "Variable '$data' expected value of type 'ScriptTemplateCreateInput!' but got:
{\"name\":\"First Script through API_H2\",\"input\":\"Something describing initial state\",\"output\":\"Something describing requred state at the end\",\"cards\":{\"connect\":[{\"id\":\"cjycl2nup00ta0703sd0kd8oa\"},{\"id\":\"cjye3ryee01ey070383sxaoxz\"}],\"create\":[{\"title\":\"New card via scriptis2\",\"description\":\"desc\",\"tasks\":[{\"label\":\"test label\",\"description\":\"test dewscription\"}]},{\"title\":\"New card through scriptos2\",\"description\":\"desc\"}]}}.
Reason: 'cards.create[0].tasks'
Expected 'TaskTemplateCreateManyWithoutCardsInput', found not an object. (line 1, column 11):\nmutation ($data: ScriptTemplateCreateInput!) {\n ^",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createScriptTemplate"
]
}
]
}
实际请求如下所示(通过console.log):
看起来我缺少任务
字段的{connect或create}位。
但是,当我将其更改为:
tasks: {create: [
{
description: "test dewscription"
label: "test label"
}
]
}
我得到的错误是,字段\“create\”不是由类型NewTaskTemplateInput定义的
和字段NewTaskTemplateInput.label和所需类型字符串的描述!未提供
input TaskTemplateCreateManyWithoutCardsInput {
create: [TaskTemplateCreateWithoutCardsInput!]
connect: [TaskTemplateWhereUniqueInput!]
}
input TaskTemplateCreateWithoutCardsInput {
id: ID
label: String!
description: String!
}
看起来我混淆了我在gql文件中定义的方案和我再次执行请求的方案,但不知道该往哪个方向走。Description
首先概述请求的整个过程
上图取自Prisma官方文档中的Prisma Basic
如图所示,localhost playway
是图中的客户端
。由localhost playway
访问的服务器
是图中的API服务器
,即Prisma客户端
。这是第一个连接,图中的绿色箭头。
Prisma客户端
通过访问Prisma服务器
从数据库获取数据。这是第二个连接,图中的黄色箭头
数据流处理,从localhost
发送到API服务器
完成第一次连接传输,然后由resolve
处理,使用Prisma客户端
发送到Prisma服务器
完成第二次连接传输Prisma服务器
根据请求的数据从数据库
检索数据,并将其返回到API服务器
,完成第二次连接验收<代码>API服务器将数据返回到本地主机
,完成对第一个连接的接受。一个完整的过程完成了
在使用Prisma
的过程中,我们定义了两个模式,一个用于定义Prisma服务器
,名称通常是datamodel.Prisma
。另一个用于定义Prisma客户端
,如果它是在文件中定义的,通常命名为schema.graphql
,也可以通过模板字符串在JS文件中定义,还有其他方式,这里不作深入讨论。您显示的内容属于客户机架构
成功完成此过程需要每个部件发送的数据正确无误。
发送第二个连接时发生第一个错误。从API服务器
发送的数据与Prisma服务器
中的架构定义不匹配。
第二个错误是,当发送第一个连接时,从localhost
发送的数据与API服务器中的架构定义不匹配
例子
假设以下架构
# datamodel.prisma
type User {
id: @id!
name: String!
post: [Post!]!
}
type Post {
id: @id!
title: String!
}
使用以下命令创建用户
mutation {
createUser(name: "prisma", posts: [{ title: "test" }]) {
id
name
}
}
从localhost
发送到API服务器
时,数据如下
{
"name": "prisma",
"posts": [{ "title": "test" }]
}
如果将此数据直接发送到Prisma服务器
,则不符合Prisma服务器
的定义。您需要将数据转换为符合Prisma服务器
模式的定义
{
"name": "prisma",
"posts": { "create": [{ "title": "test" }] }
}
然后通过Prisma客户端将其发送到Prisma服务器
const resolve={
突变:{
createUser:async(\uz,args)=>{
常量userCreateInput={
名称:args.name,
职位:{
创建:args.posts,
},
}
返回wait prisma.createUser(userCreateInput)
},
},
}
这样,最终数据通过Prisma服务器
到达数据库
解决问题
查看API Server
中createScriptTemplate
的定义。应该是它没有将接收到的数据转换为由API Server
所需格式引起的错误
我认为您必须在任务
中使用创建
,使用标签和说明,或者使用连接
仅使用id
。这里,您使用带有标签和说明且没有id的connect
。@Errorname对不起,我的错误示例。“create”的结果是否相同并不重要。我无法理解的是,我将任务的输入类型显式定义为[NewTaskTemplateInput!]
。为什么Prisma会在某一时刻根据此类型检查它,但同时根据TaskTemplateCreateWithout CardsInput
检查它?
# schema.graphql
input CreatePost {
title: String!
}
type Mutation {
createUser(name: String!,posts:[CreatePost])
}
mutation {
createUser(name: "prisma", posts: [{ title: "test" }]) {
id
name
}
}
{
"name": "prisma",
"posts": [{ "title": "test" }]
}
{
"name": "prisma",
"posts": { "create": [{ "title": "test" }] }
}