LinkingObjects and GraphQL

I have my data in RealmSwift structured like:

 class Dog {
    owner: Person 
  }

   class Person {
      var dogs: LinkingObjects(Dog.self, "owner")
    }

If I perform the following GraphQL query on my data I get unexpected results:

query {
   persons {
      dogs
  }
}

The result I am getting looks liek this;

[{
  name : "Person 1"
  dogs: []
},
{
  name : "Person 2"
   dogs: []
 }]

Even though several dogs point at Person1/Person2. Is there anything I am missing in my query to populate fields?
Also another question about the graphql endpoint: can I access it from my realm functions or is there another way to have a “stored query” taking an argument and delivering results to clients?

Hi Christian :wave:

Is there anything I am missing in my query to populate fields?

GraphQL currently doesn’t support LinkingObjects so unfortunately you aren’t missing anything. LinkingObjects are a Realm Database concept that aren’t represented in your server-side JSON Schema or relationships, so you can’t query them directly like this in GraphQL.

is there another way to have a “stored query” taking an argument and delivering results to clients

You can use a custom resolver to define your own queries/mutations or a computed property on a generated type.

You could define custom resolvers to recreate the LinkingObjects fields in GraphQL. This should work fine, just know that you’ll need to manually look up the relationship in the resolver function + define a custom resolver for every LinkingObjects field.

1 Like

Thanks @nlarew, even though if this is not was I was hoping for. One more question closely related to that: can I also do graphQL queries from within a realm function, using a graphql service? I was not able to do it but it would greatly help with creating these stored queries.

Realm functions don’t currently include a built-in GraphQL client but that’s definitely a good idea! I just searched on the official MongoDB Realm feedback site and didn’t see any posts that match your ideas. If you want to see support for LinkingObjects in GraphQL and/or a native GraphQL client in functions, I highly encourage you to create a couple of posts on the feedback site so that other devs can upvote them (I will too!). I can tell you that the product/engineering teams actively check those posts and use them to help prioritize work, so that’s your best bet in the longer term.

Short term you have a couple of options to work with data in functions. Either:

Hopefully one of these approaches works for you!

1 Like

Thanks @nlarew. At this point I think GraphQL does not add value to my purpose (retrieving a multi-level data hierachy quickly) so I decided to implement my stored query purely with mongodb queries in realm functions to avoid the pain of integrating a GraphQL client into functions (I have negative experience with manually setting up GraphQL connections).

Feel free to checkout (and upvote if you like :smiley: ) the feedback I created here and there.

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