在阿波罗

时间:2018-11-05 09:31:59

标签: graphql react-apollo graphql-compose-mongoose

我是所有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
        }
            }
        }

}

并返回此值(以防您看到响应)

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

因此,在搜索正确的类型时,我对对象进行了自省,问题是此查询的附件类型为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
          }
        }
      ]
    }
  }
}

谷歌搜索没有帮助,因为我不知道该操作的调用方式,我不认为这是我发现的嵌套变异...

1 个答案:

答案 0 :(得分:0)

确定,

我执行了以下步骤:

我首先使用__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,我仍然不知道如何直接进行内部检查,但是我对结果很满意:)