Skip to content

Commit 00650d7

Browse files
committed
added eslint, nyc, reuse memoize commonly between resolver.memoize and directive
1 parent cea5e11 commit 00650d7

File tree

6 files changed

+37
-77
lines changed

6 files changed

+37
-77
lines changed

.eslintrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": ["eslint:recommended"],
3+
"parserOptions": {
4+
"ecmaVersion": 2018,
5+
"sourceType": "module"
6+
},
7+
"env": {
8+
"browser": false,
9+
"node": true,
10+
"es6": true
11+
}
12+
}

lib/directives.js

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
11
'use strict';
22

33
const { SchemaDirectiveVisitor } = require('graphql-tools');
4-
const debug = require('debug')('graphql-component:resolver');
4+
const Resolver = require('./resolvers');
5+
const debug = require('debug')('graphql-component:directive');
56

67
const _cache = new WeakMap();
78

89
class MemoizeDirective extends SchemaDirectiveVisitor {
910
visitFieldDefinition(field) {
1011
const { resolve } = field;
11-
12+
console.log(field);
1213
if (!resolve) {
1314
return;
1415
}
1516

16-
const memoize = function (resolve) {
17-
return function (_, args, context, info) {
18-
const key = JSON.stringify(args);
19-
const { parentType } = info;
20-
21-
let cached = _cache.get(context);
22-
23-
if (cached && cached[parentType] && cached[parentType][field.name] && cached[parentType][field.name][key]) {
24-
debug(`return cached result of memoized ${parentType}.${field.name}`);
25-
return cached[parentType][field.name][key];
26-
}
27-
28-
debug(`executing and caching memoized ${parentType}.${field.name}`);
29-
30-
if (!cached) {
31-
cached = {};
32-
}
33-
if (!cached[parentType]) {
34-
cached[parentType] = {};
35-
}
36-
if (!cached[parentType][field.name]) {
37-
cached[parentType][field.name] = {};
38-
}
39-
40-
const result = resolve(_, args, context, info);
41-
42-
cached[parentType][field.name][key] = result;
43-
44-
_cache.set(context, cached);
45-
46-
return result;
47-
};
48-
};
49-
50-
field.resolve = memoize(resolve);
17+
field.resolve = Resolver.memoize(field.name, resolve);
5118

5219
debug(`memoized ${field.name}`);
5320
}

lib/resolvers.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ const wrapResolvers = function (bind, resolvers = {}) {
1515
if (wrapped[name][resolverName]) {
1616
continue;
1717
}
18-
19-
wrapped[name][resolverName] = ['Query', 'Mutation', 'Subscription'].indexOf(name) > -1 ? memoize(name, resolverName, func.bind(bind)) : func.bind(bind);
18+
if (['Query', 'Mutation', 'Subscription'].indexOf(name) > -1) {
19+
debug(`memoized ${name}.${resolverName}`);
20+
wrapped[name][resolverName] = memoize(resolverName, func.bind(bind));
21+
continue;
22+
}
23+
wrapped[name][resolverName] = func.bind(bind);
2024
}
2125
}
2226

@@ -43,11 +47,10 @@ const getImportedResolvers = function (imp) {
4347

4448
const _cache = new WeakMap();
4549

46-
const memoize = function (parentType, fieldName, resolve) {
47-
debug(`memoized ${parentType}.${fieldName}`);
48-
50+
const memoize = function (fieldName, resolve) {
4951
return function (_, args, context, info) {
5052
const key = JSON.stringify(args);
53+
const { parentType } = info;
5154

5255
debug(`executing ${parentType}.${fieldName}`);
5356

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"test": "tape test/test-*.js",
8-
"start": "DEBUG=graphql-component:* GRAPHQL_DEBUG=1 node --trace-event-categories graphql examples/example-listing/server/index.js"
8+
"start": "DEBUG=graphql-component:* GRAPHQL_DEBUG=1 node --trace-event-categories graphql examples/example-listing/server/index.js",
9+
"lint": "eslint lib",
10+
"cover": "nyc npm test"
911
},
1012
"author": "Trevor Livingston <[email protected]>",
1113
"repository": "https://github.com/tlivings/graphql-component",
@@ -20,6 +22,8 @@
2022
"devDependencies": {
2123
"apollo-server": "^2.0.7",
2224
"casual": "^1.6.0",
25+
"eslint": "^5.14.1",
26+
"nyc": "^13.3.0",
2327
"tape": "^4.9.1"
2428
}
2529
}

test/test-directive.js

-28
This file was deleted.

test/test-resolvers.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Test('wrapping', (t) => {
1818

1919
const wrapped = Resolvers.wrapResolvers({ id: 1 }, resolvers);
2020

21-
const value = wrapped.Query.test({}, {}, {});
21+
const value = wrapped.Query.test({}, {}, {}, { parentType: 'Query' });
2222

2323
t.equal(value, 1, 'resolver was bound');
2424
});
@@ -41,12 +41,13 @@ Test('wrapping', (t) => {
4141
const wrapped = Resolvers.wrapResolvers(undefined, resolvers);
4242

4343
const ctx = {};
44+
const info = { parentType: 'Query' };
4445

45-
let value = wrapped.Query.test({}, {}, ctx);
46+
let value = wrapped.Query.test({}, {}, ctx, info);
4647

4748
t.equal(value, 1, 'expected value');
4849

49-
value = wrapped.Query.test({}, {}, ctx);
50+
value = wrapped.Query.test({}, {}, ctx, info);
5051

5152
t.equal(value, 1, 'same value, only ran resolver once');
5253
});
@@ -64,15 +65,16 @@ Test('memoize resolver', (t) => {
6465
return ran;
6566
};
6667

67-
const wrapped = Resolvers.memoize('Query', 'test', resolver);
68+
const wrapped = Resolvers.memoize('test', resolver);
6869

6970
const ctx = {};
71+
const info = { parentType: 'Query' };
7072

71-
let value = wrapped({}, {}, ctx);
73+
let value = wrapped({}, {}, ctx, info);
7274

7375
t.equal(value, 1, 'expected value');
7476

75-
value = wrapped({}, {}, ctx);
77+
value = wrapped({}, {}, ctx, info);
7678

7779
t.equal(value, 1, 'same value, only ran resolver once');
7880
});

0 commit comments

Comments
 (0)