我正在尝试使用graphql进行原始应用程序。但是我不确定更新请求。我试过了,但是没有用。在这里看到我的代码。 创建帖子和查询帖子运行正常。
我正在使用express和express-graphql。 我尝试过文档,但是。我不知道。
模式
const graphqlHttp = require('express-graphql');
const {buildSchema} = require('graphql');
app.use('/graphql', graphqlHttp({
schema: buildSchema(`
type Post {
_id: ID!
title: String!
description: String!
content: String!
date: String!
}
input PostInput {
title: String!
description: String!
content: String!
date: String!
}
type RootMutation {
createPost(postInput: PostInput!): Post
updatePost(_id: ID!, postInput: PostInput!): Post
}
schema{
query: RootQuery
mutation: RootMutation
}
`),
解析器
updatePost: args => {
console.log(args); <!-- this log gives nothing
Post.findByIdAndUpdate(args._id, {$set: {
title: args.postInput.title,
description: args.postInput.description,
content: args.postInput.content,
date: new Date(args.postInput.date)
}})
.then(result => {
console.log(result);
return {
...result._doc
}
}).catch (err =>{
throw err;
});
},
localhost:8080 / graphql进行突变
mutation {
updatePost(_id: "5d5a3f380930813c647cb697", postInput: {title: "update title", description: "update", content: "update content", date: "2019-08-19T06:18:06.778Z"}) {
title
}
}
变异结果
{
"data": {
"updatePost": null
}
}
答案 0 :(得分:2)
错误的论点:
由于它的简化结构,我习惯了Apollo。在Apollo中,我遇到了类似的问题,而解析程序中查询和突变的参数由4个不同的项组成。
state = {
inputTextValue : '',
}
submitText = () => {
//handle the click action
//add this line at the end of the function after you are done handling with the input text value.
this.state.inputTextValue = '';
}
<TextInput
onChangeText={(text) => this.setState({ inputText: text })}
placeholder="Monday's breakfast"
value={this.state.inputTextValue}
/>
<TouchableOpacity
onPress={() => this.submitText()}>
<Text>Submit</Text>
</TouchableOpacity>
ECMA奖励:
我也建议使用resolver方法的await / async版本,以便获得响应而不是Promise。阅读https://www.greycampus.com/blog/programming/java-script-versions,以便使用最新的ECMAScript简化代码。
updatePost: (parent,args,context,info) => { // <-- look at this line
console.log(args);
Post.findByIdAndUpdate(args._id, {$set: {
title: args.postInput.title,
description: args.postInput.description,
content: args.postInput.content,
date: new Date(args.postInput.date)
}}).then(result => {
console.log(result);
return {
...result._doc
}
}).catch (err =>{
throw err;
});
},
ExampleMutation2: ...
答案 1 :(得分:0)
我知道我迟到了,你可能已经修复了它,但它可能会帮助其他人。
type RootMutation {
updatePost(_id: ID!, postInput: PostInput!): Post
改为写:
updatePost(id: ID!, postInput: PostInput!): Post!