0

I am using cassandra with graphql and express. On doing the mutation the database is updated, but it doesn't display the fields instead just shows null. Upon using console.log to check the data and I can see it printed on terminal.

Code for mutation,

   const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        add_user: {
            type: UserType,
            args: {
                username: { type : GraphQLString},
                email: { type : GraphQLString},
                name: { type : GraphQLString},
                password: { type : GraphQLString}
            },
            resolve(parent, args){
                const query = 'INSERT INTO users(username, email, name, password) VALUES (?, ?, ?, ?) ';
                client.execute(query, [args.username, args.email, args.name, args.password])
                    .then(result => {
                        const query = 'SELECT * FROM users WHERE username = ? ';
                        return client.execute(query, [args.username])
                            .then(result => {
                                console.log(result.rows[0]);
                                return result.rows[0];
                            });
                    });
            }
        }
    }
});

What I get

{
  "data": {
    "add_user": null
  }
}

Upon using this

mutation{
  add_user(username: "ABCdef", email: "abc.def@ddv.com", name: "ABCDEF", password: "abcdxyz"){
      username
        email
        name
  }
}

On terminal I get the fields

Update can be confirmed with

query{
  user(username: "ABCdef"){
    username
    email
    name
  }
}

which returns

{
  "data": {
    "user": {
      "username": "ABCdef",
      "email": "abc.def@ddv.com",
      "name": "ABCDEF"
    }
  }
}

Info about UserType

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        username: { type: GraphQLString },
        email: { type: GraphQLString },
        name: { type: GraphQLString },
        password: { type: GraphQLString }
    })
});

I want add_user to return the same but that's not happening for some reason

ShreyaKse
  • 45
  • 6
  • You're chaining your `then`s correctly, but your resolver does not return anything. See [Common Scenario #4](https://stackoverflow.com/a/56319138/6024220). – Daniel Rearden Jan 26 '20 at 17:08
  • Thank you so much! adding a return before client.execute solved the problem. – ShreyaKse Jan 27 '20 at 05:27

0 Answers0