无法在 apollo 中使用 "array of objects" 类型的 属性 进行突变

Unable to do a mutation with a property of type "array of objects" in apollo

我是所有 graphql 世界的新手,所以这可能是一个非常简单的问题,抱歉

我正在使用 graphql-compose-mongoose 生成我的 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)

如果我尝试在 graphiql 中进行以下更改,它会正常工作

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
        }
            }
        }

}

和returns这个(以防看到回复有帮助)

{
  "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,我不知道该怎么做,我不知道要提供哪种类型(附件 不是显然是正确的类型):

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
            }
        }
  }`

所以在搜索正确的类型时,我对我的对象进行了自省,问题是我得到的附件类型为空:

{
  __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 替换附件类型时,发生错误,该错误消息帮助我解决了问题:

Variable "$attachments" of type "[ComplaintAttachments]" used in position expecting type "[ComplaintComplaintAttachmentsInput]"

所以这个数组是ComplaintComplaintAttachmentsInput类型的,我还是不知道怎么直接反省它,但是我已经对结果很满意了:)