# What does \_\_typename do in GraphQL?

**URL:** <https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689>\
**Category:** GraphQL\
**Tags:** graphql, kind:question, auth\
**Created:** [September 30, 2020, 9:10am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689 "2020-09-30T09:10:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [September 30, 2020, 9:10am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/1 "2020-09-30T09:10:35Z")

</div>

Hi in every rule in the documentation there is one part that is not clear why it’s there and what it does.  
Can you explain it, please?  
The part that is unclear is the one I between \*\*:

```
query($USER: String!) {
  queryTodo {
    user(filter: { username: { eq: $USER } }) {
      **__typename**
    }
  }
}

```

thanks

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [September 30, 2020, 9:43am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/2 "2020-09-30T09:43:40Z")

</div>

Are you trying to return something that implements an interface?

~~The `__typename` field is used to query a `dgraph.type` I believe~~. @pawan can doublecheck me?

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [September 30, 2020, 9:48am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/3 "2020-09-30T09:48:54Z")

</div>

It is an example from your [blog](https://dgraph.io/blog/post/slash-intro-to-auth/?utm_campaign=Slash%20Welcome%20Journey&utm_medium=email&_hsmi=96230154&_hsenc=p2ANqtz-9QASLsa_cdR074St6W0_V4epQH7nKlntUHYk2tRUA7O-oN8jBZJxmj15VLoN4nAMa1Q6ppP_lusEdxA88hyak0738uVA&utm_content=96230154&utm_source=hs_automation).

There is another example from the [docs](https://dgraph.io/docs/graphql/authorization/directive/#authorization-rules)

```
    query ($USER: String!) { 
        queryTodo(filter: { owner: { eq: $USER } } ) { 
           **id**
        } 
    }

```

Here the id is return instead of \_\_typename  
But what’s the meaning of it or of the returned \_\_typename

---

<div class="post-metadata">

**Author:** ![chewxy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/chewxy/32/4339_2.png) [@chewxy](https://discuss.dgraph.io/u/chewxy)\
**Post date:** [September 30, 2020, 10:05am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/4 "2020-09-30T10:05:03Z")

</div>

Thanks to @pawan - `__typename` returns the GraphQL type name of the node - which might not be the same as `dgraph.type`.

---

<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:** [September 30, 2020, 10:18am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/5 "2020-09-30T10:18:58Z")

</div>

You have pasted an example of an Auth rule on a type which applies a filter on the `user`. Now to find out whether any nodes passed the auth rule, you need to request some child field for the user. We would ultimately check if any Todos were returned from the auth rule and use that as the result of the query.

The field could be `__typename` or `id` as both the fields are always there for any object of a type, hence we use both of them interchangeably.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [September 30, 2020, 11:37am UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/6 "2020-09-30T11:37:02Z")

</div>

So this field in the auth rule will return for any query that is made on the type?  
let’s continue with your example in the documentation:

```auto
query {
    queryTodo(filter: { text: { anyofterms: "graphql"}}, first: 10, order: { asc: text }) {
        id
        text
    }
}

```

Can I remove the id field from here and it will return because of the auth rule?

---

<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:** [September 30, 2020, 12:06pm UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/7 "2020-09-30T12:06:48Z")

</div>

> [@spinelsun](#):
>
> Can I remove the id field from here and it will return because of the auth rule?

No, you still need the id in this query. A GraphQL query would only return the fields that are requested in it and not more.

You request for `id` or `__typename` in your Auth rule so that it returns some result. Think of it like this, we `AND` the result of your GraphQL query with the Auth rule to get the starting set of uids. So if either your GraphQL query or your Auth query didn’t return anything, the result would be empty.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [September 30, 2020, 12:08pm UTC](https://discuss.dgraph.io/t/what-does-typename-do-in-graphql/10689/8 "2020-09-30T12:08:46Z")

</div>

ok got it thanks
