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
@@ -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;
|
||||
Reference in new issue
Block a user