core: init graphql

This commit is contained in:
undefined committed 2021-08-23 02:39:58 +08:00
1 parent 2097c7257f
commit c39c406535
16 files changed
+131 -23

No files matched your search

+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.32.35",
"version": "2.33.0",
"bin": "bin/hydrooj.js",
"main": "src/loader",
"module": "src/loader",
@@ -19,6 +19,7 @@
"cookies": "^0.8.0",
"detect-browser": "^5.2.0",
"fs-extra": "^10.0.0",
"graphql": "^15.5.1",
"js-yaml": "^4.1.0",
"koa": "^2.13.1",
"koa-body": "^4.2.0",
+1 -1
View File
@@ -26,7 +26,7 @@ export const builtinModel = [
export const builtinHandler = [
'home', 'problem', 'record', 'judge', 'user',
'contest', 'training', 'discussion', 'manage', 'import',
'misc', 'homework', 'domain', 'status',
'misc', 'homework', 'domain', 'status', 'api',
];
export const builtinScript = [
+3 -1
View File
@@ -78,5 +78,7 @@ export async function load(call: Entry) {
await modelSystem.set('db.ver', dbVer);
}
await bus.serial('app/started');
return await modelSystem.get('server.worker');
return typeof argv.options.worker === 'number'
? argv.options.worker
: await modelSystem.get('server.worker');
}
+50
View File
@@ -0,0 +1,50 @@
import graphql from 'graphql';
import UserModel from '../model/user';
import { Handler, Route } from '../service/server';
const schemaStr = `\
type Query {
user(id: Int, uname: String, mail: String): User
problem(id: Int): String
}
type User {
_id: Int!
uname: String!
mail: String!
}`;
const schema = graphql.buildSchema(schemaStr);
const root = {
user: (arg, ctx) => {
if (arg.id) return UserModel.getById(ctx.domainId, arg.id);
if (arg.mail) return UserModel.getByEmail(ctx.domainId, arg.mail);
if (arg.uname) return UserModel.getByUname(ctx.domainId, arg.uname);
return ctx.user;
},
problem: async (arg, ctx, node) => {
},
};
class ApiHandler extends Handler {
async get() {
const q = decodeURIComponent(this.ctx.request.querystring);
if (q === 'schema') {
this.response.body = { schema: schemaStr };
} else if (q) {
this.response.type = 'application/json';
this.response.body = await graphql.graphql(schema, q, root, this);
} else this.response.template = 'api.html';
}
async post() {
this.response.type = 'application/json';
this.response.body = await graphql.graphql(schema, this.args.query, root, this, this.args.variables);
}
}
export function apply() {
Route('api', '/api', ApiHandler);
}
global.Hydro.handler.api = apply;
+1 -1
View File
@@ -246,7 +246,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
else {
this.send({
html: await this.renderHTML('record_main_tr.html', {
rdoc, udoc, pdoc, tdoc,
rdoc, udoc, pdoc, tdoc, all: this.all,
}),
});
}
+4 -6
View File
@@ -439,7 +439,7 @@ export class Handler extends HandlerCommon {
query: ctx.query,
path: ctx.path,
params: ctx.params,
referer: ctx.request.headers.referer,
referer: ctx.request.headers.referer || '',
json: (ctx.request.headers.accept || '').includes('application/json'),
};
this.response = {
@@ -679,11 +679,9 @@ async function handle(ctx, HandlerClass, checker) {
h.domainId = args.domainId;
try {
const method = ctx.method.toLowerCase();
let operation: string;
if (method === 'post' && ctx.request.body.operation) {
operation = `_${ctx.request.body.operation}`
.replace(/_([a-z])/gm, (s) => s[1].toUpperCase());
}
const operation = (method === 'post' && ctx.request.body.operation)
? `_${ctx.request.body.operation}`.replace(/_([a-z])/gm, (s) => s[1].toUpperCase())
: '';
await bus.serial('handler/create', h);