Search code examples
graphqlreact-apollographql-compose-mongoose

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


I'm new to all graphql world, so this might be a very easy question, sorry

I'm using graphql-compose-mongoose to generate my graphql schema, here's my mongoose schema:

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)

If I attempt the following mutation in graphiql it works fine

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

}

and returns this (in case there could be helpful to see the response)

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

Now if I try to pass the attachments to apollo, I don't know how to do that, I don't know which type to provide (Attachment is not the right type 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
            }
        }
  }`

So searching for the right type, I did a introspection of my object, the issue is that I get the type of attachment as null for this query:

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

this is the response:

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

googling didn't help since I don't know how is this operation called, I don't think it's a nested mutation from what I found...


Solution

  • Ok fixed,

    I did these steps:

    I first introspected the type of attachment in a regular query using the __typename keyword: as follows

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

    it showed up a type named ComplaintAttachments

    when replacing the Attachment type with this new value, ComplaintAttachments, an error occured and that error message helped me out:

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

    so the array is of type ComplaintComplaintAttachmentsInput, I still don't know how to introspect it directly, but I'm already happy with the result :)