【发布时间】:2018-04-19 18:39:25
【问题描述】:
我正在尝试进行突变。突变本身正在起作用(用户是在数据库中创建的),
但响应只有null 值:
data
createUser:
createdAt: null
password: null
username: null
我不明白,我做错了什么。我想我必须指定我想取回哪些数据。 但是在我创建用户时,我不知道它的 ID。那么如何将当前添加的数据集作为响应返回?
服务器/突变模式
const UserType = new GraphQLObjectType({
name: 'user',
fields: {
_id: { type: GraphQLID },
createdAt: { type: GraphQLString },
username: { type: GraphQLString },
password: { type: GraphQLString }
}
})
const MutationType = new GraphQLObjectType({
name: 'RootMutationType',
description: 'Mutations',
fields: () => ({
createUser: {
type: UserType,
args: {
username: { type: new GraphQLNonNull(GraphQLString) },
password: { type: new GraphQLNonNull(GraphQLString) }
},
async resolve ({ db }, { username, password }) {
return db.collection('users').insert({
_id: Random.id(),
createdAt: new Date(),
username,
password: bcrypt.hashSync(password, 10)
})
}
}
})
})
客户端/组件
this.props.createUserMutation({
variables: { username, password }
}).then(response => {
console.log(response.data) // data has expected fields, but those have null value
})
// ...
export default compose(
withData,
withApollo,
graphql(
gql`
mutation RootMutationQuery($username: String!, $password: String!) {
createUser(
username: $username,
password: $password,
) {
_id
createdAt
username
password
}
}
`, {
name: 'createUserMutation'
}
)
)
【问题讨论】:
标签: javascript graphql