【发布时间】:2019-01-31 10:31:12
【问题描述】:
所以我已经使用 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
}
}
`;
【问题讨论】:
标签: mongodb reactjs graphql apollo