Start a conversation

Create a Forum Topic

Learn how to create a forum message and a forum message with attachments using a GraphQL mutation in a forum board.

Requirements

You must have the Start discussions and new content permission set to Grant by Administrator.

Create a Forum permission
Forum Message permission

Basic Steps

  1. Open the forum board where you want to create a forum message.
  2. Identify the ID of the forum board from the URL of the board.
Forum Board Identification
Forum Board Identification
  1. Ensure that you have the necessary permission to create the message granted by the Administrator.
  2. Create a POST request to create a message in the desired board.
  3. Verify in the forum board that the message is created.

Example

Create a Forum Message

In this example, we are going to create a forum message in the Klass forum board using a GraphQL query.
After you have created a forum message in the board, verify in the UI that the message is created successfully.

  1. Sign in to your community.
  2. Go to the Klass Forum Board.
  3. Make a note of the ID of the board.
  4. Make a GraphQL POST request to the board.
  5. Verify in the UI that the forum message has been successfully created.

The ID of the Klass Forum Board is klass-forum-board.
Generate the session key using POST/authentication/sessions/login with the member’s credentials.
Here is the GraphQL POST request and response to create a forum message.

mutation createTopic($createInput: CreateForumTopicInput!) {
  createForumTopic(createInput: $createInput) {
    result {
      id
      subject
      uid
      body
    }
  }
}

The mutation here initiates a createTopic function using information passed through GraphQL variables. Here is an example to create a forum topic.

{
  "createInput": {
    "board": {
      "id": "BOARD:klass-forum-board"
    },
    "subject": "Test Message",
    "body": "Horticulture became one of my hobbies during the pandemic where I started cultivating tomato, Lemon, Spinach, Mint leaves, and some herbs in a small area. During the initial stage, I was not familiar with gardening procedure and later, I came to know what manures I need to use to get a good yield which helped me a lot. Horticulture provides knowledge about the soil, the fertiliser and so on. Furthermore, you can do the horticulture on your own. In future, I will expand my small area to a big garden."
  }
}

Verify in the Klass Forum Board that the message has been successfully created.

Verification for the Forum Message in UI
Verification for the Forum Message in UI

Create Forum Message with Attachments

In this example, we are going to create a forum message with image attachments in the Klass forum board using a GraphQL query.
After you have created a forum message in the board with attachments, verify in the UI of your community that the message is completed successfully.

  1. Sign in to your community.
  2. Go to the Klass Forum Board.
  3. Make a note of the ID of the board.
  4. Create a forum message by making a POST request to the /api/2.1/graphql endpoint with a form-data body type and icon file.
    In the GraphQL POST request, we have added three parameters:
    Form-data parameter Description
    operations This parameter has the mutation query in a form-data. Using this parameter, we are passing the query parameters to create the forum message.
    map This parameter maps the input query with the attachments file.
    file This parameter adds your image from your machine to the query.

The ID of the Klass Forum Board is klass-forum-board.

Generate the session key using POST/authentication/sessions/login with the member’s credentials.

Here is the GraphQL POST request and response to create a forum message.

mutation createTopic($createInput: CreateForumTopicInput!) {
  createForumTopic(createInput: $createInput) {
    result {
      id
      subject
    }
  }
}
"variables": {
  "createInput": {
    "board": {
      "id": "BOARD:klass-forum-board"
    },
    "subject": "Bike Trip with the Mighty Black Knight",
    "body": "Bike Trip experience was a wonderful experience after a long time. With the Mighty Black Knight (The Black FZ 220), I went to experience some trekking at an altitude. Looking forward to know more about your hiking experiences.",
    "attachments": {
      "items": {
        "file": null
      }
    }
  }
}

The above operations and variables parameters are combined together to form the below operations query as a form-data.

{
  "query": "mutation createTopic($createInput: CreateForumTopicInput!) { createForumTopic(createInput: $createInput) { result { id subject } } }",
  "variables": {
    "createInput": {
      "board": {
        "id": "BOARD:klass-forum-board"
      },
      "subject": "Bike Trip with the Mighty Black Knight",
      "body": "Bike Trip experience was a wonderful experience after a long time. With the Mighty Black Knight (The Black FZ 220), I went to experience some trekking at an altitude. Looking forward to know more about your hiking experiences.",
      "attachments": {
        "items": {
          "file": null
        }
      }
    }
  }
}

The map parameter in the form-data is given below:

{"file":["variables.createInput.attachments.items.file"]}

The file parameter in the form-data is given below:

/Users/<USER-NAME>/Downloads/Icon.png

The above three parameters (operations query, map, and file) are combined as a single form-data request to create a forum message with attachments.

The response for the form-data request is given below:

{
  "data": {
    "createForumTopic": {
      "result": {
        "id": "message:8250",
        "subject": "Bike Trip with the Mighty Black Knight"
      }
    }
  }
}

Verify in the UI that the forum message has been successfully created with attachments.

Forum Message with Attachments Verification
Forum Message with Attachments Verification
Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. ATLAS

  2. Posted
  3. Updated

Comments