title: Resolving Types in Graphql
body: <p>There are couple of places in GraphQL where resolving a <code>Type</code> is required and not just a <code>field</code> in a <code>Type</code>.</p>
  
  <p>The Backend API -</p>
  
  <ul>
  <li><code>/users</code> - list of users - minimal info - name, id</li>
  <li><code>/users/:id</code> - detailed user info</li>
  <li><code>/foo</code> - returns a field owner which is a UserID</li>
  </ul>
  
  <h3>Query and Schema</h3>
  
  <p>Building a schema to execute the following query</p>
  
  <pre><code>query a {
    users {
      age # some detail info
    }
    foo {
      owner {
        location # some detail info
      }
    }
  }
  </code></pre>
  
  <p>and the schema can be as follows -</p>
  
  <pre><code>type Query {
    users: [User]
    foo: Foo
  }
  type Foo {
    owner: User
  }
  type User {
    id: ID
    age: Int
    location: String
  }
  </code></pre>
  
  <h3>Problem</h3>
  
  <p>The resolvers in the above schema would need to contain / handle the user details info fetching call in 2 different places. 1. List of users - <code>Query.users</code> and 2. <code>Query.foo.owner</code>. And one has to remember to handle this type where you only have a user ID to convert it to actual User.</p>
  
  <h3>Possible Solution</h3>
  
  <p>As of this writing, GraphQL supports <code>resolveType</code> on <code>Interface</code> and <code>Union</code>. It is not possible to specify a resolver for an entire <code>Type</code> - only a <code>field</code> in a <code>Type</code> can have a resolver. So if it's possible to resolve a type in GraphQL, this would make it simpler to implement.</p>
  
  <h3>Alternate Solution</h3>
  
  <p>Since only fields in a Type can be resolved, it is possible to create an extra <code>type</code> and maintain the handling of this field in the Type in the resolvers and is in one location. But now, the query is deeper than before by 1 level.</p>
  
  <pre><code>query b {
    users {
      details {
        age
      }
    }
    foo {
      owner {
        details {
          location
        }
      }
    }
  }
  </code></pre>
  
  <h3>Other similar scenarios</h3>
  
  <p>Since a <code>Type</code> cannot be resolved in GraphQL, <code>enums</code> face the same issue. When you have a special character in an API response and the field is an ENUM, you either remember to handle it in all the places where this enum is used or you create an extra type to represent this enum.</p>
  
  <p>I have created a minimal repro for all these cases with ApolloGraphQL - <a href="https://github.com/boopathi/graphql-test-1" rel="noreferrer">https://github.com/boopathi/graphql-test-1</a> </p>
  
  <h3>Questions</h3>
  
  <ol>
  <li>Shouldn't the schema/language support in specifying how to handle a particular type / specifying a resolver for a Type instead of just a field in a Type? If not, why?</li>
  <li>How do you handle these things in your schema. Is there another way to do these things ?</li>
  </ol>