Skip to content

Commit bf6485a

Browse files
whlsxlJasinYip
authored andcommitted
feat:pass other apollo server options. (#31)
1 parent d1cdb29 commit bf6485a

File tree

23 files changed

+361
-8
lines changed

23 files changed

+361
-8
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ exports.graphql = {
6767
onPreGraphQL: function* (ctx) {},
6868
// 开发工具 graphiQL 路由前的拦截器,建议用于做权限操作(如只提供开发者使用)
6969
onPreGraphiQL: function* (ctx) {},
70+
// apollo server的透传参数,参考[文档](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#parameters)
71+
apolloServerOptions: {
72+
rootValue,
73+
formatError,
74+
formatResponse,
75+
mocks,
76+
schemaDirectives,
77+
introspection,
78+
playground,
79+
debug,
80+
validationRules,
81+
tracing,
82+
cacheControl,
83+
subscriptions,
84+
engine,
85+
persistedQueries,
86+
cors,
87+
}
7088
};
7189

7290
// 添加中间件拦截请求

app/middleware/graphql.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,31 @@ module.exports = (_, app) => {
4545
return async (ctx, next) => {
4646
/* istanbul ignore else */
4747
if (ctx.path === graphQLRouter) {
48+
const {
49+
onPreGraphiQL,
50+
onPreGraphQL,
51+
apolloServerOptions,
52+
} = options;
4853
if (ctx.request.accepts([ 'json', 'html' ]) === 'html' && graphiql) {
49-
if (options.onPreGraphiQL) {
50-
await options.onPreGraphiQL(ctx);
54+
if (onPreGraphiQL) {
55+
await onPreGraphiQL(ctx);
5156
}
5257
return graphiqlKoa({
5358
endpointURL: graphQLRouter,
5459
})(ctx);
5560
}
56-
if (options.onPreGraphQL) {
57-
await options.onPreGraphQL(ctx);
61+
if (onPreGraphQL) {
62+
await onPreGraphQL(ctx);
5863
}
59-
return graphqlKoa({
60-
schema: app.schema,
61-
context: ctx,
62-
})(ctx);
64+
const serverOptions = Object.assign(
65+
{},
66+
apolloServerOptions,
67+
{
68+
schema: app.schema,
69+
context: ctx,
70+
}
71+
);
72+
return graphqlKoa(serverOptions)(ctx);
6373
}
6474
await next();
6575
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const mm = require('egg-mock');
5+
6+
describe('test/app/graphql-options.test.js', () => {
7+
let app;
8+
9+
before(() => {
10+
app = mm.app({
11+
baseDir: 'apps/graphql-options-app',
12+
});
13+
return app.ready();
14+
});
15+
16+
after(mm.restore);
17+
18+
it('should return custom error, use formatError', async () => {
19+
const resp = await app.httpRequest()
20+
.get('/graphql?query=query+getUser($id:Int){user(id:$id){name}}&variables={"id":1}')
21+
.expect(200);
22+
assert.equal(resp.body.errors[0].code, 100001);
23+
});
24+
25+
it('should return frameworks, user formatResponse', async () => {
26+
const resp = await app.httpRequest()
27+
.get('/graphql?query=query+getFramework($id:Int){framework(id:$id){name}}&variables={"id":1}')
28+
.expect(200);
29+
assert.deepEqual(resp.body.data, {
30+
frameworks: {
31+
name: 'framework1',
32+
},
33+
});
34+
});
35+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
changedName: 'frameworks',
5+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = {
4+
upper(next) {
5+
return next().then(str => {
6+
if (typeof str === 'string') {
7+
return str.toUpperCase();
8+
}
9+
return str;
10+
});
11+
},
12+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
directive @upper on FIELD_DEFINITION
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const DataLoader = require('dataloader');
4+
5+
class FrameworkConnector {
6+
constructor(ctx) {
7+
this.ctx = ctx;
8+
this.loader = new DataLoader(this.fetch.bind(this));
9+
}
10+
11+
fetch(ids) {
12+
return Promise.resolve(ids.map(id => ({
13+
id,
14+
name: `framework${id}`,
15+
projects: [],
16+
})));
17+
}
18+
19+
fetchByIds(ids) {
20+
return this.loader.loadMany(ids);
21+
}
22+
23+
fetchById(id) {
24+
return this.loader.load(id);
25+
}
26+
27+
}
28+
29+
module.exports = FrameworkConnector;
30+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = {
4+
Query: {
5+
framework(root, { id }, ctx) {
6+
return ctx.connector.framework.fetchById(id);
7+
},
8+
},
9+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
type Framework {
3+
id: Int!
4+
name: String!
5+
projects: [Project]
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return {
5+
Query: {
6+
projects() {
7+
console.log(app);
8+
return [];
9+
},
10+
},
11+
};
12+
};

0 commit comments

Comments
 (0)