-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiApi.js
91 lines (85 loc) · 2.01 KB
/
wikiApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { GraphQLClient, gql } from 'graphql-request';
import dotenv from 'dotenv';
dotenv.config();
const client = new GraphQLClient(process.env.WIKI_API_URL, {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.WIKI_API_TOKEN}`,
},
});
export const fetchAllPages = async (locale) => {
const query = gql`
query($locale: String!){
pages {
list(locale: $locale) {
id
locale
path
title
description
}
}
}
`;
const variables = { locale };
const data = await client.request(query, variables);
return data.pages.list;
};
export const fetchSinglePages = async (id) => {
const query = gql`
query ($id: Int!) {
pages {
single(id: $id) {
id
locale
path
title
description
content
}
}
}
`;
const variables = { id };
const data = await client.request(query, variables);
return data.pages.single;
};
export const createPage = async (title, content, path, locale) => {
const mutation = `
mutation($title: String!, $path: String!, $locale: String!, $content: String!) {
pages {
create(title: $title, content: $content, path: $path, locale: $locale, isPublished: true, isPrivate: false, tags: [], editor: "markdown", description: "") {
page {
id
}
}
}
}
`;
const variables = {
title, content, path, locale
};
const data = await client.request(mutation, variables);
return data.pages.create;
};
export const checkPageExists = async (path, locale) => {
const query = `
query($path: String!, $locale: String!) {
pages {
singleByPath(path: $path, locale: $locale) {
id
}
}
}
`;
const variables = { path, locale };
try {
const data = await client.request(query, variables);
return !!data.pages.singleByPath;
} catch (error) {
if (error.response.errors[0].message === "This page does not exist.") {
return false;
}
throw error;
}
};