Part 6 from series: How to build a real-time chat app

GraphQL Mutations

Create and POST a new message using a GraphQL Mutation

We have our GraphQL query to retrieve a list of messages for our Slack hosted conversation. And we have the Angular components we need to display the list of messages once we've retrieved the data. Lets take a look at how to create a message in our conversation from our application using Apollo Angular and GraphQL mutations.

Prerequisites

Before we move on to developing the new functionality, we need to set one more environment variable for our GraphQL API, of which we will get from Slack.

If you're not already signed in to Slack, go ahead and sign in to your new workspace, the URL should be in your inbox, created in the setup guide.

Next you'll need to get your Slack user ID. The easiest way is to log in to your Slack workspace, click your profile image (usually in the bottom left on the desktop app) and click 'Profile'. In the panel that opens, click the three dot mennu and choose 'Copy member ID'.

Use this to provision the SLACK_ME_ID environment variable:

export SLACK_ME_ID=Replace with your id here

Create the schema Mutation

We're first going to define a top-level mutation in our GraphQL schema. From your project folder, open the file <project folder>/web-api/index.js and add the following code to the typeDefs variable in the middle of the file:

File path icon web-api/index.js
//Existing code...

const typeDefs = gql`
  type Query {
    #Existing code...
  }

  # Add the top level mutation
  type Mutation {
    createMessage(text: String): Message
  }

  #Existing code...
`;

This will define a mutation in our schema called createMessage that takes a text string parameter. This will hold the contents of a message sent by an anonymous user, which we'll use to send to Slack.

Extending the conversations data source

Before we create our resolver, we will need to extend our conversations API data source that we'll need in our new resolver. We will need to add three new methods for interacting with the Slack API to start a conversation.

Open the file api/slack-integration/data-sources/conversations.js and at the end of the class, add the createConversation method:

File path icon web-api/api/slack-integration/data-sources/conversations.js
// Existing code...

module.exports = class ConversationsAPI {
  // Existing code...

  /**
   * Create a Slack channel
   */
  createConversation(name, userIds) { // <-- New method
    const { webAPI } = connection.getConnection();

    return webAPI.conversations.create({ name, user_ids: userIds })
      .then(res => res.channel);
  }
}

The createConversation method will create a Slack channel named with the value name and add users to the channel from the array of userIds.

The next method we will create will be for inviting users to a channel.

File path icon web-api/api/slack-integration/data-sources/conversations.js
module.exports = class ConversationsAPI {
  // Existing code...

  createConversation(name, userIds) {
    // Existing code...
  }

  /**
   * Invite users to a Slack channel
   */
  inviteUser({ channelId, userIds }) { // <-- New method
    const { webAPI } = connection.getConnection();

    return webAPI.conversations.invite({ channel: channelId, users: userIds.join(',') })
      .then(res => res.channel);
  }
}

This method takes a channelId and list of user ids that are used to join the channel.

We have our methods for creating a conversation channel and inviting users to the channel. The last method we will add to our data source will be used to actually post a message to the new channel. We can add this to the end of our class:

File path icon web-api/api/slack-integration/data-sources/conversations.js
module.exports = class ConversationsAPI {
  // Existing code...

  createConversation(name, userIds) {
    // Existing code...
  }

  inviteUser({ channelId, userIds }) {
    // Existing code...
  }

  /**
   * Create a message in a channel
   */
  chatPostMessage(channelId, text) { // <-- New method
    const { webAPI } = connection.getConnection();

    return webAPI.chat.postMessage({ channel: channelId, text })
      .then(res => res.message);
  }
}

Again, we are using the shared Slack connection to call chat.postMessage with a channel id and text option then return the message from the response. The webAPI methods all return asynchronous Promises.

With the ability to create a message in a new Slack channel, we are all set to now integrate our resolver function.

Create the resolver

Just like GraphQL queries, we need to create a resolver to handle the new incoming request.

In your terminal or command prompt, change directory to the web-api folder within your project folder then create the file api/slack-integration/resolvers/messages.createMessage.mutation.js and open it.

touch ./api/slack-integration/resolvers/messages.createMessage.mutation.js

At the top of the file we need to import the slackMessageMap function we previously used for our query and then create a new function definition that we'll export as our GraphQL resolver.

File path icon web-api/api/slack-integration/resolvers/messages.createMessage.mutation.js
const slackMessageMap = require('../../slack-integration/data-mappings/message');

module.exports = async function createMessage (parent, args, { dataSources }) {

};

The async keyword will allow us to use the await keyword in the function body. This will pause execution of the function to resolve a promise and get the result before moving on to execute the next line of code. We can do this by adding the next two lines.

File path icon web-api/api/slack-integration/resolvers/messages.createMessage.mutation.js
const slackMessageMap = require('../../slack-integration/data-mappings/message');

module.exports = async function createMessage (parent, args, { dataSources }) {
  const channelName = 'conversation-1';
  const conversation = await dataSources.conversationsAPI.getConversation(channelName);
};

We first need to check if our conversation exists before we create a message. We will also create another async method that we will conditionally call to create a new conversation if one doesn't exist.

File path icon web-api/api/slack-integration/resolvers/messages.createMessage.mutation.js
const slackMessageMap = require('../../slack-integration/data-mappings/message');

module.exports = async function createMessage (parent, args, { dataSources }) {
  const channelName = 'conversation-1';
  const conversation = await dataSources.conversationsAPI.getConversation(channelName);

  async function createConversation() { // <-- New async method
    return dataSources.conversationsAPI.createConversation(channelName, [process.env.SLACK_ME_ID])
      .then(channel => dataSources.conversationsAPI.inviteUser({
        channelId: channel.id,
        userIds: [process.env.SLACK_ME_ID]
      }));
  }
};

In the new method, we call our createConversation and inviteUser methods to first create a conversation, then invite the relevant user to the conversation using the environment variable we previously set (process.env.SLACK_ME_ID). Because our Slack app creates the channel, it is automatically a member of it, so we don't need to invite the app as well.

We can now add the code that will conditionally call these methods if a conversation doesn't exist to first create the channel and id or use the existing found channel id needed to post a new message. After the message has been created, we return a mapped version of the message data to the client using the function slackMessageMap.

File path icon web-api/api/slack-integration/resolvers/messages.createMessage.mutation.js
const slackMessageMap = require('../../slack-integration/data-mappings/message');

module.exports = async function createMessage (parent, args, { dataSources }) {
  const channelName = 'conversation-1';
  const conversation = await dataSources.conversationsAPI.getConversation(channelName);

  async function createConversation() {
    // Existing code...
  }

  // New return value
  return (conversation ? Promise.resolve(conversation) : createConversation())
    .then(channel => dataSources.conversationsAPI.chatPostMessage(channel.id, args.text))
    .then(slackMessageMap);
};

Finally, we have to register our new resolver to the exported object that will get included in the resolvers we pass to makeExecutableSchema.

Open the file index.js. We need to import our new mutation resolver and declare it in our exports object.

File path icon web-api/index.js
// Existing code...
const createMessage = require('./messages.createMessage.mutation'); // <-- New import

const schema = makeExecutableSchema({
  // Existing code...
  resolvers: {
    Query: {
      // Existing code...
    },

    Mutation: { // <-- New property to add
      createMessage
    },
  }
});

Testing our GraphQL Mutation

We can now test our new mutation. Make sure your API is running (restart it with node index.js to pick up our changes), then open a browser window and navigate to Apollo Sandbox at: http://localhost:4123/graphql.

If you open the schema reference from the left-hand sidebar, you'll see our new definition createMessage under the Mutations heading. Clicking this will show all the options we can send with this type of query (in this case just text).

In the Operation panel, replace the example query with the following code and click the run button:

mutation createMessage {
  createMessage(text: "Send me") {
    id
    userId
    text
    fromYou
    timestamp
    user {
      id
      name
      colour
      avatarUrl
    }
  }
}

If you have the Slack application installed to your computer and have desktop notifications enabled, you should get an instant notification from your operating system with details of the new message.

If you open your installed Slack app, you should see a new entry in the conversation-1 channel. Notice that the message 'Send me' appears immediately after sending. This is because Apollo will emit the new result to all subscribers after the message cached automatically with Apollo.

You can inspect the Apollo cache with the Apollo Dev Tools plugin for your browser.