question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error Abstract type Search must resolve to an Object type at runtime for field Query.search

See original GitHub issue

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.",

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.

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
}

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;
        });
     },
  }
};

Playground

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

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
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:10

github_iconTop GitHub Comments

15reactions
usama-asfarcommented, Jan 25, 2020

When using unions, you will have to return a __typename so apollo-server knows which type the result has and which resolver map must be used for resolving further field values of the resolved type.

search query must be something like below

Query: {
  search: async (_source, { query }, { dataSources }) => {
    const data = dataSources.API.search(query)

    if (data.name === 'Series') return {
      __typename: 'Series',
      ...data
    }

    if (data.name === 'Movies') {
      __typename: 'Movies',
      ...data
    }
  }
}
2reactions
MarshHawkcommented, Mar 4, 2020

Simplest (fewest extra lines of code) solution follows:

const resolvers = {
  Search:{
    __resolveType(parent) {
    return parent.constructor.name
  }
},

  SMovies:{
    ...
  },

  SSeries:{
    ...
  },

  MovieExtra:{
    ...
  },

  SerieExtra:{
    ...
  },


  Query: {
    search: async(_source , {query} , { dataSources}) =>{
      return dataSources.API.search(query)
        .then(doc => {
          return doc.content;
        })
    },
  }
};

module.exports = resolvers;

No Need for __typename: or _isTypeOf:

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - "Abstract type X must resolve to an Object type at ...
The resolveType function should return a string with the name of the concrete type the abstract type should resolve to. You are returning...
Read more >
Abstract type Search must resolve to an Object type at runtime ...
helo friends,. Someone who can help me solve a problem in Apollo Graphql. I have implemented a search query that receives a parameter...
Read more >
Abstract type "..." must resolve to an Object type at runtime for ...
i got some error about this but i can't fix, can anyone help me to solve this? here is my schemas. type Query{...
Read more >
type-graphql/Lobby - Gitter
I've tried implementing abstract class as a field, but I'm getting an error: "Abstract type \"IReward\" must resolve to an Object type at...
Read more >
Thanks a Billion | DoSomething.org
Error : GraphQL error: Abstract type "Block" must resolve to an Object type at runtime for field "Query.block". Either the "Block" type should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found