Custom DQL __typename bug / strange behaviour

You can just add dgraph.type edge in the dql query, and __typename would work as you expect it to. So, this should work:

type Query {
  queryByName(search: String!): [WithName] @custom(dql: """
    query q($search: string) {
        var(func: anyoftext(Text.string, $search)) {
            ~TextWrapper.text {
              ~WithName.names {
                u as uid
              }
            }
        }
        queryByName(func: uid(u)) {
            dgraph.type
            id: Metadata.id
            names : WithName.names
			age: Person.age
			parents: Person.parents
			address: Company.address
        }
    }
    """)
}

Note that you don’t need to alias it.

The GraphQL layer uses dgraph.type for interfaces to figure out the type of implementation. And, as it is not returned from custom DQL, it won’t be able to know the correct type. So, you would need to add it.
I will update the docs for this.

Also, note that you don’t need to mention the extend keyword. The default behaviour is to extend the auto-generated CRUD API with the user-provided Query type.