GraphQL nested types: "The type of ... must be Output Type but got: undefined."

I realize this question has been asked similarly ad nauseum, but I haven't been able to find a pre-existing issue that matches my scenario. I'm building a simple GraphQL wrapper for pokeapi.co to learn GraphQL. Everything went smoothly until I attempted to nest one type inside of another.

The graphiql error states "The type of pokemon.abilities must be Output Type but got: undefined."

Does each Type require a specific resolver? The getPokemonById resolver includes the data required for the Abilities array.

Also, in terms of project structure, I am (very) loosely following this repo for separation of concerns. Very much open to thoughts on structure as well.

types.js

const {
  GraphQLList,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLBoolean
} = require("graphql");

const AbilityType = new GraphQLObjectType({
  name: "ability",
  fields: {
    is_hidden: { type: GraphQLBoolean },
    slot: { type: GraphQLInt },
    ability: {
      name: { type: GraphQLString },
      url: { type: GraphQLString }
    }
  }
});

const PokemonType = new GraphQLObjectType({
  name: "pokemon",
  fields: {
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    base_experience: { type: GraphQLInt },
    height: { type: GraphQLInt },
    is_default: { type: GraphQLBoolean },
    order: { type: GraphQLInt },
    weight: { type: GraphQLInt },
    abilities: new GraphQLList(AbilityType),
  }
});

module.exports = {
  PokemonType,
  AbilityType,
};

queries.js

const { GraphQLInt, GraphQLObjectType } = require('graphql');
const { PokemonType } = require('../types/pokemon');
const { getPokemonById } = require("../resolvers/pokemon");

const pokemonQuery = new GraphQLObjectType({
  name: "Query",
  fields: {
    pokemon: {
      type: PokemonType,
      args: {
        id: { type: GraphQLInt }
      },
      resolve: (_, { id }) => {
        return getPokemonById(id);
      }
    }
  }
});

module.exports = {
  pokemonQuery
};

resolvers.js

const rp = require("request-promise");

const BASE_URL = "https://pokeapi.co/api/v2/pokemon";

const getPokemonById = id => {
  console.log(`attempting to query pokemon with id ${id}`)

  const rpOptions = {
    uri: `${BASE_URL}/${id}/`,
    headers: {
      'User-Agent': 'Request-Promise'
    },
    json: true,
  };
  return rp(rpOptions).catch(err => console.error(err));
}

module.exports = {
  getPokemonById
}

1 answer

  • answered 2018-07-11 06:13 MarcoDaniels

    The issue seems to be in the filed abilities under PokemonType, it's missing its type definition. Try updating it to:

    const PokemonType = new GraphQLObjectType({
      name: "pokemon",
      fields: {
        ....
        order: { type: GraphQLInt },
        weight: { type: GraphQLInt },
        abilities: { type: new GraphQLList(AbilityType) },
      }
    });