简体   繁体   中英

Get data of new dataset, which is created via GraphQL mutation

I'm trying to do a mutation. The mutation itself is working (user is created in DB), but the response has only null values:

data
  createUser:
    createdAt: null
    password: null
    username: null

I don't see, what I am doing wrong. I think I have to specifiy which data I want to get back. But at the time I do create the user, I don't know its ID. So how do I get the current added dataset back as a response?

server / mutation schema

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)
        })
      }
    }
  })
})

client / component

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'
    }
  )
)

I think it should catch error to know what's going on

so change this query

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

to

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) })

I haven't worked with meteor, but looking at the docs, I think your call to insert just returns the id, and the actual db operation is completed asynchronously.

In this particular case, you're determining the value for both _id and createAt before sending the insert call, so you have all the information you need to send back to the client. Just do:

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 }
}

If we wanted MongoDB to generate the _id for us, we can omit it from the insert call:

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 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM