liangwei0101 / egg-demo Goto Github PK
View Code? Open in Web Editor NEWegg + ts + mongoose + restful +graphql + schedule demo
egg + ts + mongoose + restful +graphql + schedule demo
大佬的项目非常优雅,我想用在一个项目上,但是很需要 文件上传功能,也需要订阅功能,请求大佬增加。
目前在用分支:egg-ts-mongoose-graphql-websocket
如果Schema中定义的类型关联么一个User
XXXSchema{
belong: {type: Schema.Types.ObjectId, ref: 'User', select: true},
}
那在这个结构中的Interface对应的类型应该是什么呢?
IXXXSchema{
belong: ..???
}
谢谢
您好,看到您这个关于graphql在ts下的用法,想学习下,按照您的代码运行,egg服务正常启动,但是graphql服务失败,辛苦您,有时间时帮看下,谢谢!
失败信息:
[ '/Users/xugy/xugy/egg-space/mt.ik.sunyata/app/graphql/schemaResolver/studentResolver.ts']
(node:89254) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:89254) DeprecationWarning: This Package got moved, please use @hasezoey/typegoose
| github:hasezoey/typegoose
2019-12-28 18:15:43,376 INFO 89254 graphql server init
(node:89254) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
node_redis: Warning: Redis server does not require a password, but a password was supplied.
node_redis: Warning: Redis server does not require a password, but a password was supplied.
2019-12-28 18:15:43,470 INFO 89243 [master] egg started on http://127.0.0.1:7001 (2912ms) with STICKY MODE!
2019-12-28 18:18:08,043 ERROR 89254 [-/127.0.0.1/-/10ms POST /graphql?] nodejs.GraphQLError: Syntax Error: Unexpected
at syntaxError (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/error/syntaxError.js:15:10)
at Parser.unexpected (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/language/parser.js:1463:41)
at Parser.parseDefinition (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/language/parser.js:157:16)
at Parser.many (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/language/parser.js:1518:26)
at Parser.parseDocument (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/language/parser.js:111:25)
at Object.parse (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql/language/parser.js:36:17)
at Object.buildSchemaFromTypeDefinitions (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql-tools/src/generate/buildSchemaFromTypeDefinitions.ts:37:19)
at makeExecutableSchema (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/graphql-tools/src/makeExecutableSchema.ts:52:18)
at Application.get (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/egg-graphql/lib/load_schema.js:72:31)
at /Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/egg-graphql/app/middleware/graphql.js:68:23
at dispatch (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/koa/node_modules/koa-compose/index.js:42:32)
at passportSession (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/egg-passport/lib/framework.js:59:12)
at dispatch (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/koa/node_modules/koa-compose/index.js:42:32)
at passportInitialize (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/egg-passport/lib/framework.js:36:12)
at dispatch (/Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/koa/node_modules/koa-compose/index.js:42:32)
at /Users/xugy/xugy/egg-space/mt.ik.sunyata/node_modules/egg-development/app/middleware/egg_loader_trace.js:9:56
message: "Syntax Error: Unexpected "
locations: [{"line":1,"column":1}]
headers: {"Access-Control-Allow-Origin":"*","vary":"Origin"}
pid: 89254
hostname: xuMacBook-Pro.local
首先对作者表示非常非常的感谢,终于找到一个TS的框架能在上面做东西了(之前弄ts框架浪费好长好长时间),仔细看了下代码,有一个设计没有太明白,但在主页上没有找到作者的联系方式,只能在这里提问。
下面这段代码为什么要这样设计,会有什么好处呢?如果我要对它进行扩展字段,我应该怎么写才算是合理了。谢谢了。
并且希望能看到大佬的邮箱,可以向您交流学习。
/**
* 用户字段接口
*/
export interface IUser {
userNo: number;
userName: string;
}
/**
* 用户Document(实例方法在这写)
*/
export interface IUserDocument extends IUser, Document {
/**
* 实例方法接口(名称需要和Schema的方法名一样)
*/
userInstanceTestMethods: () => IUser;
}
/**
* 静态方法接口
*/
export interface IUserModel extends Model<IUserDocument> {
/**
* 静态方法
*/
userStaticTestMethods: () => IUser;
}
export const UserModel = model<IUserDocument, IUserModel>('User', UserSchema);
$ yarn tsc
yarn run v1.22.4
$ ets && tsc -p tsconfig.json
[egg-ts-helper] create typings/app/extend/application.d.ts (4ms)
[egg-ts-helper] create typings/app/controller/index.d.ts (1ms)
[egg-ts-helper] create typings/app/model/index.d.ts (1ms)
[egg-ts-helper] create typings/config/index.d.ts (15ms)
[egg-ts-helper] create typings/config/plugin.d.ts (1ms)
[egg-ts-helper] create typings/app/service/index.d.ts (1ms)
[egg-ts-helper] create typings/app/index.d.ts (1ms)
app/extend/application.ts:7:15 - error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters.
7 get graphql(this: Application): GraphQL {
~~~~~~~~~~~~~~~~~
Found 1 error.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
因为警告信息,无法启动生产环境的服务端
于是更新 typegoose 为 @typegoose/typegoose 后,因为新版typegoose去掉一些接口,程序出现很多报错
相关分支:egg-ts-mongoose-graphql-websocket
是关于 egg + egg-graphql + egg-mongoose + graphql-compose + graphql-compose-mongoose
的
我通过egg-mongoose
创建了model
,考虑到不想维护两套schema
,所以采用graphql-compose-mongoose
,将mongoose schema
转成graphql schema
我的实现步奏:
1.安装egg-graphql后,写入配置文件 config/config.default.ts
config.graphql = {
router: '/graphql',
// 是否加载到 app 上,默认开启
app: true,
// 是否加载到 agent 上,默认关闭
agent: false,
// 是否加载开发者工具 graphiql, 默认开启。路由同 router 字段。使用浏览器打开该可见。
graphiql: true,
//是否设置默认的Query和Mutation, 默认关闭
defaultEmptySchema: true,
};
2.像平常一样创建mongoose model, app/model/case.ts
module.exports = (app) => {
const schemaName = 'Case';
const Schema = app.mongoose.Schema;
const SchemaTypes = Schema.Types;
const conn = app.mongooseDB.get('main');
const mongooseSchema = new Schema(
{
name: { type: String, trim: true, required: true, match: /^.{1,20}$/ }, // 案例名称
description: { type: String, trim: true, match: /^[\s\S]{1,200}$/ }, // 描述
},
{ timestamps: true }
);
return conn.model(schemaName, mongooseSchema);
};
3.在app/graphql/case/resolver.js
const { composeMongoose } = require('graphql-compose-mongoose');
const { schemaComposer } = require('graphql-compose');
module.exports = (app) => {
const ctx = app.createAnonymousContext();
const modelName = 'Case';
// 这一步 我理解的就是 将mongoose schema转为 graphql schema
const graphqlTC = composeMongoose(ctx.model[modelName]);
console.log(graphqlTC)
schemaComposer.Query.addFields({
[modelName + 'findById']: graphqlTC.getResolver('findById'),
[modelName + 'findByIds']: graphqlTC.getResolver('findByIds'),
[modelName + 'findOne']: graphqlTC.getResolver('findOne'),
[modelName + 'findMany']: graphqlTC.getResolver('findMany'),
[modelName + 'count']: graphqlTC.getResolver('count'),
[modelName + 'pagination']: graphqlTC.getResolver('pagination'),
[modelName + 'connection']: graphqlTC.getResolver('connection'),
});
schemaComposer.Mutation.addFields({
[modelName + 'updateById']: graphqlTC.getResolver('updateById'),
[modelName + 'updateOne']: graphqlTC.getResolver('updateOne'),
[modelName + 'updateMany']: graphqlTC.getResolver('updateMany'),
[modelName + 'removeById']: graphqlTC.getResolver('removeById'),
[modelName + 'removeOne']: graphqlTC.getResolver('removeOne'),
[modelName + 'removeMany']: graphqlTC.getResolver('removeMany'),
[modelName + 'createOne']: graphqlTC.getResolver('createOne'),
[modelName + 'createMany']: graphqlTC.getResolver('createMany'),
});
};
这是graphql-compose-mongoose, 如果您方便的话 希望您能远程指导我一下,QQ:676114734 不胜感激
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.