Working With MongoDB Embedded Array of Objects in GraphQL

Hello All,

I’m trying to sort out how to manage one-to-one embedded array of objects via GraphQL queries and mutations. The data was modeled in MongoDB for quick reads where possible. The data in the array doesn’t change frequently, but does need to be occasionally modified.

The issue is when trying to insert/update/delete items in the embedded array via GraphQL it’s not possible without re-entering the entire array of objects. I also don’t understand how to query or filter for items in the array. Since the schemas are auto-generated and can’t be modified what’s the best approach/workaround for this?

Thanks!

Here’s an example of what the data looks like in the document:

{
  "_id": {
    "$oid": "555555e0e5bb5555555ff555"
  },
  "version": "1.0",
  "employee": "John Doe",
  "company": "XYZ Company, Inc",
  "startYear": "2021",
  "socialChannels": [
    {
      "position": 0,
      "profileUrl": "http://testing123.com/xyz",
      "channelName": "Twitter"
    },
    {
      "position": 1,
      "profileUrl": "http://testing123.com/xyz",
      "channelName": "Instagram"
    },
    {
      "position": 2,
      "profileUrl": "http://testing123.com/xyz",
      "channelName": "Facebook"
    },
    {
      "position": 3,
      "profileUrl": "http://testing123.com/xyz",
      "channelName": "Snapchat"
    }
  ]
}

At the moment, the best way to achieve this would be to use a GraphQL Custom Resolver and add custom logic in the function. However, this is definitely a feature request we’ve seen before and you can upvote it here - Adding or removing elements from array fields – MongoDB Feedback Engine

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.