所以我使用Graphql和apollo建立了一个api,并设法将一个字符串数组导入mongoDB ...现在我正在使用Apollo查询数据反应,似乎无法找到如何检索它,因为我得到了
error:[GraphQL error]: Message: String cannot represent an array value: [pushups,situps], Location: [object Object], Path: wods,0,movements
我的架构设置为:
const WodType = new GraphQLObjectType({
name: 'Wod',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
movements: { type: GraphQLString },
difficulty: { type: GraphQLString },
group: {
type: GroupType,
resolve(parent, args) {
return Group.findById(parent.groupId);
}
}
}) });
和我的突变:
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addWod: {
type: WodType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
movements: { type: new GraphQLList(GraphQLString) },
difficulty: { type: new GraphQLNonNull(GraphQLString) },
groupId: { type: new GraphQLNonNull(GraphQLID) }
},
resolve(parent, args) {
let wod = new Wod({
// Use model to create new Wod
name: args.name,
movements: args.movements,
difficulty: args.difficulty,
groupId: args.groupId
});
// Save to database
return wod.save();
}
这个数组是“移动”下的一个字符串数组...非常感谢任何有关查询到React的帮助......这是前端的当前查询...使用Apollo Boost
const getWodsQuery = gql`
{
wods {
id
name
movements
difficulty
}
}
`;
不确定它是否仍然相关,我没有重新创建代码,但问题可能是,你将“运动”作为“字符串”返回,而不是输出对象类型Wod中的字符串数组。这只是基于您传递给变异的参数(这是字符串列表)的假设。修复应该只是修改Wood类型,如下所示
const WodType = new GraphQLObjectType({
name: 'Wod',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
movements: { type: new GraphQLList(GraphQLString) },
difficulty: { type: GraphQLString },
group: {
type: GroupType,
resolve(parent, args) {
return Group.findById(parent.groupId);
}
})
请注意,这只是我的假设,因为我不知道您的数据是如何存储的,但根据错误消息它可能是正确的。我发表了关于在GraphQL模式中实现列表/数组的文章,因为我看到许多人都在努力解决类似的问题。你可以在这里查看https://graphqlmastery.com/blog/graphql-list-how-to-use-arrays-in-graphql-schema