5

I'm testing my GraphQL API, but I would like to clean it up a little bit. It's worth noting I'm using chai-http for the network requests. Here's what I'm currently doing (which works):

let createUser = (()=> {
  return new Promise((resolve, reject) => {
    chai.request(server)
      .post('/api/graphql/')
      .set('content-type', 'application/json')
      .send({ 'query' : 
        'mutation users { \
          user : addUser(inputs: { \
            firstName: \"Test\", \
            lastName: \"User\", \
            email: \"test@test.com\", \
          }) { \
            id, \
            firstName \
          } \
        }'
      })
      .end((err, res) => {
        if (err) { reject(err) }
        let data = res.body.data;
        let user = data.user;
        resolve(user);
      })
  });
});

However, I would like to clean it up a bit and do something like this:

let createUser = (() => {
  let newUser = {
    firstName: 'Test',
    lastName: 'User',
    email: 'test@test.com'
  };

  return new Promise((resolve, reject) => {
    chai.request(server)
      .post('/api/graphql/')
      .set('content-type', 'application/json')
      .send({ 'query' : 
        'mutation users { \
          user : addUser(inputs: ' + JSON.stringify(newUser) + ') { \
            id, \
            firstName \
          } \
        }'
      })
      .end((err, res) => {
        if (err) { reject(err) }
        let data = res.body.data;
        let user = data.user;
        resolve(user);
      })
  });
});

However, this style does of placing the object inputs does not work and returns a bad request error. Here is what part of the returned error object reads:

enter image description here

Any ideas why this doesn't work? Thanks in advance!

Thomas
  • 2,356
  • 7
  • 23
  • 59

1 Answers1

6

Looks like there's an issue with your JSON input there. It could be some combination of your line breaks and your quotes that's not getting parse properly on the server.

However, I still wouldn't recommend in-lining your variables like that in your GraphQL query. You should do something like this instead:

let createUser = (() => {
  let newUser = {
    firstName: 'Test',
    lastName: 'User',
    email: 'test@test.com'
  };

  return new Promise((resolve, reject) => {
    chai.request(server)
      .post('/api/graphql/')
      .set('content-type', 'application/json')
      .send({
        'query' : 'mutation users ($input: CreateUserInput!) { \
          user : addUser(inputs: $input) { \
            id, \
            firstName \
          } \
        }',
        'variables' : {
           'input': newUser
         }
      })
      .end((err, res) => {
        if (err) { reject(err) }
        let data = res.body.data;
        let user = data.user;
        resolve(user);
      })
  });
});

You can check out another simple example here as to how you can execute a mutation on Scaphold to create a user with query and variables split out.

Hope this helps!

vince
  • 1,904
  • 1
  • 12
  • 17
  • Thanks bud, will try it out! @vince – Thomas May 15 '17 at 14:21
  • Hey @vince, anyway to clean up the response I want back? Say I wanted id, firstName, lastName, facebookUID, email, etc.. returned after creation? What do you think? – Thomas May 16 '17 at 04:35
  • In that case you could always just return more fields. Right now, the query is returning `id` and `firstName`, but you can also keep adding to that list and include `lastName`, `facebookUID`, `email`, etc. – vince May 16 '17 at 08:05