Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3] Use Typescript in admin scripts #338

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5c38ac9
refactor: rewriting blockUser and util to TS
MrOrz May 13, 2024
fe4583e
refactor: blockUser script uses typescript defn from rumors-db
MrOrz Jun 23, 2024
1bae4c2
chore(rumors-db): update
MrOrz Jun 23, 2024
6f75058
refactor(CreateOrUpdateArticleReplyFeedback): rewrite into TS
MrOrz Jun 23, 2024
0aed9e5
fix: types
MrOrz Jun 23, 2024
7850990
fix(pacakge.json): add type for cli-progress
MrOrz Jul 14, 2024
02bfa0f
feat(graphql): type dataloaders
MrOrz Jul 14, 2024
38f3242
refactor(util): rewrite util/user to Typeascript
MrOrz Jul 14, 2024
4fdf1a9
fix(package.json): typing lodash
MrOrz Jul 14, 2024
d1846a7
fix(dataLoaders): make _checkOrSetLoader don't return undefined
MrOrz Jul 14, 2024
e4fb5ad
refactor(index): extract and type GraphQL context factory function
MrOrz Jul 14, 2024
f69f225
refactor(util): assertUser can narrow down param
MrOrz Jul 14, 2024
b3416a3
refactor(CreateOrUpdateArticleReplyFactory): typing rootValue and con…
MrOrz Jul 14, 2024
0c5cfff
fix(graphql): property type updateArticleReplyStatus
MrOrz Jul 14, 2024
cfc1d0d
fix(util): getAvailableAvatarTypes handles undefined
MrOrz Jul 14, 2024
627ecf6
fix(util): fix user test
MrOrz Jul 14, 2024
5400da3
fix(createBacendUsers): skip test because they are one-off scripts an…
MrOrz Jul 14, 2024
dd31183
chore(src): update src/rumors-db to latest
MrOrz Jul 14, 2024
87d2e27
fix(CreateCategory): move error check in front of accessing data
MrOrz Jul 14, 2024
7291c63
chore: use newer categories DB schema
MrOrz Jul 14, 2024
9a2da2f
fix(pacakge.json): fix posttest as it now imports typescript files
MrOrz Jul 14, 2024
ddb3f3c
fix: use latest schema
MrOrz Jul 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"seed": "cd src/rumors-db && npm run seed",
"pretest": "npm run rumors-db:install && npm run rumors-db:test && mkdir -p build",
"test": "NODE_ENV=test ELASTICSEARCH_URL=http://localhost:62223 jest --runInBand",
"posttest": "NODE_ENV=test ELASTICSEARCH_URL=http://localhost:62223 babel-node test/postTest.js",
"posttest": "NODE_ENV=test ELASTICSEARCH_URL=http://localhost:62223 babel-node --extensions .ts,.js test/postTest.js",
"start": "pm2-runtime start ecosystem.config.js --env production",
"lint": "eslint src/.",
"lint:fix": "eslint --fix src/.",
Expand Down Expand Up @@ -75,6 +75,9 @@
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.24.1",
"@google-cloud/storage": "^6.11.0",
"@types/cli-progress": "^3.11.6",
"@types/dotenv": "^8.2.0",
"@types/lodash": "^4.17.6",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"apollo-server-testing": "^2.18.2",
Expand Down
42 changes: 42 additions & 0 deletions src/contextFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import DataLoaders from './graphql/dataLoaders';
import { createOrUpdateUser } from './util/user';

type ContextFactoryArgs = {
ctx: {
appId: string;
query: { userId?: string };
state: { user?: { userId?: string } };
};
};

export default async function contextFactory({ ctx }: ContextFactoryArgs) {
const {
appId,
query: { userId: queryUserId } = {},
state: { user: { userId: sessionUserId } = {} } = {},
} = ctx;

const userId = queryUserId ?? sessionUserId;

let currentUser = null;
if (appId && userId) {
({ user: currentUser } = await createOrUpdateUser({
userId,
appId,
}));
}

return {
loaders: new DataLoaders(), // new loaders per request
user: currentUser,

// userId-appId pair
//
userId: currentUser?.id,
appUserId: userId,
appId,
};
}

/** GraphQL resolver context */
export type ResolverContext = Awaited<ReturnType<typeof contextFactory>>;
101 changes: 0 additions & 101 deletions src/graphql/dataLoaders/index.js

This file was deleted.

98 changes: 98 additions & 0 deletions src/graphql/dataLoaders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import docLoaderFactory from './docLoaderFactory';
import analyticsLoaderFactory from './analyticsLoaderFactory';
import articleRepliesByReplyIdLoaderFactory from './articleRepliesByReplyIdLoaderFactory';
import articleCategoriesByCategoryIdLoaderFactory from './articleCategoriesByCategoryIdLoaderFactory';
import articleReplyFeedbacksLoaderFactory from './articleReplyFeedbacksLoaderFactory';
import articleCategoryFeedbacksLoaderFactory from './articleCategoryFeedbacksLoaderFactory';
import searchResultLoaderFactory from './searchResultLoaderFactory';
import urlLoaderFactory from './urlLoaderFactory';
import repliedArticleCountLoaderFactory from './repliedArticleCountLoaderFactory';
import votedArticleReplyCountLoaderFactory from './votedArticleReplyCountLoaderFactory';
import userLevelLoaderFactory from './userLevelLoaderFactory';
import userLoaderFactory from './userLoaderFactory';
import contributionsLoaderFactory from './contributionsLoaderFactory';

const LOADER_FACTORY_MAP = {
docLoader: docLoaderFactory,
articleRepliesByReplyIdLoader: articleRepliesByReplyIdLoaderFactory,
articleCategoriesByCategoryIdLoader:
articleCategoriesByCategoryIdLoaderFactory,
articleReplyFeedbacksLoader: articleReplyFeedbacksLoaderFactory,
articleCategoryFeedbacksLoader: articleCategoryFeedbacksLoaderFactory,
searchResultLoader: searchResultLoaderFactory,
urlLoader: urlLoaderFactory,
repliedArticleCountLoader: repliedArticleCountLoaderFactory,
votedArticleReplyCountLoader: votedArticleReplyCountLoaderFactory,
userLoader: userLoaderFactory,
userLevelLoader: userLevelLoaderFactory,
analyticsLoader: analyticsLoaderFactory,
contributionsLoader: contributionsLoaderFactory,
} as const;

type LoaderFactoryMap = typeof LOADER_FACTORY_MAP;

export default class DataLoaders {
// List of data loaders
//
get docLoader() {
return this._checkOrSetLoader('docLoader');
}
get articleRepliesByReplyIdLoader() {
return this._checkOrSetLoader('articleRepliesByReplyIdLoader');
}
get articleCategoriesByCategoryIdLoader() {
return this._checkOrSetLoader('articleCategoriesByCategoryIdLoader');
}
get articleReplyFeedbacksLoader() {
return this._checkOrSetLoader('articleReplyFeedbacksLoader');
}
get articleCategoryFeedbacksLoader() {
return this._checkOrSetLoader('articleCategoryFeedbacksLoader');
}
get searchResultLoader() {
return this._checkOrSetLoader('searchResultLoader');
}
get urlLoader() {
return this._checkOrSetLoader('urlLoader');
}
get repliedArticleCountLoader() {
return this._checkOrSetLoader('repliedArticleCountLoader');
}
get votedArticleReplyCountLoader() {
return this._checkOrSetLoader('votedArticleReplyCountLoader');
}
get userLoader() {
return this._checkOrSetLoader('userLoader');
}
get userLevelLoader() {
return this._checkOrSetLoader('userLevelLoader');
}
get analyticsLoader() {
return this._checkOrSetLoader('analyticsLoader');
}
get contributionsLoader() {
return this._checkOrSetLoader('contributionsLoader');
}

_loaders: {
-readonly [key in keyof LoaderFactoryMap]?: ReturnType<
LoaderFactoryMap[key]
>;
};

// inner-workings
//
constructor() {
this._loaders = {};
}

_checkOrSetLoader<N extends keyof LoaderFactoryMap>(
name: N
): ReturnType<LoaderFactoryMap[N]> {
const cached = this._loaders[name];
if (cached) return cached;

this._loaders[name] = LOADER_FACTORY_MAP[name](this);
return this._loaders[name] as ReturnType<LoaderFactoryMap[N]>;
}
}
Loading
Loading