Skip to content

Commit 12ca5c0

Browse files
committed
adding prettier
1 parent 01cc48d commit 12ca5c0

13 files changed

+730
-574
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"eslint-config-google": "^0.6.0",
5353
"graphql": "^0.12.0",
5454
"mocha": "^3.1.2",
55-
"nyc": "^8.3.1"
55+
"nyc": "^8.3.1",
56+
"prettier": "^1.13.5"
5657
}
5758
}

src/custom.js

+70-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GraphQLDirective } from 'graphql/type/directives';
2-
import { GraphQLSchema, parse } from 'graphql';
1+
import {GraphQLDirective} from 'graphql/type/directives';
2+
import {GraphQLSchema, parse} from 'graphql';
33

44
const DEFAULT_DIRECTIVES = ['skip', 'include'];
55

@@ -13,16 +13,20 @@ function defaultResolveFn(source, args, context, info) {
1313
var fieldName = info.fieldName;
1414
// ensure source is a value for which property access is acceptable.
1515
if (typeof source === 'object' || typeof source === 'function') {
16-
return typeof source[fieldName] === 'function' ? source[fieldName]() : source[fieldName];
16+
return typeof source[fieldName] === 'function'
17+
? source[fieldName]()
18+
: source[fieldName];
1719
}
1820
}
1921

2022
/**
2123
* resolving field using directive resolver
2224
*/
2325
function resolveWithDirective(resolve, source, directive, context, info) {
24-
source = source || (((info || {}).variableValues || {}).input_0) || {};
25-
let directiveConfig = info.schema._directives.filter(d => directive.name.value === d.name)[0];
26+
source = source || ((info || {}).variableValues || {}).input_0 || {};
27+
let directiveConfig = info.schema._directives.filter(
28+
d => directive.name.value === d.name,
29+
)[0];
2630

2731
let args = {};
2832

@@ -31,23 +35,28 @@ function resolveWithDirective(resolve, source, directive, context, info) {
3135
}
3236

3337
return directiveConfig.resolve(resolve, source, args, context, info);
34-
};
38+
}
3539

3640
/**
3741
* parse directives from a schema defenition form them as graphql directive structure
3842
*/
3943
function parseSchemaDirectives(directives) {
4044
let schemaDirectives = [];
4145

42-
if (!directives || !(directives instanceof Object) || Object.keys(directives).length === 0) {
46+
if (
47+
!directives ||
48+
!(directives instanceof Object) ||
49+
Object.keys(directives).length === 0
50+
) {
4351
return [];
4452
}
4553

4654
for (let directiveName in directives) {
47-
let argsList = [], args = '';
55+
let argsList = [],
56+
args = '';
4857

4958
Object.keys(directives[directiveName]).map(key => {
50-
argsList.push(`${key}:"${directives[directiveName][key]}"`)
59+
argsList.push(`${key}:"${directives[directiveName][key]}"`);
5160
});
5261

5362
if (argsList.length > 0) {
@@ -57,8 +66,9 @@ function parseSchemaDirectives(directives) {
5766
schemaDirectives.push(`@${directiveName}${args}`);
5867
}
5968

60-
return parse(`{ a: String ${schemaDirectives.join(' ')} }`).definitions[0].selectionSet.selections[0].directives;
61-
};
69+
return parse(`{ a: String ${schemaDirectives.join(' ')} }`).definitions[0]
70+
.selectionSet.selections[0].directives;
71+
}
6272

6373
/**
6474
* If the directive is defined on a field it will execute the custom directive
@@ -69,35 +79,69 @@ function resolveMiddlewareWrapper(resolve = defaultResolveFn, directives = {}) {
6979
const serverDirectives = parseSchemaDirectives(directives);
7080

7181
return (source, args, context, info) => {
72-
const directives = serverDirectives.concat((info.fieldASTs || info.fieldNodes)[0].directives);
73-
const directive = directives.filter(d => DEFAULT_DIRECTIVES.indexOf(d.name.value) === -1)[0];
82+
const directives = serverDirectives.concat(
83+
(info.fieldASTs || info.fieldNodes)[0].directives,
84+
);
85+
const directive = directives.filter(
86+
d => DEFAULT_DIRECTIVES.indexOf(d.name.value) === -1,
87+
)[0];
7488

7589
if (!directive) {
7690
return resolve(source, args, context, info);
7791
}
7892

79-
let defer = resolveWithDirective(() => Promise.resolve(resolve(source, args, context, info)), source, directive, context, info);
80-
defer.catch(e => resolveWithDirective(() => Promise.reject(e), source, directive, context, info))
93+
let defer = resolveWithDirective(
94+
() => Promise.resolve(resolve(source, args, context, info)),
95+
source,
96+
directive,
97+
context,
98+
info,
99+
);
100+
defer.catch(e =>
101+
resolveWithDirective(
102+
() => Promise.reject(e),
103+
source,
104+
directive,
105+
context,
106+
info,
107+
),
108+
);
81109

82110
if (directives.length <= 1) {
83111
return defer;
84112
}
85113

86114
for (let directiveNext of directives.slice(1)) {
87-
defer = defer.then(result => resolveWithDirective(() => Promise.resolve(result), source, directiveNext, context, info));
88-
defer.catch(e => resolveWithDirective(() => Promise.reject(e), source, directiveNext, context, info));
115+
defer = defer.then(result =>
116+
resolveWithDirective(
117+
() => Promise.resolve(result),
118+
source,
119+
directiveNext,
120+
context,
121+
info,
122+
),
123+
);
124+
defer.catch(e =>
125+
resolveWithDirective(
126+
() => Promise.reject(e),
127+
source,
128+
directiveNext,
129+
context,
130+
info,
131+
),
132+
);
89133
}
90134

91135
return defer;
92136
};
93-
};
137+
}
94138

95139
/**
96140
* Scanning the shema and wrapping the resolve of each field with the support
97141
* of the graphql custom directives resolve execution
98142
*/
99143
function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}) {
100-
if(!type){
144+
if (!type) {
101145
return;
102146
}
103147

@@ -107,9 +151,12 @@ function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}) {
107151
let field = fields[label];
108152
if (field && !typeMet[field.type.name]) {
109153
if (!!field && typeof field == 'object') {
110-
field.resolve = resolveMiddlewareWrapper(field.resolve, field.directives);
154+
field.resolve = resolveMiddlewareWrapper(
155+
field.resolve,
156+
field.directives,
157+
);
111158
if (field.type._fields && deepWrap) {
112-
wrapFieldsWithMiddleware(field.type, deepWrap, typeMet)
159+
wrapFieldsWithMiddleware(field.type, deepWrap, typeMet);
113160
} else if (field.type.ofType && field.type.ofType._fields && deepWrap) {
114161
wrapFieldsWithMiddleware(field.type.ofType, deepWrap, typeMet);
115162
}
@@ -122,7 +169,7 @@ function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}) {
122169
* create a new graphql custom directive which contain a resolve
123170
* function for altering the execution of the graphql
124171
*/
125-
exports.GraphQLCustomDirective = function (config) {
172+
exports.GraphQLCustomDirective = function(config) {
126173
const directive = new GraphQLDirective(config);
127174

128175
if (config.resolve) {
@@ -135,8 +182,7 @@ exports.GraphQLCustomDirective = function (config) {
135182
/**
136183
* Apply custom directives support in the graphql schema
137184
*/
138-
exports.applySchemaCustomDirectives = function (schema) {
139-
185+
exports.applySchemaCustomDirectives = function(schema) {
140186
if (!(schema instanceof GraphQLSchema)) {
141187
throw new Error('Schema must be instanceof GraphQLSchema');
142188
}

src/directives/currency.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
import { GraphQLString } from 'graphql';
2-
import { DirectiveLocation } from 'graphql/language/directiveLocation';
3-
import { GraphQLCustomDirective } from '../custom';
1+
import {GraphQLString} from 'graphql';
2+
import {DirectiveLocation} from 'graphql/language/directiveLocation';
3+
import {GraphQLCustomDirective} from '../custom';
44

55
import numeral from 'numeral';
66

77
const DEFAULT_CURRENCY_FORMAT = '$0,0';
88

99
exports.GraphQLCurrencyDirective = new GraphQLCustomDirective({
1010
name: 'currency',
11-
description:
12-
'Format the currency value from resolving the field',
13-
locations: [
14-
DirectiveLocation.FIELD
15-
],
11+
description: 'Format the currency value from resolving the field',
12+
locations: [DirectiveLocation.FIELD],
1613
args: {
1714
as: {
1815
type: GraphQLString,
19-
description: 'A currency format given by numeral module'
20-
}
16+
description: 'A currency format given by numeral module',
17+
},
2118
},
22-
resolve(resolve, source, { as }) {
19+
resolve(resolve, source, {as}) {
2320
return resolve().then(input => {
2421
const format = as || DEFAULT_CURRENCY_FORMAT;
2522

@@ -29,6 +26,5 @@ exports.GraphQLCurrencyDirective = new GraphQLCustomDirective({
2926

3027
return input;
3128
});
32-
}
29+
},
3330
});
34-

src/directives/date.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { GraphQLString, GraphQLBoolean } from 'graphql';
2-
import { DirectiveLocation } from 'graphql/language/directiveLocation';
3-
import { GraphQLCustomDirective } from '../custom';
4-
import { _ } from 'lodash';
1+
import {GraphQLString, GraphQLBoolean} from 'graphql';
2+
import {DirectiveLocation} from 'graphql/language/directiveLocation';
3+
import {GraphQLCustomDirective} from '../custom';
4+
import {_} from 'lodash';
55

66
const moment = require('moment');
77

@@ -10,18 +10,15 @@ const DEFAULT_DATE_FORMAT = 'DD MMM YYYY HH:mm';
1010
exports.GraphQLDateDirective = new GraphQLCustomDirective({
1111
name: 'date',
1212
description: 'Format the date from resolving the field by moment module',
13-
locations: [
14-
DirectiveLocation.FIELD
15-
],
13+
locations: [DirectiveLocation.FIELD],
1614
args: {
1715
as: {
1816
type: GraphQLString,
19-
description: 'A format given by moment module'
20-
}
17+
description: 'A format given by moment module',
18+
},
2119
},
22-
resolve(resolve, source, { as }) {
20+
resolve(resolve, source, {as}) {
2321
return resolve().then(input => {
24-
2522
const format = as || DEFAULT_DATE_FORMAT;
2623

2724
if (format.indexOf('days') !== -1 || format.indexOf('ago') !== -1) {
@@ -38,30 +35,29 @@ exports.GraphQLDateDirective = new GraphQLCustomDirective({
3835

3936
return moment.utc(input).format(format);
4037
});
41-
}
38+
},
4239
});
4340

44-
4541
exports.GraphQLTimeOffsetDirective = new GraphQLCustomDirective({
4642
name: 'timeOffset',
4743
description: 'Add offset (in minutes) to a 13 digit unixtime',
4844
locations: [DirectiveLocation.FIELD],
4945
args: {
5046
offsetLocation: {
5147
type: GraphQLString,
52-
description: 'Path of offset in minutes within context object. e.g - "req.profile.utcOffset"'
53-
}
48+
description:
49+
'Path of offset in minutes within context object. e.g - "req.profile.utcOffset"',
50+
},
5451
},
5552
resolve: function resolve(_resolve, source, _ref, context, info) {
5653
var offsetMinutes = _.get(context, _ref.offsetLocation);
5754
var offsetMilliseconds = offsetMinutes * 60 * 1000;
5855

59-
return _resolve().then(function (input) {
60-
56+
return _resolve().then(function(input) {
6157
if (('' + input).length === 13) {
6258
input = Number(input) + offsetMilliseconds;
6359
return input;
6460
}
6561
});
66-
}
62+
},
6763
});

src/directives/number.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { GraphQLString } from 'graphql';
2-
import { DirectiveLocation } from 'graphql/language/directiveLocation';
3-
import { GraphQLCustomDirective } from '../custom';
1+
import {GraphQLString} from 'graphql';
2+
import {DirectiveLocation} from 'graphql/language/directiveLocation';
3+
import {GraphQLCustomDirective} from '../custom';
44

55
import numeral from 'numeral';
66

@@ -9,18 +9,15 @@ const DEFAULT_NUMBER_FORMAT = '0,0';
99
exports.GraphQLNumberDirective = new GraphQLCustomDirective({
1010
name: 'number',
1111
description: 'Format the number value from resolving the field',
12-
locations: [
13-
DirectiveLocation.FIELD
14-
],
12+
locations: [DirectiveLocation.FIELD],
1513
args: {
1614
as: {
1715
type: GraphQLString,
18-
description: 'A format given by numeral module'
19-
}
16+
description: 'A format given by numeral module',
17+
},
2018
},
21-
resolve(resolve, source, { as }) {
22-
return resolve().then((input) => {
23-
19+
resolve(resolve, source, {as}) {
20+
return resolve().then(input => {
2421
const format = as || DEFAULT_NUMBER_FORMAT;
2522

2623
if (format.indexOf('0') !== -1 && !Number.isNaN(Number(input))) {
@@ -29,6 +26,5 @@ exports.GraphQLNumberDirective = new GraphQLCustomDirective({
2926

3027
return input;
3128
});
32-
}
29+
},
3330
});
34-

0 commit comments

Comments
 (0)