Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加ts的支持 #15

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
合并master
  • Loading branch information
ShuaiShuaiguo committed Apr 15, 2019
commit 3026c756d157290cafb397bd7761fb5b5d8deef9
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ sudo: false
language: node_js
node_js:
- '8'
- '10'
- '9'
install:
- npm i npminstall && npminstall
script:
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ exports.graphql = {
};

// 添加中间件拦截请求
exports.middleware = [ 'graphql' ];
exports.middleware = ['graphql'];
```

## 使用方式
@@ -90,7 +90,7 @@ exports.middleware = [ 'graphql' ];
│ │ │ └── schemaDirective.js // 自定义 SchemaDirective
│  │  │ 
│   │   └── user // 一个graphql模型
│   │   ├── connector.js
│   │   ├── connector.js
│   │   ├── resolver.js
│   │   └── schema.graphql
│   ├── model
@@ -103,6 +103,8 @@ exports.middleware = [ 'graphql' ];

## 增加了 schemaDireactives 的支持

支持如上约定的目录结构,以及文件名作为指令名的方式.

## 参考文章

- [graphql 官网](http://facebook.github.io/graphql)
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
14 changes: 10 additions & 4 deletions lib/loader/graphql-loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { join, dirname } = require('path');
const { merge } = require('lodash');
const { merge, isFunction } = require('lodash');
const is = require('is-type-of');
const {
makeExecutableSchema,
@@ -65,21 +65,27 @@ module.exports = app => {
const pathName = opt.pathName.split('.').pop();
// 加载resolver
if (pathName === 'resolver') {
if (isFunction(obj)) {
obj = obj(this.app);
}
merge(resolvers, obj);
}
// 加载schemaDirective
// load schemaDirective
if (is.class(obj)) {
const proto = Object.getPrototypeOf(obj);
if (proto === SchemaDirectiveVisitor) {
const name = opt.pathName.split('.').pop();
schemaDirectives[name] = obj;
}
}
// 加载directiveResolver
if (pathName === 'schemaDirective') {
merge(schemaDirectives, obj);
}
// load directiveResolver
if (pathName === 'directive') {
merge(directiveResolvers, obj);
}
// 加载connector
// load connector
if (pathName === 'connector') {
// 获取文件目录名
const type = dirname(opt.path)
1 change: 1 addition & 0 deletions test/app/service/graphql.test.js
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ describe('test/plugin.test.js', () => {
);
assert.deepEqual(resp.data, { user: { upperName: 'NAME1' } });
});

it('should return createAt with @date directive', async () => {
const ctx = app.mockContext();
const resp = await ctx.service.graphql.query(
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

type Project {
name: String!
}
7 changes: 4 additions & 3 deletions test/fixtures/apps/graphql-app/app/graphql/user/connector.js
Original file line number Diff line number Diff line change
@@ -16,18 +16,19 @@ class UserConnector {
name: `name${id}`,
upperName: `name${id}`,
password: `password${id}`,
lowerName: `NAME${id}`,
createAt: 1528375304600,
projects: [],
}))
);
}

fetchByIds(ids) {
async fetchByIds(ids) {
return this.loader.loadMany(ids);
}

fetchById(id) {
return this.loader.load(id);
async fetchById(id) {
return await this.loader.load(id);
}
}