无法在阿波罗中进行具有 "array of objects" 型属性的突变



我是所有graphql世界的新手,所以这可能是一个非常容易的问题,对不起

我正在使用graphql-compose猫鼬来生成我的graphql模式,这是我的猫鼬模式:

const ComplainSchema = new Schema({
entityId: {type: String, required: true},
user: {type: UserInfoSchema, required: true},
title: String, // standard types
desc: String,
state: {required: true, type: String, enum: ["DRAFT", "MODERATION", "PUBLIC", "SOLVED"]},
attachments: [{
url: {type: String, required: true},
name: String,
mimeType: String,
attachmentId: Schema.Types.ObjectId
}],
createdAt: {type: Date, index: true},
updatedAt: {type: Date, index: true},
}, {timestamps: {}})
export default mongoose.model('Complaint', ComplainSchema)

如果我在graphicql中尝试以下突变,它可以很好地进行

mutation {
complaintUpdateById(record:{_id:"5bdd9350fe144227042e6a20", title:"ok", desc:"updated", attachments:[{name:"zied", url:"http://zied.com"}]}){
recordId, 
record{
_id, 
entityId,
user {
userId,
userName,
roleInShop
},
title,
desc,
createdAt,
updatedAt,
attachments{
name,
url
}
}
}
}

并返回这个(以防有助于查看响应(

{
"data": {
"complaintUpdateById": {
"recordId": "5bdd9350fe144227042e6a20",
"record": {
"_id": "5bdd9350fe144227042e6a20",
"entityId": "5bd9b1858788f51f44ab678a",
"user": {
"userId": "5bd9ac078788f51f44ab6785",
"userName": "Zied Hamdi",
"roleInShop": "ASA"
},
"title": "ok",
"desc": "updated",
"createdAt": "2018-11-03T12:23:44.565Z",
"updatedAt": "2018-11-05T09:02:51.494Z",
"attachments": [
{
"name": "zied",
"url": "http://zied.com"
}
]
}
}
}
}

现在,如果我试图将附件传递给apollo,我不知道如何做到这一点,我也不知道该提供哪种类型(附件不是正确的类型obvisouly(:

const UPDATE_COMPLAINT = gql `mutation complaintUpdateById($_id:MongoID!, $title: String!, $desc: String!, $attachments: [Attachment]
)
{
complaintUpdateById(record:{_id:$_id, title:$title, desc:$desc, attachments:$attachments}){
recordId, 
record{
_id, 
entityId,
user {
userId,
userName,
roleInShop
},
title,
desc,
createdAt,
updatedAt
}
}
}`

因此,在搜索正确的类型时,我对我的对象进行了内省,问题是,对于这个查询,我得到的附件类型为null:

{
__type(name: "Complaint") {
kind
name
fields {
name
description
type {
name
}
}
}
}

这是回应:

{
"data": {
"__type": {
"kind": "OBJECT",
"name": "Complaint",
"fields": [
{
"name": "entityId",
"description": null,
"type": {
"name": "String"
}
},
{
"name": "user",
"description": null,
"type": {
"name": "ComplaintUser"
}
},
{
"name": "title",
"description": null,
"type": {
"name": "String"
}
},
{
"name": "desc",
"description": null,
"type": {
"name": "String"
}
},
{
"name": "state",
"description": null,
"type": {
"name": "EnumComplaintState"
}
},
{
"name": "attachments",
"description": null,
"type": {
"name": null
}
},
{
"name": "createdAt",
"description": null,
"type": {
"name": "Date"
}
},
{
"name": "updatedAt",
"description": null,
"type": {
"name": "Date"
}
},
{
"name": "_id",
"description": null,
"type": {
"name": null
}
}
]
}
}
}

谷歌搜索没有帮助,因为我不知道这个操作是怎么叫的,我不认为这是我发现的嵌套突变。。。

确定已修复,

我做了以下步骤:

我首先使用__typename关键字在一个常规查询中内省了附件的类型:如下

mutation {
complaintUpdateById(record:{_id:"5bdd9350fe144227042e6a20", title:"ok", desc:"updated", attachments:[{name:"zied", url:"http://zied.com"}]}){
recordId, 
record{
_id, 
entityId,
user {
userId,
userName,
roleInShop
},
title,
desc,
createdAt,
updatedAt,
attachments{
__typename,
name,
url
}
}
}
}

它显示了一个名为ComplaintAttachments 的类型

当用这个新值ComplaintAttachments替换附件类型时,出现了一个错误,该错误消息帮助了我:

中使用的类型为"[ComplaintAttachments]"的变量"$attachments"仓位应为类型"[ComplaintComplaintAttachmentsInput]">

所以数组的类型是ComplaintComplaintAttachmentsInput,我仍然不知道如何直接内省它,但我已经对结果感到满意了:(

最新更新