DEV Community

Chris Michael
Chris Michael

Posted on

Need Help - Abstract type Search must resolve to an Object type at runtime for field Query.search

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:

"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.",
Enter fullscreen mode Exit fullscreen mode

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

Query

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
}
Enter fullscreen mode Exit fullscreen mode

resolvers

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;
        });
     },
  }
};
Enter fullscreen mode Exit fullscreen mode

Playground

query{
    search(query: "avatar"){
      ... on Series{
        id
    }
      ... on Movies{
        id
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Error

{
  "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
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
ryantan profile image
Ryan Tan • Edited
Search:{
    __resolveType(obj , context , info){
      if (custom logic to check obj is series) return 'Series';
      if (custom logic to check obj is movies) return 'Movies';
    }
  },
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aforamitrai profile image
amit_

did you find the solution, i am facing simmilar issue