【问题标题】:Get data of new dataset, which is created via GraphQL mutation获取通过 G​​raphQL 变异创建的新数据集的数据
【发布时间】: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


    【解决方案1】:

    我认为它应该捕获错误以了解发生了什么

    所以改变这个查询

    this.props.createUserMutation({ variables: { username, password } }).then(response => { console.log(response.data) // data has expected fields, but those have null value })

    this.props.createUserMutation({ variables: { username, password } }).then(response => { console.log(response.data) // data has expected fields, but those have null value }).catch(error => { console.error(error) })

    【讨论】:

    • 没有错误输出。突变正在工作(在数据库中创建用户),但返回的数据丢失...
    【解决方案2】:

    我没有使用过流星,但是查看文档,我认为您对 insert 的调用只是返回了 id,而实际的 db 操作是异步完成的。

    在这种特殊情况下,您在发送插入调用之前确定_idcreateAt 的值,因此您拥有所有需要发送回客户端的信息。做吧:

    resolve ({ db }, { username, password }) {
      const _id = Random.id()
      const createdAt = new Date()
      db.collection('users').insert({
        _id,
        createdAt,
        username,
        password: bcrypt.hashSync(password, 10)
      })
      return { _id, username, password, createdAt }
    }
    

    如果我们希望 MongoDB 为我们生成 _id,我们可以在插入调用中省略它:

    resolve ({ db }, { username, password }) {
      const createdAt = new Date()
      const _id = db.collection('users').insert({
        createdAt,
        username,
        password: bcrypt.hashSync(password, 10)
      })
      return { _id, username, password, createdAt }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多