mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: init graphql
This commit is contained in:
1 parent
2097c7257f
commit
c39c406535
16 files changed
+131
-23
No files matched your search
@@ -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 = [
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user