Allow filtering on __typename

It would be great if you could allow filtering on __typename.

Given this schema:

interface Foo {
   v: String!
}

type Bar implements Foo {
   b: String!
}

type Baz implements Foo {
   b: String!
}

type Dataset {
   id: String! @id
   foos: [Foo!]!
}

I wan’t to count all instances of Bar and Baz for a given Dataset efficiently without transferring all instances back to the client. Something like the following would work great if filtering __typename would be allowed:

query GetInstanceCountForDataset($datasetID: String!) {
   barCount: getDataset($id: $datasetID) {
                       aggregateFoo(filter: {__typename: {eq: "Bar"} }) {
                          count
                       }
                    }
   bazCount: getDataset($id: $datasetID) {
                       aggregateFoo(filter: {__typename: {eq: "Baz"} }) {
                          count
                       }
                    }
}

I’m aware of that I could just add a datasetID field to Foo, use aggregateBar and filter for the datasetID. But I want to avoid to have these kind of duplicate information in my database.

1 Like

Hi, has someone had time to have a look here?

Hi @maaft ,

This looks like a valid use case. I am accepting this as a feature request.

We can add a __typename filter to foosAggregate field. This will allow to get count and other aggregates for specific implementing types of interface.

Similar filter won’t be needed for foos field as ... on Baz could be used to filter out Baz.

I hope this satisfies your use case.

3 Likes

I guess yes, this looks like exactly what I asked for! :slight_smile:

That’s true, although currently I’ll get empty dicts in the response for every Foo that is not a Baz. But that might be an unrelated issue:

query {
  queryFoo {
    ... on Baz {
      v
      b
    }
  }
}

"data": {
    "queryFoo": [
      {
        "v": "bla"
        "b": "blub"
      },
      {},
      {},
      {},
      {}
    ]

It would be useful for any filter, like:

query something($name: String!, $type: String!) {
  querySomeInterface(filter: {
    name: $name
    and: {
     __typename: $type
    }
  }) {
    ...
  }
}
1 Like

Hi @denis111 !

You could just use queryImplementation in that case. If you want to “filter” mutliple implementations, you could either do this:

query {
   queryImplementation1(filter: {... }) { ... }
   queryImplementation2(filter: { ... }) { ... }
}

or this:

query {
   querySomeInterface(filter: { ... }) {
      ... on Implementation1 { ... }
      ... on Implementation2 { ... }
   }
}

Thanks @maaft . That’s true but I can’t pass type name as query parameter such way. In DQL I can filter by dgraph.type without a problem.

2 Likes

Is this added in the latest release?

Update: Just tried this and it seems this isn’t available in the current release (v21.03). Correct me if I’m wrong…

2 Likes