Skip to content

Commit e3111fe

Browse files
authored
fix: missing this context (#12)
* v1.0.3-beta * v1.0.3-beta1 * v1.0.3-beta2 * v1.0.3-beta3 * v1.0.3-beta4 * fix: update Babel to transpile back to Node 6 * fix: update to string-based docs for schema * fix: add model to an object Because `context` gets copied and combined with `extraContext`, we need to export an object rather than an instance of a class. Otherwise we get issues with `this` misbehaving. * chore: upgrade dependencies * test: fix Babel coverage with temporary plugin Until istanbuljs/istanbuljs#125 is merged, we need a temporary plugin to get coverage to report correctly. BREAKING CHANGE: Moves model class from `context` prop directly to a `context.model` prop, which prevents issues when merging context objects.
1 parent 693bfd0 commit e3111fe

9 files changed

+2055
-954
lines changed

.babelrc

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"presets": [
3-
"stage-2",
43
[
5-
"env",
4+
"@babel/preset-env",
65
{
7-
"targets": {
8-
"node": "current"
9-
}
6+
"targets": { "node": "6" },
7+
"shippedProposals": true,
8+
"useBuiltIns": "usage"
109
}
1110
]
1211
],
13-
"plugins": ["babel-plugin-inline-import"],
14-
"ignore": ["node_modules/**"]
12+
"plugins": [
13+
"@babel/plugin-proposal-class-properties",
14+
"babel-plugin-inline-import",
15+
"./babel/arrow-function-coverage-fix.js"
16+
]
1517
}

babel/arrow-function-coverage-fix.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Restore old babylon behavior for istanbul.
2+
// https://github.com/babel/babel/pull/6836
3+
// https://github.com/istanbuljs/istanbuljs/issues/119
4+
// Temporary fix for Jest issue: https://github.com/facebook/jest/issues/5001
5+
module.exports = function hacks() {
6+
return {
7+
visitor: {
8+
Program(programPath) {
9+
programPath.traverse({
10+
ArrowFunctionExpression(path) {
11+
const node = path.node;
12+
node.expression = node.body.type !== 'BlockStatement';
13+
},
14+
});
15+
},
16+
},
17+
};
18+
};

package.json

+20-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"prepare": "npm run build",
2121
"prebuild": "del-cli ./dist",
2222
"build": "babel src -d dist",
23+
"postbuild": "cpy ./src/schema.graphql ./dist",
2324
"dev": "gramps dev -d ./",
2425
"mock-data": "npm run dev -- --mock",
2526
"live-data": "npm run dev -- --live",
@@ -34,35 +35,38 @@
3435
"license": "MIT",
3536
"dependencies": {
3637
"@gramps/errors": "^1.0.0",
37-
"@gramps/rest-helpers": "^1.0.0-beta.3",
38+
"@gramps/rest-helpers": "^1.0.0",
3839
"casual": "^1.5.14"
3940
},
4041
"peerDependencies": {
4142
"graphql": "^0.9.0 || ^0.10.0 || ^0.11.0",
4243
"graphql-tools": "^1.2.1 || ^2.5.1"
4344
},
4445
"devDependencies": {
45-
"@gramps/cli": "^1.1.0",
46-
"@gramps/gramps": "^1.0.0",
47-
"babel-cli": "^6.24.1",
48-
"babel-eslint": "^8.0.1",
49-
"babel-jest": "^22.0.3",
46+
"@babel/cli": "^7.0.0-beta.37",
47+
"@babel/core": "^7.0.0-beta.37",
48+
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.37",
49+
"@babel/preset-env": "^7.0.0-beta.37",
50+
"@gramps/cli": "^1.1.4",
51+
"@gramps/gramps": "^1.2.0",
52+
"babel-core": "^7.0.0-0",
53+
"babel-eslint": "^8.2.1",
54+
"babel-jest": "^22.0.6",
5055
"babel-plugin-inline-import": "^2.0.6",
51-
"babel-preset-env": "^1.6.1",
52-
"babel-preset-stage-2": "^6.24.1",
56+
"cpy-cli": "^1.0.1",
5357
"del-cli": "^1.1.0",
54-
"eslint": "^4.4.1",
58+
"eslint": "^4.15.0",
5559
"eslint-config-airbnb-base": "^12.1.0",
5660
"eslint-config-prettier": "^2.3.0",
5761
"eslint-plugin-import": "^2.7.0",
5862
"eslint-plugin-prettier": "^2.1.2",
59-
"graphql": "^0.11.7",
60-
"graphql-tools": "^2.5.1",
63+
"graphql": "^0.12.3",
64+
"graphql-tools": "^2.18.0",
6165
"husky": "^0.14.3",
62-
"jest": "^22.0.3",
63-
"nodemon": "^1.11.0",
64-
"prettier": "^1.5.3",
65-
"semantic-release": "^8.2.0"
66+
"jest": "^22.0.6",
67+
"nodemon": "^1.14.11",
68+
"prettier": "^1.10.2",
69+
"semantic-release": "^11.0.2"
6670
},
6771
"jest": {
6872
"coverageReporters": [
@@ -81,5 +85,5 @@
8185
}
8286
}
8387
},
84-
"version": "0.0.0-development"
88+
"version": "1.0.3-beta4"
8589
}

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import mocks from './mocks';
1010
*/
1111
export default {
1212
namespace: 'XKCD',
13-
context: new Model({ connector: new Connector() }),
13+
context: { model: new Model({ connector: new Connector() }) },
1414
typeDefs,
1515
resolvers,
1616
mocks,

src/resolvers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default {
22
Query: {
3-
getLatestComic: (_, __, context) => context.getLatestComic(),
4-
getComicById: (_, { id }, context) => context.getComicById(id),
3+
getLatestComic: (_, __, context) => context.model.getLatestComic(),
4+
getComicById: (_, { id }, context) => context.model.getComicById(id),
55
},
66
XKCD_Comic: {
77
// The link is often empty, so build one if it’s not returned.

src/schema.graphql

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
1-
# The query/queries to access data MUST extend the Query type.
1+
"The query/queries to access data MUST extend the Query type."
22
type Query {
3-
# Load info about the most recent xkcd comic.
3+
"Load info about the most recent xkcd comic."
44
getLatestComic: XKCD_Comic
55

6-
# Load info about the xkcd comic with a given ID.
6+
"Load info about the xkcd comic with a given ID."
77
getComicById(
8-
# The ID of the given comic. The ID can be found using its URL.
9-
#
10-
# Example: https://xkcd.com/619/ has the ID of 619.
8+
"""
9+
The ID of the given comic. The ID can be found using its URL.
10+
11+
Example: https://xkcd.com/619/ has the ID of 619.
12+
"""
1113
id: ID!
1214
): XKCD_Comic
1315
}
1416

15-
# Details about an xkcd comic.
17+
"Details about an xkcd comic."
1618
type XKCD_Comic {
17-
# The unique ID of the comic.
19+
"The unique ID of the comic."
1820
num: ID!
1921

20-
# The title of the comic.
22+
"The title of the comic."
2123
title: String!
2224

23-
# As far as I can tell, this is always the same as the title field.
25+
"As far as I can tell, this is always the same as the title field."
2426
safe_title: String!
2527

26-
# Direct link to the comic image.
28+
"Direct link to the comic image."
2729
img: String!
2830

29-
# Alt text (this is what shows on hover in comics on the site).
31+
"Alt text (this is what shows on hover in comics on the site)."
3032
alt: String!
3133

32-
# A transcript of what’s happening in the comic.
34+
"A transcript of what’s happening in the comic."
3335
transcript: String
3436

35-
# The year the comic was released.
37+
"The year the comic was released."
3638
year: String
3739

38-
# The month the comic was released.
40+
"The month the comic was released."
3941
month: String
4042

41-
# The day the comic was released.
43+
"The day the comic was released."
4244
day: String
4345

44-
# A link to the comic on xkcd.com
46+
"A link to the comic on xkcd.com"
4547
link: String
4648

47-
# Additional info, shown on the xkcd website in the banner.
49+
"Additional info, shown on the xkcd website in the banner."
4850
news: String
4951
}

test/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Data Source: XKCD', () => {
77
});
88

99
it('returns its model as a context', () => {
10-
expect(dataSource.context).toBeInstanceOf(Model);
10+
expect(dataSource.context).toBeTruthy();
1111
});
1212

1313
it('returns typeDefs', () => {

test/resolvers.test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ describe('XKCD resolvers', () => {
77

88
describe('queryResolvers', () => {
99
const mockContext = {
10-
// Mock a response so we can actually test it.
11-
getLatestComic: () => Promise.resolve('latest'),
12-
// For testing, we mock the model to simply return the ID.
13-
getComicById: id => Promise.resolve(id),
10+
model: {
11+
// Mock a response so we can actually test it.
12+
getLatestComic: () => Promise.resolve('latest'),
13+
// For testing, we mock the model to simply return the ID.
14+
getComicById: id => Promise.resolve(id),
15+
},
1416
};
1517

1618
describe('getLatestComic()', () => {

0 commit comments

Comments
 (0)