如何在 GraphQL(中继)中查询和改变数组类型?

How to query and mutate array type in GraphQL (Relay)?

我是 GraphQL/Relay 的新手,我遇到了一个小项目的问题。我有一个文档集合,其中包含 "array" 类型的字段。请告诉我使用哪种类型的 GraphQL 来处理数组? 我尝试使用 GraphQLList 但出现了一些错误,例如

"Expected GraphQL named type but got: [function GraphQLList]."

等。 非常感谢任何帮助!

架构如下:

const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
const Schema = mongoose.Schema;

const houseSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  events: {
    type: Array,
    default: []
  }
});

var houseModel = mongoose.model("House", houseSchema);

module.exports = {
  getHouses: () => {
    return houseModel.find({}).limit(10).sort({_id:-1})
      .then(houses => {
        return houses.map(house => {
          return {
            ...house._doc,
            id: house.id
          };
        });
      })
      .catch(err => {
        throw err;
      });
  },
  getHouse: id => {
    return houseModel.findOne({ _id: id });
  },
  createHouse: house => {
    return houseModel(house).save();
  },
  removeHouse: id => {
    return houseModel.findByIdAndRemove(id);
  },
  updateHouse: (id, args) => {
    return houseModel.findByIdAndUpdate(
      id,
      {
        name: args.name,
        events: args.events //-----------------
      },
      { new: true }
    );
  }
};

'house'类型:

const {
  GraphQLList,
  GraphQLObjectType,
  GraphQLString
} = require('graphql');

const { globalIdField, connectionDefinitions } = require('graphql-relay');
const { nodeInterface } = require('../nodes');

const House = new GraphQLObjectType({
  name: "House",
  description: "lkjlkjlkjlkjlk",
  interfaces: [nodeInterface],
  fields: () => ({
    id: globalIdField(),
    name: {
      type: GraphQLString,
      description: "Name of House"
    },
    events: {
      type: GraphQLList,
      description: "Events list"
    }
  })
});

const { connectionType: HouseConnection } = connectionDefinitions({
  nodeType: House
});

module.exports = { House, HouseConnection };

突变:

const {
  GraphQLList,
  GraphQLObjectType,
  GraphQLNonNull,
  GraphQLString,
  GraphQLBoolean
} = require('graphql');

const { fromGlobalId, mutationWithClientMutationId } = require('graphql-relay');
const { House } = require('./types/house');

const houseModel = require('./models/house');

const CreateHouseMutation = mutationWithClientMutationId({
  name: "CreateHouse",
  inputFields: {
    name: { type: new GraphQLNonNull(GraphQLString) },
    events: { type: new GraphQLNonNull(GraphQLList) }
  },
  outputFields: {
    house: {
      type: House
    }
  },
  mutateAndGetPayload: args => {
    return new Promise((resolve, reject) => {
      houseModel.createHouse({
        name: args.name,
        events: args.events
      })
        .then(house => resolve({ house }))
        .catch(reject);
    });
  }
});

const UpdateHouseMutation = mutationWithClientMutationId({
  name: "UpdateHouse",
  inputFields: {
    id: { type: new GraphQLNonNull(GraphQLString) },
    name: { type: new GraphQLNonNull(GraphQLString) },
    events: { type: new GraphQLNonNull(GraphQLList) }
  },
  outputFields: {
    updated: { type: GraphQLBoolean },
    updatedId: { type: GraphQLString }
  },
  mutateAndGetPayload: async (args) => {
    const { id: productId } = fromGlobalId(args.id);
    const result = await houseModel.updateHouse(productId, args);
    return { updatedId: args.id, updated: true };
  }
});

const RemoveHouseMutation = mutationWithClientMutationId({
  name: "RemoveHouse",
  inputFields: {
    id: { type: new GraphQLNonNull(GraphQLString) },
  },
  outputFields: {
    deleted: { type: GraphQLBoolean },
    deletedId: { type: GraphQLString }
  },
  mutateAndGetPayload: async ({ id }, { viewer }) => {
    const { id: productId } = fromGlobalId(id);
    const result = await houseModel.removeHouse(productId);
    return { deletedId: id, deleted: true };
  }
});

const Mutation = new GraphQLObjectType({
  name: "Mutation",
  description: "kjhkjhkjhkjh",
  fields: {
    createHouse: CreateHouseMutation,
    removeHouse: RemoveHouseMutation,
    updateHouse: UpdateHouseMutation
  }
});

module.exports = Mutation;

GraphQLList 是一个 wrapper 类型,就像 GraphQLNonNull 一样。它 包装 另一种类型。您可以像 GraphQLNonNull 一样使用它——通过调用构造函数并传入您要包装的类型。

new GraphQLList(GraphQLString)

两种包装类型都可以相互包装,因此您也可以这样做:

new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))