Hi Sumedha, thanks! Please let me share the code then.
So let’s say a Thread has many Chats, and a Chat belongs to one Thread.
I defined a schema based on TypeScript Class, by reading this page.
class Chat {
public _id: ObjectId;
public content: string;
public thread: Thread;
public static schema: Realm.ObjectSchema = {
name: "Chat",
primaryKey: "_id",
properties: {
_id: "objectId",
content: "string",
thread: "Thread",
},
};
}
class Thread {
public _id: ObjectId;
public title: string;
public chats: Realm.List<Chat>;
public static schema: Realm.ObjectSchema = {
name: "Thread",
primaryKey: "_id",
properties: {
_id: "objectId",
title: "string",
chats: {
type: 'linkingObjects',
objectType: 'Chat',
property: 'thread'
}
},
};
}
I enabled Sync, and executed realm.write
.
realm.write(() => {
const thread = realm.create("Thread", {
_id: new ObjectId(),
title: "Parent thread",
});
const chat = realm.create("Chat", {
_id: new ObjectId(),
content: "Child chat",
thread: thread,
});
});
Then the chat->thread relationship was successfully generated like a charm!
And So I was able to query GraphQL perfectly as expected.
query{
chats{
content
thread{
title
}
}
}
This responds successfully like:
{
"data": {
"chats": [
{
"content": "Child chat",
"thread": {
"title": "Parent thread"
}
}
]
}
}