定义 GraphQL 模式时获取字段类型必须是输出类型错误

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

我将 express graphql 与 React 初学者工具包样板一起使用,并在收到错误时尝试定义我自己的嵌套模式 Error: Availability.rooms field type must be Output Type but got: undefined. 我采用了所有在第一个深度场都已解析的示例 -我更改了它,以便我的字段具有嵌套字段类型并将解析移至嵌套字段 - 但我认为我有语法错误。

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

应该是:

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

由于一个字段可以获得 type 以外的更多属性(例如 resolve),您应该传递一个具有 type 属性 的对象。就像您为其他人所做的那样。