forked from eggjs/egg-graphql
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
144 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters