GraphQL Query issue with result array


#1

Hi

Need some advice on a query issue we’re having. We’re experimenting with Realm for a couple of projects (mobile and web) and have an issue with a query. Not sure if it’s our model or how we’re trying to query. For this experiment I’ve created a very basic social media model (as below)…

exports.Blog = {
  name: 'Blog',
  primaryKey: 'uuid',
  properties: {
    uuid: 'string',
    Publisher: 'User',
    Title: 'string',
    Story: 'string',
    Image: 'string',
    Deleted: 'bool'
  }
}

exports.Follower = {
  name: 'Follower',
  primaryKey: 'uuid',
  properties: {
    uuid: 'string',
    User: 'User',
    Blocked: 'bool'
  }
}

exports.User = {
  name: 'User',
  primaryKey: 'uuid',
  properties: {
    uuid: 'string',
    Nickname: 'string',
    Followers: 'Follower[]',
    Blogs: 'Blog[]'
  }
}

and want a query (as below) to return all blogs where the current user is a follower of the publisher unless the current user (Publisher.Followers.User.uuid = currentUser) is blocked by the publisher… Note we do not just want to delete the follower on block as we’d like the user to have visibility of their blocked users.

query getBlogs {
  blogs (query:"Publisher.Followers.User.uuid = 'd6b5fb73-6270-4f52-8e34-f3d544dc0736' && Publisher.Followers.Blocked = false") {
    uuid
    Title
    Story
    Image
    Deleted
    Publisher {
      uuid
      Nickname
      Followers {
        uuid
        Blocked
        User {
          uuid
          Nickname
        }
      }
    }
  }
}

when I run this query I’m getting the result as below. Note User3 is is blocked and the current user in this example.

{
  "data": {
    "blogs": [
      {
        "uuid": "ecb691cc-f809-4e74-bf68-ea4e33be5e1a",
        "Title": "Blog",
        "Story": "bla bla blah..",
        "Image": "",
        "Deleted": false,
        "Publisher": {
          "uuid": "2ec2e71b-6528-4b58-97c8-87a36241d447",
          "Nickname": "User1",
          "Followers": [
            {
              "uuid": "873eaaeb-4054-4d6b-88e1-dd067faca99e",
              "Blocked": false,
              "User": {
                "uuid": "17c26250-e5ff-4643-b00f-434b292f8dbf",
                "Nickname": "User2"
              }
            },
            {
              "uuid": "87f444a7-8777-4dc0-9d09-a149c85b9542",
              "Blocked": true,
              "User": {
                "uuid": "d6b5fb73-6270-4f52-8e34-f3d544dc0736",
                "Nickname": "User3"
              }
            }
          ]
        }
      }
    ]
  }
}

It seems we’re unable to query / filter by anything within the Followers node. As stated I’m new to Realm and GraphQL so may be something daft but appreciate any advice. Thanks


#2

The query you’re using translates roughly to: Give me all blogs, whose Publisher has at least one follower that matches this Id and whose Publisher has at least one follower who is not blocked. You see, the key here is that those conditions are not evaluated together - i.e. the blocked and id checks don’t necessarily apply to the same user, which is why you’re getting that result. I’ll go ahead and summon our query expert to see if he can construct a query that satisfies your requirements, but in the meantime, you have to be aware that if the GraphQL endpoint is exposed to end users, a malicious actor can easily modify the request, thus reading blog posts from publishers that have blocked them.


#3

I got a response back. Essentially, it seems like you need to run a subquery - the syntax should be something along the lines of: SUBQUERY(Publisher.Followers, $x, $x.User.uuid = ‘d6b5fb73-6270-4f52-8e34-f3d544dc0736’ && $x.Blocked = false)[email protected] > 0. We have (admittedly limited) docs on the subquery feature here and here.