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.
Basic Steps
- Open the forum board where you want to create a forum message.
- Identify the ID of the forum board from the URL of the board.
- Ensure that you have the necessary permission to create the message granted by the Administrator.
- Create a POST request to create a message in the desired board.
- 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.
- Sign in to your community.
- Go to the Klass Forum Board.
- Make a note of the ID of the board.
- Make a GraphQL POST request to the board.
- 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.
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.
- Sign in to your community.
- Go to the Klass Forum Board.
- Make a note of the ID of the board.
- 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.
ATLAS
Comments