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

问题描述 投票:0回答:1

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

“期望的GraphQL命名类型,但是得到:[函数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 }
    );
  }
};

“房屋”的类型:

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;
javascript mongoose graphql relayjs relay
1个回答
0
投票

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

new GraphQLList(GraphQLString)

这两种包装器类型都可以互相包装,因此您也可以执行以下操作:

new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))
© www.soinside.com 2019 - 2024. All rights reserved.