Need Help - Abstract type Search must resolve to an Object type at runtime for field Query.search
Tags: help, discuss, graphql
Published: 2019-09-30 15:29:10

_helo friends,_

_Someone who can help me solve a problem in Apollo Graphql. I have implemented a search query that receives a parameter that is the title to search, the problem I have is that it shows me the following error:_

```json
"message": "Abstract type Search must resolve to an Object type at runtime for field Query.search with value { id: \"avatar\", title: \"Avatar\", poster: \"https://pedropolis.tv/wp-content/uploads/2009/12/Avatar-150x150.jpg\", sinopsis: \"Jake Sully, un ex-marine confinado a una silla de ruedas, es reclutado para viajar al planeta Pandora, donde un consorcio corporativo está extrayendo un mineral que será clave en la solución de ...\", type: \"pelicula\", extra: [[Object]] }, received \"undefined\". Either the Search type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
```

_The return type of the search is a **union** of the type **Series** | **Movies**_

❤️  _Any help will be more than received, I've been trying to solve the problem for a couple of weeks._


_For more information about the code, visit [cinemanight-graphql](https://github.com/ChrisMichaelPerezSantiago/cinemanight-graphql)_


**Query**
```javascript
const resolvers = require('./resolvers.js');
const {gql} = require('apollo-server');

const typeDefs = gql `
  extend type Query{
    search(query: String!): [Search!]!
  }

  union Search = Series | Movies

  type Series{
    id: String!
    title: String!
    sinopsis: String!
    poster: String!
    rating: String!
    year: String!
    extra: [SerieExtra!]!
  }

  type Movies{
    id: String!
    title: String!
    sinopsis: String!
    poster: String!
    rating: String!
    quality: String!
    year: String!
    extra: [MovieExtra!]!
  }

  type Episodes{
    id: String!
    title: String!
    episode_name: String!
    poster: String!
    date: String!
    quality: String!
    sinopsis: String!
  }

  type VideoIframe{
    iframe: Iframe
  }

  type Iframe{
    option: Int 
    video_iframe: String
  }

  type SerieExtra {
    channel: String!
    first_air_date: String!
    last_air_date: String!
    total_seasons: String!
    total_episodes: String
    season_list: [SeasonList!] !
    cast_members: CastMembers!
    similar_series: [SimilarSeries!] !
  }

  type MovieExtra {
    air_date: String!
    country: String!
    runtime: String!
    rated: String!
    cast_members: CastMembers!
    similar_movies: [SimilarMovies!] !
  }

// The information of the type SeasonList, CastMembers, SimilarSeries, SimilarMovies
// It was omitted since it would be a lot of information in the template .. for this discussion.
`;

module.exports = {
  typeDefs,
  resolvers
}
```

**resolvers**

```javascript
const resolvers = { 
 Search:{
    __resolveType(obj , context , info){
      // .....
      return null;
    }
  },
  Query:{ 
    search: async(_source , {query} , { dataSources}) =>{
    return dataSources.API.search(query)
        .then(doc => {
          console.log(doc.content) // It correctly shows me the query data.
          return doc.content;
        });
     },
  }
};
```


**Playground**

```javascript
query{
    search(query: "avatar"){
      ... on Series{
        id
    }
      ... on Movies{
        id
    }
  }
}
```

**Error**
```json
{
  "errors": [
    {
      "message": "Abstract type Search must resolve to an Object type at runtime for field Query.search with value { id: \"avatar\", title: \"Avatar\", poster: \"https://pedropolis.tv/wp-content/uploads/2009/12/Avatar-150x150.jpg\", sinopsis: \"Jake Sully, un ex-marine confinado a una silla de ruedas, es reclutado para viajar al planeta Pandora, donde un consorcio corporativo está extrayendo un mineral que será clave en la solución de ...\", type: \"pelicula\", extra: [[Object]] }, received \"undefined\". Either the Search type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "search",
        0
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "message": "Abstract type Search must resolve to an Object type at runtime for field Query.search with value { id: \"avatar\", title: \"Avatar\", poster: \"https://pedropolis.tv/wp-content/uploads/2009/12/Avatar-150x150.jpg\", sinopsis: \"Jake Sully, un ex-marine confinado a una silla de ruedas, es reclutado para viajar al planeta Pandora, donde un consorcio corporativo está extrayendo un mineral que será clave en la solución de ...\", type: \"pelicula\", extra: [[Object]] }, received \"undefined\". Either the Search type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
          "locations": [
            {
              "line": 2,
              "column": 3
            }
          ],
          "stacktrace": [
            "GraphQLError: Abstract type Search must resolve to an Object type at runtime for field Query.search with value { id: \"avatar\", title: \"Avatar\", poster: \"https://pedropolis.tv/wp-content/uploads/2009/12/Avatar-150x150.jpg\", sinopsis: \"Jake Sully, un ex-marine confinado a una silla de ruedas, es reclutado para viajar al planeta Pandora, donde un consorcio corporativo está extrayendo un mineral que será clave en la solución de ...\", type: \"pelicula\", extra: [[Object]] }, received \"undefined\". Either the Search type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
            "    at ensureValidRuntimeType (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:667:11)",
            "    at completeAbstractValue (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:660:42)",
            "    at completeValue (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:585:12)",
            "    at completeValue (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:557:21)",
            "    at completeValueCatchingError (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:495:19)",
            "    at C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:618:25",
            "    at Array.forEach (<anonymous>)",
            "    at forEach (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\iterall\\index.js:83:25)",
            "    at completeListValue (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:614:24)",
            "    at completeValue (C:\\Users\\c\\Desktop\\cinemanight-graphql\\node_modules\\graphql\\execution\\execute.js:573:12)"
          ]
        }
      }
    }
  ],
  "data": null
}
```