-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (25 loc) · 946 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const { ApolloLink } = require('apollo-link');
const get = require('lodash/get');
const merge = require('lodash/merge');
const { removeDirectivesFromDocument } = require('apollo-utilities');
const { mapObject, genConfigFromDoc, setComputedProperty } = require('./utils');
const computedLint = new ApolloLink((operation, forwart) => {
const operationDefinition = get(operation, 'query.definitions').find(
definition => definition.kind === 'OperationDefinition'
);
const config = genConfigFromDoc(
get(operationDefinition, ['selectionSet', 'selections'])
);
operation.query = removeDirectivesFromDocument(
[{ name: 'computed', remove: true }],
operation.query
);
return forwart(operation).map(response => {
mapObject(config, (val, key, obj) => {
setComputedProperty(val, key, obj, response);
});
merge(response, { data: config });
return response;
});
});
module.exports = computedLint;