Skip to content

Commit

Permalink
Add safelist fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavjitChauhan committed Oct 11, 2023
1 parent 231c3bf commit 1c02d89
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "npx vue-cli-service serve",
"serve": "set NODE_OPTIONS=--openssl-legacy-provider && npx vue-cli-service serve",
"build": "npx vue-cli-service build",
"sitemap": "npx vue-cli-service sitemap"
},
Expand Down
39 changes: 27 additions & 12 deletions client/src/util/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ function hotlistBody({
} = {}) {
return {
query: `query hotlist($curationNodeId: String, $onlyOfficialProjectSpinoffs: Boolean!, $sort: ListProgramSortOrder, $pageInfo: ListProgramsPageInfo) {
listTopPrograms(curationNodeId: $curationNodeId, onlyOfficialProjectSpinoffs: $onlyOfficialProjectSpinoffs, sort: $sort, pageInfo: $pageInfo) {
listTopPrograms(
curationNodeId: $curationNodeId
onlyOfficialProjectSpinoffs: $onlyOfficialProjectSpinoffs
sort: $sort
pageInfo: $pageInfo
) {
complete
cursor
programs {
Expand All @@ -24,8 +29,7 @@ function hotlistBody({
}
__typename
}
}
`,
}`,
variables: {
curationNodeId,
onlyOfficialProjectSpinoffs,
Expand Down Expand Up @@ -67,8 +71,7 @@ function projectsAuthoredByUserBody({
}
__typename
}
}
`,
}`,
variables: {
kaid,
sort,
Expand All @@ -89,8 +92,19 @@ function feedbackQueryBody({
} = {}) {
return {
query: `query feedbackQuery($topicId: String!, $focusKind: String!, $cursor: String, $limit: Int, $feedbackType: FeedbackType!, $currentSort: Int, $qaExpandKey: String) {
feedback(focusId: $topicId, cursor: $cursor, limit: $limit, feedbackType: $feedbackType, focusKind: $focusKind, sort: $currentSort, qaExpandKey: $qaExpandKey, answersLimit: 1) {
feedback(
focusId: $topicId
cursor: $cursor
limit: $limit
feedbackType: $feedbackType
focusKind: $focusKind
sort: $currentSort
qaExpandKey: $qaExpandKey
answersLimit: 1
) {
feedback {
isLocked
isPinned
replyCount
appearsAsDeleted
author {
Expand Down Expand Up @@ -145,6 +159,8 @@ function feedbackQueryBody({
... on QuestionFeedback {
hasAnswered
answers {
isLocked
isPinned
replyCount
appearsAsDeleted
author {
Expand Down Expand Up @@ -204,6 +220,8 @@ function feedbackQueryBody({
}
... on AnswerFeedback {
question {
isLocked
isPinned
replyCount
appearsAsDeleted
author {
Expand Down Expand Up @@ -266,8 +284,7 @@ function feedbackQueryBody({
sortedByDate
__typename
}
}
`,
}`,
variables: {
topicId,
focusKind,
Expand Down Expand Up @@ -347,8 +364,7 @@ function programQueryBody({
width
__typename
}
}
`,
}`,
variables: {
programId
}
Expand All @@ -369,8 +385,7 @@ function avatarDataForProfileBody({
}
__typename
}
}
`,
}`,
variables: {
kaid
}
Expand Down
37 changes: 35 additions & 2 deletions server/lib/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const axios = require('axios').default;
const { getLatestQuery, getLatestMutation } = require('@bhavjit/khan-api');
const { AxiosError } = require('axios');


const instance = axios.create({
Expand Down Expand Up @@ -48,8 +50,39 @@ const get = async (path, params) => {
* @returns {Promise}
*/
const post = async (path, data) => {
const response = await instance.post(path, data)
return response.data;
try {
const response = await instance.post(path, data)
return response.data;
} catch (err) {
if (err instanceof AxiosError && err.response.status === 403) {
const { query } = data;
const isQuery = query.startsWith('query'),
operationName = query.match(/^(?:query|mutation) (\w+)/)?.[1]

if (!operationName)
throw new Error(`An unknown query is no longer in the safelist`)

console.warn(
`The query for operation "${operationName}" is no longer in the safelist. Attempting to fetch the latest version from the safelist...`
)
const latestQuery = isQuery
? await getLatestQuery(operationName)
: await getLatestMutation(operationName)

if (!latestQuery)
throw new Error(
`The query for operation "${operationName}" was not found in the safelist`
)

const newData = {
...data,
query: latestQuery,
}

const response = await instance.post(path, newData)
return response.data
} else throw err;
}
};

/**
Expand Down
103 changes: 103 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "Bhavjit Chauhan",
"license": "MIT",
"dependencies": {
"@bhavjit/khan-api": "^0.6.4",
"axios": "^0.27.2",
"body-parser": "^1.20.1",
"chalk": "^4.1.2",
Expand Down

0 comments on commit 1c02d89

Please sign in to comment.