javascript - 如何在 GraphQL (Relay) 中查询和改变数组类型?
问题描述
我是 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;
解决方案
GraphQLList
是一个包装器类型,就像GraphQLNonNull
. 它包装了另一种类型。您可以像使用它一样GraphQLNonNull
-- 通过调用构造函数并传入要包装的类型。
new GraphQLList(GraphQLString)
两种包装器类型都可以相互包装,因此您也可以执行以下操作:
new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)))
推荐阅读
- node.js - nodejs中html内的数组映射错误
- python - 如何绘制多个树状图?
- flutter - 如何将 AdobeXD 精确原型转换为颤振
- java - 三个单元如何在 Analogic 中并行工作传入的工件载体?
- dataframe - 两个数据帧之间的欧式距离
- regex - 如何匹配以反斜杠开头的字符串,后跟 / 之类的问号?
= 用正则表达式 - python - 根据键在字典中查找值
- python - Python(CPython)中的最大可解析行长度是多少?
- python - 通过 python 3 在用户文件夹中搜索光栅图像文件的最有效方法是什么?
- .net - 在 Webview2 中使用 ExecuteScriptAsync 从网站中提取数据