[英]How to query and mutate array type in GraphQL (Relay)?
I am new to GraphQL/Relay and I have a problem with a small project.我是 GraphQL/Relay 的新手,我有一个小项目的问题。 I have a collection of documents containing a field of type "array".
我有一个包含“数组”类型字段的文档集合。 Please tell me which type of GraphQL to use for working with arrays?
请告诉我使用哪种类型的 GraphQL 来处理数组? I tried to use GraphQLList but got some errors like
我尝试使用 GraphQLList 但遇到了一些错误,例如
"Expected GraphQL named type but got: [function GraphQLList]."
“预期的 GraphQL 命名类型,但得到:[函数 GraphQLList]。”
and other.和别的。 Will be very grateful for any help!
将非常感谢任何帮助!
Here is the schema:这是架构:
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 }
);
}
};
Type for '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 };
Mutation:突变:
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
is a wrapper type just like GraphQLNonNull
. GraphQLList
是一个包装器类型,就像GraphQLNonNull
一样。 It wraps another type.它包装了另一种类型。 You use it just like
GraphQLNonNull
-- by invoking the constructor and passing in the type you want to wrap.您可以像使用
GraphQLNonNull
一样使用它——通过调用构造函数并传入您想要包装的类型。
new GraphQLList(GraphQLString)
Both wrapper types can wrap each other, so you can do something like this as well:两种包装器类型都可以相互包装,因此您也可以执行以下操作:
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.