I recently started a little side project using wiki.js and wanted to be able to automatically create, search for, and edit wiki pages. wiki.js provides a graphql API to do this but documentation is a bit sparse. Here is a working example for creating a new page:
mutation = '''
mutation Page (
$content: String!,
$description: String!,
$editor:String!,
$isPublished:Boolean!,
$isPrivate:Boolean!,
$locale:String!,
$path:String!,
$tags:[String]!,
$title:String!
) {
pages {
create (
content:$content,
description:$description,
editor: $editor,
isPublished: $isPublished,
isPrivate: $isPrivate,
locale: $locale,
path: $path,
tags: $tags,
title:$title
) {
responseResult {
succeeded,
errorCode,
slug,
message
},
page {
id,
path,
title
}
}
}
}
'''
variables = {
'content': output_str, #string containing the page contents
'description': description, #string containing the page description
'editor': 'markdown', #I like to use the markdown editor
'isPublished': True, #should the page be published? i haven't found a way to publish it after the fact
'isPrivate': False, #should the page be visible to all users? i haven't found a way to publish it after the fact
'locale': 'en', #other languages also available
'path': '/show-notes/'+formatted_date, #URL path ie wiki.com/en/<your/path>
'tags': [entry.published.strip('+0000'), description], #optional strings to tag pages to speed up searching
'title': formatted_date #page title
}
response = requests.post(url, headers=headers, json={'query': mutation, 'variables': variables})
print(response.json())
Searching for a page (by title in this example):
#SEARCH
search_query = '''
query SearchPage($titles: String!) {
pages {
search(query: $titles) {
totalHits
results {
path
id
title
}
}
}
}
'''
search_variables = {
'titles': formatted_date #page title you are searching for
}
SEARCH
search_query = '''
query SearchPage($titles: String!) {
pages {
search(query: $titles) {
totalHits
results {
path
id
title
}
}
}
}
'''
search_variables = {
'titles': formatted_date
}
And lastly the one i had the most trouble with, updating a page:
mutation = '''
mutation UpdatePage($id: Int!, $tags: [String]!, $content: String!, $isPublished: Boolean!) {
pages {
update(id: $id, tags: $tags, content: $content, isPublished: $isPublished) {
responseResult {
message
}
page {
tags {
id
tag
title
}
}
}
}
}
'''
variables = {
'id': 66, #page ID which can be found via the search function noted above
'tags': ['test', 'test2'], #tags to overwrite existing tages
'content': 'Your updated page content here', #string containing data to overwrite the current page data
'isPublished': True
}
response = requests.post(url, headers=headers, json={'query': mutation, 'variables': variables})
print(response.json())
Note that the tags and page content must be updated or you will receive an error. The wiki.js site indicates that this will be changed in version 3 of wiki.js.