Skip to content

Commit

Permalink
feat: add ts support
Browse files Browse the repository at this point in the history
  • Loading branch information
Carrotzpc committed Apr 17, 2019
1 parent 1fbe6e2 commit 4e27797
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 118 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
3.1.0-beta.1 / 2019-04-17 (by Carrotzpc)
==================

* add ts support

3.0.0 / 2019-04-14 (by Carrotzpc)
==================

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ exports.middleware = [ 'graphql' ];

## 参考文章

- [graphql官网](http://facebook.github.io/graphql)
- [graphql 官网](http://facebook.github.io/graphql)

- [如何在egg中使用graphql](https://zhuanlan.zhihu.com/p/30604868)
- [如何在 egg 中使用 graphql](https://zhuanlan.zhihu.com/p/30604868)

- [项目例子:结合sequelize](https://github.com/freebyron/egg-graphql-boilerplate)
- [项目例子:结合 sequelize](https://github.com/freebyron/egg-graphql-boilerplate)

## 协议

Expand Down
4 changes: 1 addition & 3 deletions agent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = agent => {
require('./lib/load_schema')(agent);
require('./lib/load_connector')(agent);
require('./lib/loader')(agent);
};

4 changes: 1 addition & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = app => {
require('./lib/load_schema')(app);
require('./lib/load_connector')(app);
require('./lib/loader')(app);
};

2 changes: 0 additions & 2 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
* connector instance
* @member Context#connector
*/

get connector() {
/* istanbul ignore else */
if (!this[SYMBOL_CONNECTOR]) {
Expand All @@ -25,7 +24,6 @@ module.exports = {
* graphql instance access
* @member Context#graphql
*/

get graphql() {
return this.service.graphql;
},
Expand Down
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

declare module 'egg' {
export interface IConnector extends PlainObject {}

// extend context
interface Context {
connector: IConnector;
}

// extend your config
interface EggAppConfig {
graphql: any;
}

}
31 changes: 0 additions & 31 deletions lib/load_connector.js

This file was deleted.

74 changes: 0 additions & 74 deletions lib/load_schema.js

This file was deleted.

116 changes: 116 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

const { join, dirname } = require('path');
const { merge, isFunction } = require('lodash');
const is = require('is-type-of');
const {
makeExecutableSchema,
SchemaDirectiveVisitor,
} = require('graphql-tools');

const SYMBOL_SCHEMA = Symbol('Application#schema');
const SYMBOL_CONNECTOR_CLASS = Symbol('Application#connectorClass');

module.exports = app => {
const directiveResolvers = {};
const schemaDirectives = {};
const resolvers = {};
const typeDefs = [];

class GraphqlLoader {
constructor(app) {
this.app = app;
}

load() {
const connectorClasses = new Map();
this.loadGraphql(connectorClasses);
this.loadTypeDefs();
/**
* create a GraphQL.js GraphQLSchema instance
*/
Object.defineProperties(this.app, {
schema: {
get() {
if (!this[SYMBOL_SCHEMA]) {
this[SYMBOL_SCHEMA] = makeExecutableSchema({
typeDefs,
resolvers,
directiveResolvers,
schemaDirectives,
});
}
return this[SYMBOL_SCHEMA];
},
},
connectorClass: {
get() {
if (!this[SYMBOL_CONNECTOR_CLASS]) {
this[SYMBOL_CONNECTOR_CLASS] = connectorClasses;
}
return this[SYMBOL_CONNECTOR_CLASS];
},
},
});
}
// 加载 graphql
loadGraphql(connectorClasses) {
const loader = this.app.loader;
loader.timing.start('Loader Graphql');
const opt = {
caseStyle: 'lower',
directory: join(this.app.baseDir, 'app/graphql'),
target: {},
initializer: (obj, opt) => {
const pathName = opt.pathName.split('.').pop();
// 加载 resolver
if (pathName === 'resolver') {
if (isFunction(obj)) {
obj = obj(this.app);
}
merge(resolvers, obj);
}
// load schemaDirective
if (is.class(obj)) {
const proto = Object.getPrototypeOf(obj);
if (proto === SchemaDirectiveVisitor) {
const name = opt.pathName.split('.').pop();
schemaDirectives[name] = obj;
}
}
if (pathName === 'schemaDirective') {
merge(schemaDirectives, obj);
}
// load directiveResolver
if (pathName === 'directive') {
merge(directiveResolvers, obj);
}
// load connector
if (pathName === 'connector') {
// 获取文件目录名
const type = dirname(opt.path)
.split(/\/|\\/)
.pop();
connectorClasses.set(type, obj);
}
},
};
new this.app.loader.FileLoader(opt).load();
loader.timing.end('Loader Graphql');
}
// 加载 typeDefs
loadTypeDefs() {
const opt = {
directory: join(this.app.baseDir, 'app/graphql'),
match: '**/*.graphql',
target: {},
initializer: obj => {
typeDefs.push(obj.toString('utf8'));
},
};
new this.app.loader.FileLoader(opt).load();
}
}

new GraphqlLoader(app).load();
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@switchdog/egg-graphql",
"version": "3.0.0",
"version": "3.1.0-beta.1",
"description": "egg graphql plugin",
"eggPlugin": {
"name": "graphql"
Expand Down Expand Up @@ -49,7 +49,8 @@
"agent.js",
"config",
"app",
"lib"
"lib",
"index.d.ts"
],
"ci": {
"version": "8, 9"
Expand Down

0 comments on commit 4e27797

Please sign in to comment.