Getting field type must be Output Type error when defining a GraphQL schema

1.1k views Asked by At

I'm using express graphql with the react starter kit boilerplate and am trying to define my own nested schema when i get the error Error: Availability.rooms field type must be Output Type but got: undefined. I took the examples which all had resolve at the first depth field - I changed it so that my field had a nested field type and moved the resolve to the nested field - but I think I have a syntax error.

schema.js

import {
  GraphQLSchema as Schema,
  GraphQLObjectType as ObjectType,
} from 'graphql';

import hotel from './queries/hotel';

const schema = new Schema({
  query: new ObjectType({
    name: 'Query',
    fields: {
      hotel,
    },
  }),
});

export default schema;

hotel.js

import {
  GraphQLObjectType as ObjectType,
  GraphQLList as List,
  GraphQLString as StringType,
  GraphQLBoolean as BooleanType,
  GraphQLInt as IntType,
  GraphQLNonNull as NonNull,
} from 'graphql';

import { checkAvailability } from './hotels';

const RoomType = new ObjectType({
  name: 'Room',
  fields: {
    name: { type: new NonNull(StringType) },
    roomTypeCategory: { type: new NonNull(StringType) },
    nights: { type: new NonNull(IntType) },
    isAvailable: { type: new NonNull(BooleanType) },
    startDate: { type: new NonNull(StringType) },
    endDate: { type: new NonNull(StringType) },
    availability: { type: new List(BooleanType) },
  },
});

const AvailabilityType = new ObjectType({
  name: 'Availability',
  args: {
    hotelId: { type: new NonNull(StringType) },
    checkIn: { type: new NonNull(StringType) },
    checkOut: { type: new NonNull(StringType) },
    numberOfAdults: { type: new NonNull(IntType) },
  },
  fields: { rooms: new List(RoomType) },
  async resolve({ request }, { hotelId, checkIn, checkOut, numberOfAdults }) {
    const rooms = await checkAvailability({
      hotelId,
      checkIn,
      checkOut,
      numberOfAdults,
    });
    return { rooms };
  },
});

export default {
  type: new ObjectType({
    name: 'Hotels',
    fields: {
      availability: { type: AvailabilityType },
    },
  }),
};
1

There are 1 answers

0
Aᴍɪʀ On BEST ANSWER

It should be:

fields: {
    rooms: {type: new List(RoomType) }
}

As a field could get more properties other than type (resolve for example), you should pass an object that has a type property. Just like what you did for the rest of them.