【发布时间】:2019-11-30 11:47:40
【问题描述】:
我正在使用 Prisma 1.34。来自 API 开发。通过 localhost 游乐场对其进行测试。 抱歉,提前写了很长的文字,不明白我哪里出错了。
我有以下表示层次结构的方案脚本模板由卡片模板组成,卡片包括任务模板:
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 突变或 createCardTemplate 突变 - 一切正常。我可以创建这些实体,包括使用其中的新任务创建新卡的嵌套突变或绑定现有任务。或现有卡到新创建的任务。这就是明确定义输入类型的原因:ExistingTaskTemplateInput、NewTaskTemplateInput 和 NewCardTemplateInput。
当我尝试创建包含新卡或将其连接到现有卡的新脚本时,一切都按预期工作。
但是,如果我尝试创建脚本、卡片并在其中包含新任务,我会收到上面的错误消息。
- 尝试以下突变时:
mutation{
createScriptTemplate(
name: "Script via API_H2"
input: "Something describing initial state"
output: "Something describing required state at the end"
cards: [
{
id: "cjycl2nup00ta0703sd0kd8oa"
},
{
id: "cjye3ryee01ey070383sxaoxz"
}
]
new_cards:[
{
title:"New card via scriptis2"
description:"desc"
tasks: [
{
description: "test dewscription"
label: "test label"
}
]
},
{
title:"New card through scriptos2"
description: "desc"
}
]
){
id
name
input
output
createdAt
updatedAt
cards{
id
title
tasks{
id
label
}
}
}
}
我有错误:
{
"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):
{ name: 'First Script through API_H2',
input: 'Something describing initial state',
output: 'Something describing requred state at the end',
cards:
{ connect:
[ [Object: null prototype] { id: 'cjycl2nup00ta0703sd0kd8oa' },
[Object: null prototype] { id: 'cjye3ryee01ey070383sxaoxz' } ],
create:
[ [Object: null prototype] {
title: 'New card via scriptis2',
description: 'desc',
tasks:
[ [Object: null prototype] { label: 'test label', description: 'test dewscription' } ] },
[Object: null prototype] { title: 'New card through scriptos2', description: 'desc' } ] } }
看起来我缺少 tasks 字段的 {connect or create} 位。
但是,当我将其更改为:
tasks: {create: [
{
description: "test dewscription"
label: "test label"
}
]
}
Field \"create\" is not defined by type NewTaskTemplateInput 和 Field NewTaskTemplateInput.label and description of required type String! was not provided 出现错误
- 但是,这完全可以正常工作(没有任务的相同请求):
mutation{
createScriptTemplate(
name: "Script via API_H2"
input: "Something describing initial state"
output: "Something describing required state at the end"
cards: [
{
id: "cjycl2nup00ta0703sd0kd8oa"
},
{
id: "cjye3ryee01ey070383sxaoxz"
}
]
new_cards:[
{
title:"New card via scriptis2"
description:"desc"
},
{
title:"New card through scriptos2"
description: "desc"
}
]
){
id
name
input
output
createdAt
updatedAt
cards{
id
title
tasks{
id
label
}
}
}
}
检查生成的方案,在那里找不到任何问题。
input TaskTemplateCreateManyWithoutCardsInput {
create: [TaskTemplateCreateWithoutCardsInput!]
connect: [TaskTemplateWhereUniqueInput!]
}
input TaskTemplateCreateWithoutCardsInput {
id: ID
label: String!
description: String!
}
看起来我混淆了我在 gql 文件中定义的方案和我再次提出请求的方案,但不知道该往哪个方向发展。
【问题讨论】:
-
我认为您必须在
tasks中使用带有标签和描述的create,或者在connect中仅使用id。在这里,您使用connect带有标签和描述,没有 id。 -
@Errorname 对不起,我的错误示例。与“创建”相同的结果并不重要。我无法理解的是,因为我将
tasks的输入类型明确定义为[NewTaskTemplateInput!]。为什么 Prisma 有时会根据这种类型检查它,但同时会根据TaskTemplateCreateWithoutCardsInput来检查它?
标签: graphql graphql-js prisma prisma-graphql