如何为连接到mLab的GraphQL中的“更新”数据编写Mutation

时间:2018-05-31 10:25:50

标签: mongoose graphql crud

我正在学习GraphQL Mutations。我将数据保存在mLab上。我使用GraphiQL进行查询输出。我不知道如何为“更新”数据编写Mutation。现在我有适当的“创建”和“删除”突变:

schema.js

if let phone = number.value as? CNPhoneNumber {
    print(phone.stringValue)
} else {
   print("number.value not of type CNPhoneNumber")
}

}

GraphiQL

addMovie: {
type: MovieType,
args: {
    title: {type: new GraphQLNonNull(GraphQLString)},
    genre: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args) {
    let new_movie = new Movie({
        title: args.title,
        genre: args.genre
    });
    return new_movie.save();
}

}

schema.js

mutation {
addMovie(title: "Goodfellas", genre: "Drama"){
    title
    genre
}

}

GraphiQL

deleteMovie: {
type: MovieType,
args: {
    id: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args) {
    return Movie.findOneAndDelete(args.id);
}

}

我尝试写“更新”变种,到目前为止我所做的最好是:

schema.js

mutation {
deleteMovie(id: "5c0djut25d81cbq17356628y"){
    id
}

}

GraphiQL

editMovie: {
type: MovieType,
args: {
    id: {type: new GraphQLNonNull(GraphQLString)},
    title: {type: GraphQLString},
    genre: {type: GraphQLString}
},
resolve(parent, args) {
    return Movie.findOneAndUpdate(args.id);
}

}

上面的变异不会产生任何错误,但它仍然会返回

mutation {
editMovie(id: "5c0djut25d81cbq17356628y", title: "Goodfellas", genre: "Crime"){
    id
}

而不是

genre: "Drama"

1 个答案:

答案 0 :(得分:0)

好的,我有这个:

<强>突变

var jsonObj = [{"min_order_amount":"0.00000010","ask":"0.00000017","bid":"0.0000001","last":"0.00000010","lastDayAgo":"0.00000009","vol":"154955.9586604","spread":"0","buy_fee_percent":"0","sell_fee_percent":"0","market_name":"ATR_BTC","market_id":338,"updated_time":1527789301,"server_time":1527789301},{"min_order_amount":"0.00000010","ask":"0.000032","bid":"0.000012","last":"0.00003200","lastDayAgo":"0.000065","vol":"372.5011152","spread":"0","buy_fee_percent":"0","sell_fee_percent":"0","market_name":"ETH_BTC","market_id":35,"updated_time":1527789301,"server_time":1527789301},{"min_order_amount":"0.00000010","ask":"0.00003595","bid":"0.00003","last":"0.00003000","lastDayAgo":"0.00003001","vol":"26.44435669","spread":"0","buy_fee_percent":"0","sell_fee_percent":"0","market_name":"ARDOR_BTC","market_id":262,"updated_time":1527789301,"server_time":1527789301}];

var res = jsonObj.filter(obj => {
  return obj.market_name == 'ETH_BTC'
});

console.log(res);

<强>查询:

updateMovie: {
    type: MovieType,
    args: {
        id: {type: new GraphQLNonNull(GraphQLID)},
        title: {type: new GraphQLNonNull(GraphQLString)},
        genre: {type: new GraphQLNonNull(GraphQLString)}
    },
    resolve(parent, args) {
        return Movie.findByIdAndUpdate(args.id, {$set: {title: args.title, genre: args.genre}}).exec();
    }
}