# GraphQL Fragments generates unexpected behaviour

**URL:** <https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470>\
**Category:** GraphQL\
**Tags:** kind:bug\
**Created:** [July 1, 2020, 3:21pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470 "2020-07-01T15:21:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luscha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/luscha/32/3588_2.png) [@Luscha](https://discuss.dgraph.io/u/Luscha)\
**Post date:** [July 1, 2020, 3:21pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/1 "2020-07-01T15:21:05Z")

</div>

I have a schema like:

```auto
interface WithStaff {
  staff: [Collaboration!]!
}
interface Metadata {
  id: String! @id @search(by: [hash])
  createdAt: DateTime!
  modifiedAt: DateTime!
  generation: Int!
  version: Int!
}
interface Collaborator {
  collaborations: [Collaboration!]!
}

type Anime implements Metadata & WithStaff {
  name: String
}

type Collaboration implements Metadata {
  """
  Organization or Person
  """
  collaborator: Collaborator! @hasInverse(field: collaborations)
  """
  Episode, Anime, Manga, Volume, Chapter, ...
  """
  content: WithStaff! @hasInverse(field: staff)
}

type Organization implements Metadata & Collaborator {
  foundation: Date
  name: String
}

type Person implements Metadata & Collaborator {
   name: String
}

```

if I run the query

```auto
query {
    queryAnime(filter: {id: {eq: "AS0N2fOigo8X-ViL"}}) {
            staff {
                collaborator {
                    __typename
                    ... on Organization {
                        id
                    }
                }
            }
        }
    }

```

I expect to get all the organization involved with this anime (reading the GraphQL doc when a fragment is specified, it returns the fields inside of the fragment only for the entities which are of the fragment type)  
Instead i get

```auto
{
  "data": {
    "queryAnime": [
      {
        "staff": [
          {
            "collaborator": {
              "__typename": "Organization",
              "id": "AS0vZTJ78C7wLIb3"
            }
          },
          {
            "collaborator": {
              "__typename": "Organization",
              "id": "AS0i6UMuZ7Fd2BqJ"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0MqA6d8p7Rwpq_"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0CDrNrC4LIUsJZ"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0ZAJZC77Rys1gU"
            }
          },
          {
            "collaborator": {
              "__typename": "Organization",
              "id": "AS0OqJKJ4lTUKSrs"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0Q7qCjHlmjlJ8K"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0_89Yzr8G8lIZk"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0_89Yzr8G8lIZk"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0USJ2_3ePCXWZ9"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS05buZfC262v7AQ"
            }
          },
          {
            "collaborator": {
              "__typename": "Person",
              "id": "AS0R2qEUF3PSQEh2"
            }
          }
        ]
      }
    ]
  },
  "extensions": {
    "touched_uids": 60
  }
}

```

Wich is a mix between all the entities, regardless the type.  
I think it’s a bug derived from the fact that both Organization and Person implement the Metadata interface and both have the id field. I’m not an ace with GraphQL, but i feel like the objects returned should depend on the **fragment** declaration and not if the **underlying entity satisfies or not the fields inside the fragment itself**

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 1, 2020, 4:33pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/2 "2020-07-01T16:33:38Z")

</div>

> [@Luscha](#):
>
> `type Anime implements Metadata & WithStaff {`

This was new to me. I did not know that you could implement on more than a single interface at a time. Thx!

> [@Luscha](#):
>
> `... on Organization {`

it seems like this is completely ignored. This is another new syntax to me though. According the the [Apollo GraphQL docs](https://graphql.org/learn/queries/#inline-fragments) this should yield the results you expect.

To drive your point home with another example for the devs, in the Apollo GraphQL docs playground from the link above. If you provide this query:

```auto
query HeroForEpisode($ep: Episode!) {
  hero(episode: $ep) {
    __typename
    ... on Human {
      id
    }
  }
}

```

with variables:

```auto
{
  "ep": "JEDI"
}

```

It yields the results:

```auto
{
  "data": {
    "hero": {
      "__typename": "Droid"
    }
  }
}

```

If you will notice that the typename is shown but the id is not shown because the type is not `Human` even though the `id` is a valid property on the Droid as well.

---

<div class="post-metadata">

**Author:** ![Luscha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/luscha/32/3588_2.png) [@Luscha](https://discuss.dgraph.io/u/Luscha)\
**Post date:** [July 1, 2020, 4:50pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/3 "2020-07-01T16:50:02Z")

</div>

Yw 🙂 you should also be able to implement multiple interfaces in an interface

```auto
interface Anime implements Metadata & WithStaff {

```

but is not yet implemented in dgraph… But that’s another story

About the fragment, I was referring to that guide, that’s why I would expect the fragment to “filter” the entities

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 1, 2020, 4:54pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/4 "2020-07-01T16:54:45Z")

</div>

As a side note, interfaces and types that implement interfaces do not yet support the @auth directive. Might want to keep that in mind if you are planning on going down that route like I am.

> auth is supported an on types, but interfaces (and the types that implement them) don’t correctly support auth in the current beta.  
> [https://graphql.dgraph.io/authorization/](https://graphql.dgraph.io/authorization/)

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [July 2, 2020, 12:13pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/5 "2020-07-02T12:13:48Z")

</div>

This looks like a bug. We are going to look into this. Thanks for reporting it.

---

<div class="post-metadata">

**Author:** ![michaelcompton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/michaelcompton/32/1774_2.png) [@michaelcompton](https://discuss.dgraph.io/u/michaelcompton)\
**Post date:** [July 2, 2020, 1:11pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/6 "2020-07-02T13:11:07Z")

</div>

> [@amaster507](#):
>
> As a side note, interfaces and types that implement interfaces do not yet support the @auth directive. Might want to keep that in mind if you are planning on going down that route like I am.

Yeah, that’s right. Interfaces are a bit of rabbit hole.

For the 20.07.0 release, auth will work on all types, but not at all on interfaces. That quote from the docs needs a little updating. I think the situation for 20.07.0 will be that auth is implemented on all types, for queries and mutations, including when the type implements an interface, but not on the interface itself.

So for example if types `TheTypeA` and `TheTypeB` implements interface `TheInterface`, then you can put auth on both `TheTypeA` and `TheTypeB`, but not at all on `TheInterface`, and I think if we find auth on the concrete types, we’ll remove the queries and mutations for the interface because they aren’t protected by the auth.

The auth moving up to the interfaces is one of the first roadmap items for 20.11.0

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 2, 2020, 1:29pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/7 "2020-07-02T13:29:29Z")

</div>

> [@michaelcompton](#):
>
> I think the situation for 20.07.0 will be that auth is implemented on all types, for queries and mutations, including when the type implements an interface, but not on the interface itself.

That would be perfect for my use case!

---

<div class="post-metadata">

**Author:** ![Luscha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/luscha/32/3588_2.png) [@Luscha](https://discuss.dgraph.io/u/Luscha)\
**Post date:** [July 2, 2020, 1:38pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/8 "2020-07-02T13:38:55Z")

</div>

should I open a ticket on github?

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [July 3, 2020, 11:41am UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/9 "2020-07-03T11:41:33Z")

</div>

You don’t need to for now. This should be simple enough to fix without a GitHub issue. We are looking into it and will have a fix for you next week.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [August 12, 2020, 3:01pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/10 "2020-08-12T15:01:59Z")

</div>

Did this ever get resolved? Just clarifying to whether or not it will continue to cause bugs

---

<div class="post-metadata">

**Author:** ![Luscha](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/luscha/32/3588_2.png) [@Luscha](https://discuss.dgraph.io/u/Luscha)\
**Post date:** [August 12, 2020, 3:09pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/11 "2020-08-12T15:09:02Z")

</div>

No, the behaviour is still present in master release.  
Whatever type resolves the body of the fragment, get returned even if it is not the right type

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [August 12, 2020, 3:18pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/12 "2020-08-12T15:18:27Z")

</div>

@pawan, any ETA?

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [August 12, 2020, 3:33pm UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/13 "2020-08-12T15:33:35Z")

</div>

@abhimanyusinghgaur is currently working on this. I expect it to be resolved in a week.

---

<div class="post-metadata">

**Author:** ![abhimanyusinghgaur](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhimanyusinghgaur/32/2980_2.png) [@abhimanyusinghgaur](https://discuss.dgraph.io/u/abhimanyusinghgaur)\
**Post date:** [August 21, 2020, 11:37am UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/14 "2020-08-21T11:37:30Z")

</div>

Hi @Luscha @amaster507  
It has been fixed with [this PR](https://github.com/dgraph-io/dgraph/pull/6228) and is available in master. Will be part of release `v20.7.2`.

---

<div class="post-metadata">

**Author:** ![abhimanyusinghgaur](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/abhimanyusinghgaur/32/2980_2.png) [@abhimanyusinghgaur](https://discuss.dgraph.io/u/abhimanyusinghgaur)\
**Post date:** [August 21, 2020, 11:39am UTC](https://discuss.dgraph.io/t/graphql-fragments-generates-unexpected-behaviour/7470/15 "2020-08-21T11:39:28Z")

</div>


