Realm | GraphQL Schema | How to format/create/define relationships inside nested objects and arrays?

I have the following data in a collection called “projects”:

{
     _id: "project-id",
    title: "Project Title",
    credits: [
        {
            personId: "person-id"
            roles: ["writer"]
        }
    ]
}

I have the following collection called “people”:

{
    _id: "person-id"'
    name: "John Doe"
}

Is there a way to create a “Relationship” in Realm for GraphQL that is nested like this? Normally I would write some kind of foreign key relationship like this in the Realm UI under Realm > Schema > Relationships:

{
  "credits": {
    "foreign_key": "_id",
    "ref": "#/relationship/mongodb-atlas/burns_media/people",
    "is_list": true
  }
}

However, since “personId” is contained in an object inside an array, I’m not sure how to define the nested relationship. Is this possible?

2 Likes

Hey Brian -

At the moment we don’t support relationship linking between fields in nested objects in arrays but you can request the feature here so we can track requests for future prioritization Realm: Top (70 ideas) – MongoDB Feedback Engine

As a workaround, you can achieve the same thing by nesting the personId in the projects collection (as you’re already doing) and writing a custom resolver that will query the people collection using the personIds that is returned from the projects collection.

Let me know if you have further questions

Perfect answer! Thank you so much @Sumedha_Mehta1! :slight_smile:

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