3

I'm running the following mutation query in http://localhost:3010/graphiql:

Mutation

mutation($fromID: String!, $toID: String!, $msgText: String!){
  createIM(fromID: $fromID, toID: $toID, msgText: $msgText){
    fromID
    toID
    msgText
  }
}

Query Variables

{
  "fromID": "1",
  "toID": "2",
  "msgText": "Test from GraphIQL"
}

I have this mutation code in my resolvers:

Mutation: {
    createIM(root, args, context) {
        return Promise.resolve()
            .then(() => {
                console.log('checkpoint #1');
                const temp = connectors.IM.create(args);
                return temp;
            })
            .then((x) => {
                //console.log(x);

                return x;
            })
            .then((args) =>{
                console.log(args);
                console.log('checkpoint #2');
                const temp = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues))
                    return temp;
                }
            )
            .then(comment => {
                // publish subscription notification
                console.log('checkpoint #3');
                console.log(comment);
                pubsub.publish('IMAdded', comment);
                return comment;
            })
            .catch((err)=>{console.log(err);});
    },
},

It successfully creates a new record in my postgres database. The console.log(comment) in the final then block (checkpoint #3) logs the following:

 [ { id: 1,
     fromID: '1',
     toID: '2',
     msgText: 'testing 123 from graphiql',
     createdAt: Thu Sep 15 2016 12:42:18 GMT-0700 (PDT),
     updatedAt: Thu Sep 15 2016 12:42:18 GMT-0700 (PDT) },
   { id: 2,
     fromID: '1',
     toID: '2',
     msgText: 'test from graphiql',
     createdAt: Sat Sep 17 2016 22:50:43 GMT-0700 (PDT),
     updatedAt: Sat Sep 17 2016 22:50:43 GMT-0700 (PDT) },
   { id: 3,
     fromID: 'DsmkoaYPeAumREsqC',
     toID: '572bddac4ecbbac0ffe37fd4',
     msgText: 'testing 123 from ui',
     createdAt: Sun Sep 18 2016 10:57:12 GMT-0700 (PDT),
     updatedAt: Sun Sep 18 2016 10:57:12 GMT-0700 (PDT) },
[.....]
   { id: 32,
     fromID: '1',
     toID: '2',
     msgText: 'Test from GraphIQL',
     createdAt: Wed Oct 12 2016 16:34:41 GMT-0700 (PDT),
     updatedAt: Wed Oct 12 2016 16:34:41 GMT-0700 (PDT) } ]

But GraphIQL gives me the following response:

{
  "data": {
    "createIM": {
      "fromID": null,
      "toID": null,
      "msgText": null
    }
  }
}

Do I need to do something different in my final .then block?

VikR
  • 4,818
  • 8
  • 51
  • 96
  • Are you trying to return 32 comments, or just one? I suspect GraphQL is looking for one comment object, rather than an array of them? – stubailo Oct 12 '16 at 23:57
  • Would it be considered correct for my final `then` block to return the most recent record, or would that be a potential error due to race conditions? – VikR Oct 13 '16 at 03:54
  • 1
    Got it! The `then` code block at checkpoint #2 was unnecessary. The param received was actually the just-added database record. GraphiQL is now displaying the correct result! – VikR Oct 13 '16 at 08:01
  • @VikR would love to see your answer posted if you've got it working! – Patrick Lee Scott Oct 13 '16 at 14:45

1 Answers1

1

Per request of Patrick Scott, here is the working code, with thanks to @stubailo for his help:

Mutation: {
    createIM(root, args, context) {
        return Promise.resolve()
            .then(() => {
                console.log('cp #1');
                const temp = connectors.IM.create(args);
                return temp;
            })
            .then(comment => {
                // publish subscription notification
                console.log('cp #2');
                console.log(comment);
                pubsub.publish('IMAdded', comment);
                return comment;
            })
            .catch((err)=>{
                console.log(err);
            });
    },
},
VikR
  • 4,818
  • 8
  • 51
  • 96