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

Merged
merged 17 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -76,6 +76,9 @@
"@babel/preset-typescript": "^7.24.1",
"@google-cloud/storage": "^6.11.0",
"@types/node": "^18",
"@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