Skip to main content

Create Post

createPost mutation is used to create a new post in a space. It takes spaceId and CreatePostInput as inputs.

Each post has a type which defines the fields of it. You can use postTypes query to retrieve the available post types in your community.

postTypes(limit: 10) {
nodes {
id
name
mappings {
key
type
required
}
}
}

mapping field in postType defines the fields of the posts with this type. It is an array of PostTypeMapping. The important attributes of it are key, type, and required.

  • key: the name of this field used to identify it
  • type: the type of this field can be one of PostMappingTypeEnum
  • required: indicate whether this field is required when creating a post

An example of a postType can be:

{
"id": "YP4thX1J1SMMsuw",
"name": "Discussion",
"mappings": [
{
"key": "title",
"type": "text",
"required": null
},
{
"key": "content",
"type": "html",
"required": true
},
{
"key": "previewImageId",
"type": "image",
"required": null
}
]
}

This is the post type for discussion. It has 2 required fields, title and content. title should have a text value but content can contain html. It also has a non-required field, previewImageId, whose type is image.

Create a post#

To create a post you need to define its post type. We will use the discussion post type mentioned above. The value of the fields (i.e. title and content) is passed in the mappingFields. The value should be a valid stringified json object. Typically, in JS you can use JSON.stringify().

mutation {
createPost(
spaceId: "0CXG3DhjnlRZ",
input: {
postTypeId: "YP4thX1J1SMMsuw"
mappingFields: [
{
key: "title"
type: text
value: "\"Get started by introducing yourself in the Community\""
},
{
key: "content"
type: html
value: "\"<p>Introduce yourself, spark connections and conversation, and welcome other Tribe Campfire Community members as we continue to grow.</p>\""
}
]
publish: true
}
) {
id
}
}

Reply to Post#

createReply mutation is used to reply to a post. It takes postId and CreatePostInput as inputs.

For example, we can reply with a comment on the post we created above. The post type of comment is this.

{
"id": "XSaq81Sedx2pe4R",
"name": "Comment",
"mappings": [
{
"key": "content",
"type": "html",
"required": true
},
{
"key": "previewImageId",
"type": "image",
"required": null
}
]
}

Now we can create a reply using this post type.

mutation {
createReply(postId: "3ReqMxgxaF9YXPQ", input: {
postTypeId: "XSaq81Sedx2pe4R",
mappingFields: [
{
key: "content"
type: html
value: "\"<p>Hello! I'm Siavash, CEO at Tribe.</p>\""
}
]
publish: true
}) {
id
}
}

Create Post with Attachments#

To create a post with attachments you have to first upload the attachments using upload files. Then you can pass the ids of the files as attachmentIds.

mutation {
createPost(
spaceId: "0CXG3DhjnlRZ",
input: {
postTypeId: "XSaq81Sedx2pe4R"
mappingFields: [
{
key: "content"
type: html
value: "\"<p>This is the content</p>\""
}
]
publish: true
attachmentIds: ["GFfd6sdfgG", "Zn92Jdofd"]
}
) {
id
}
}

Create Post with Embedded Image#

To embed images in the post first you need to upload images. Then you can use the mediaDownloadUrl of the uploaded images in an img tag.

mutation {
createPost(
spaceId: "0CXG3DhjnlRZ",
input: {
postTypeId: "XSaq81Sedx2pe4R"
mappingFields: [
{
key: "content"
type: html
value: "\"<img src=\\\"https://tribe-development.imgix.net/yRoVIDMiIQEMCsDXBrtxr?w=1000&amp;auto=compress,format&amp;dl\\\">\""
}
]
publish: true
}
) {
id
}
}

Create Post with Embedded Video#

To embed videos in the post first you need to upload them. Then you can use the mediaDownloadUrl of the uploaded files in a video tag.

mutation {
createPost(
spaceId: "0CXG3DhjnlRZ",
input: {
postTypeId: "XSaq81Sedx2pe4R"
mappingFields: [
{
key: "content"
type: html
value: "\"<video controls=\\\"true\\\" src=\\\"https://files.dev.t-cdn.net/files/ffVvFW0AABz7TX4LySDhl\\\"></video>\""
}
]
publish: true
}
) {
id
}
}

Similarly, you can use the same approach to embed anything else (e.g. audio) to the posts.

Create Post with Embedded Links#

You can embed links to your post. For example, you can add a YouTube video.

mutation {
createPost(
spaceId: "0CXG3DhjnlRZ",
input: {
postTypeId: "XSaq81Sedx2pe4R"
mappingFields: [
{
key: "content"
type: html
value: "\"<div data-type=\\\"embed\\\" data-id=\\\"BCvHaomcZ1pFYU2xd0MG3\\\" data-embed-url=\\\"https://www.youtube.com/watch?v=dQw4w9WgXcQ\\\"></div>\""
}
]
publish: true
}
) {
id
}
}