What does __typename do in GraphQL?

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 } }) {


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?

It is an example from your blog.

There is another example from the docs

    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

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

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.

1 Like

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:

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

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.

ok got it thanks