Skip to content

Commit

Permalink
refactor (graphql.js): Upgrade to the v2 of apollo (#18)
Browse files Browse the repository at this point in the history
We used to get the 'graphqlKoa' and 'graphiqlKoa' before as the
middle-wares,
but now they have been either removed or hidden from the public. And
since v2
of apollo we've faced with a BREAKING CHANGE
(See:apollographql/apollo-server@dbaa465#diff-64af8fdf76996fa3ed4e498d44124800
).
In order to be compatible with the older codes users are referring,
we have to do some tricks:

1) Directly refer 'graphqlKoa' from 'apollo-server-koa/dist/koaApollo'
instead
of 'apollo-server-koa', because it's NOT exposed through 'index.js'.

2) 'apollo-server-module-graphiql' is removed, so in order to compatible
with
what it was, we have to add this in our package and reuse that. And then
we should
rewrite 'graphiqlKoa' function as the middle-ware copied from:
https://github.com/apollographql/apollo-server/blob/300c0cd12b56be439b206d55131e1b93a9e6dade/packages/apollo-server-koa/src/koaApollo.ts#L51

Notice: This change is also a BREAKING one, so please DO NOT apply to
apollo's
version that is less than 2.x. And there're many ambigious problems
about this
design of v2 for apollo (See:
apollographql/apollo-server#1308).
So according to chentsulin's ideas, maybe the middle-ware functions
would be re-added
in the future (See:
apollographql/apollo-server#1282 (comment)).
  • Loading branch information
Maledong authored and jtyjty99999 committed Aug 26, 2018
1 parent 0248cb2 commit 461edf0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
33 changes: 32 additions & 1 deletion app/middleware/graphql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
'use strict';

const { graphqlKoa, graphiqlKoa } = require('apollo-server-koa');
// Notice that this path is totally changed, because this function isn't
// directly exposed to the public, now we must still use that for the middle-
// ware.
const { graphqlKoa } = require('apollo-server-koa/dist/koaApollo');

// This has been newly imported, because in v2 of apollo-server, this is removed.
const { resolveGraphiQLString } = require('apollo-server-module-graphiql');

/**
This function is directly copied from:
https://github.com/apollographql/apollo-server/blob/300c0cd12b56be439b206d55131e1b93a9e6dade/packages/apollo-server-koa/src/koaApollo.ts#L51
And now this has been removed since v2 at:
https://github.com/apollographql/apollo-server/commit/dbaa465646b0acb839860a85bfd68fb4379d64ab#diff-64af8fdf76996fa3ed4e498d44124800
So we must keep this here to be compatible with what it was before.
Thus users can directly use that by upgrading graphql to the v2 WITHOUT
doing any other big changes.
* @param {Object} options The `options` of graphiqlKoa.
* @return {Promise} The result of the graphiqlKoa.
*/
function graphiqlKoa(options) {
return ctx => {
const query = ctx.request.query;
return resolveGraphiQLString(query, options, ctx)
.then(graphiqlString => {
ctx.set('Content-Type', 'text/html');
ctx.body = graphiqlString;
});
};
}

module.exports = (_, app) => {
const options = app.config.graphql;
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"orm"
],
"dependencies": {
"apollo-server-koa": "^1.2.0",
"graphql": "^0.11.7",
"graphql-tag": "^2.5.0",
"graphql-tools": "^2.19.0",
"lodash": "4.17.4"
"apollo-server-koa": "^2.0.4",
"apollo-server-module-graphiql": "^1.4.0",
"graphql": "^0.13.2",
"graphql-tag": "^2.9.2",
"graphql-tools": "^3.1.1",
"lodash": "^4.17.10"
},
"devDependencies": {
"autod": "^2.9.0",
Expand Down
5 changes: 3 additions & 2 deletions test/app/graphiql/closeGraphiql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ describe('test/closeGraphiql.test.js', () => {

after(mm.restore);

it('should get graphql json response', function* () {
it('should get graphql json response', async () => {
app.mockHttpclient('/graphql', 'GET', {
headers: {
'accept-language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
Accept: 'text/html',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0',
},
});
return app.httpRequest()

await app.httpRequest()
.get('/graphql')
.expect('GET query missing.');
});
Expand Down
11 changes: 5 additions & 6 deletions test/app/graphiql/graphiql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ describe('test/graphiql.test.js', () => {

after(mm.restore);

it('should get graphiql html response', function* () {
it('should get graphiql html response', async () => {
app.mockHttpclient('/graphql', 'GET', {});
return app.httpRequest()
const result = await app.httpRequest()
.get('/graphql')
.set('Accept', 'text/html')
.expect(200)
.then(response => {
assert(response.type, 'text/html');
});
.expect(200);

assert(result.type, 'text/html');
});
});

0 comments on commit 461edf0

Please sign in to comment.