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' ;
3
3
4
4
const DEFAULT_DIRECTIVES = [ 'skip' , 'include' ] ;
5
5
@@ -13,16 +13,20 @@ function defaultResolveFn(source, args, context, info) {
13
13
var fieldName = info . fieldName ;
14
14
// ensure source is a value for which property access is acceptable.
15
15
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 ] ;
17
19
}
18
20
}
19
21
20
22
/**
21
23
* resolving field using directive resolver
22
24
*/
23
25
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 ] ;
26
30
27
31
let args = { } ;
28
32
@@ -31,23 +35,28 @@ function resolveWithDirective(resolve, source, directive, context, info) {
31
35
}
32
36
33
37
return directiveConfig . resolve ( resolve , source , args , context , info ) ;
34
- } ;
38
+ }
35
39
36
40
/**
37
41
* parse directives from a schema defenition form them as graphql directive structure
38
42
*/
39
43
function parseSchemaDirectives ( directives ) {
40
44
let schemaDirectives = [ ] ;
41
45
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
+ ) {
43
51
return [ ] ;
44
52
}
45
53
46
54
for ( let directiveName in directives ) {
47
- let argsList = [ ] , args = '' ;
55
+ let argsList = [ ] ,
56
+ args = '' ;
48
57
49
58
Object . keys ( directives [ directiveName ] ) . map ( key => {
50
- argsList . push ( `${ key } :"${ directives [ directiveName ] [ key ] } "` )
59
+ argsList . push ( `${ key } :"${ directives [ directiveName ] [ key ] } "` ) ;
51
60
} ) ;
52
61
53
62
if ( argsList . length > 0 ) {
@@ -57,8 +66,9 @@ function parseSchemaDirectives(directives) {
57
66
schemaDirectives . push ( `@${ directiveName } ${ args } ` ) ;
58
67
}
59
68
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
+ }
62
72
63
73
/**
64
74
* If the directive is defined on a field it will execute the custom directive
@@ -69,35 +79,69 @@ function resolveMiddlewareWrapper(resolve = defaultResolveFn, directives = {}) {
69
79
const serverDirectives = parseSchemaDirectives ( directives ) ;
70
80
71
81
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 ] ;
74
88
75
89
if ( ! directive ) {
76
90
return resolve ( source , args , context , info ) ;
77
91
}
78
92
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
+ ) ;
81
109
82
110
if ( directives . length <= 1 ) {
83
111
return defer ;
84
112
}
85
113
86
114
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
+ ) ;
89
133
}
90
134
91
135
return defer ;
92
136
} ;
93
- } ;
137
+ }
94
138
95
139
/**
96
140
* Scanning the shema and wrapping the resolve of each field with the support
97
141
* of the graphql custom directives resolve execution
98
142
*/
99
143
function wrapFieldsWithMiddleware ( type , deepWrap = true , typeMet = { } ) {
100
- if ( ! type ) {
144
+ if ( ! type ) {
101
145
return ;
102
146
}
103
147
@@ -107,9 +151,12 @@ function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}) {
107
151
let field = fields [ label ] ;
108
152
if ( field && ! typeMet [ field . type . name ] ) {
109
153
if ( ! ! field && typeof field == 'object' ) {
110
- field . resolve = resolveMiddlewareWrapper ( field . resolve , field . directives ) ;
154
+ field . resolve = resolveMiddlewareWrapper (
155
+ field . resolve ,
156
+ field . directives ,
157
+ ) ;
111
158
if ( field . type . _fields && deepWrap ) {
112
- wrapFieldsWithMiddleware ( field . type , deepWrap , typeMet )
159
+ wrapFieldsWithMiddleware ( field . type , deepWrap , typeMet ) ;
113
160
} else if ( field . type . ofType && field . type . ofType . _fields && deepWrap ) {
114
161
wrapFieldsWithMiddleware ( field . type . ofType , deepWrap , typeMet ) ;
115
162
}
@@ -122,7 +169,7 @@ function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}) {
122
169
* create a new graphql custom directive which contain a resolve
123
170
* function for altering the execution of the graphql
124
171
*/
125
- exports . GraphQLCustomDirective = function ( config ) {
172
+ exports . GraphQLCustomDirective = function ( config ) {
126
173
const directive = new GraphQLDirective ( config ) ;
127
174
128
175
if ( config . resolve ) {
@@ -135,8 +182,7 @@ exports.GraphQLCustomDirective = function (config) {
135
182
/**
136
183
* Apply custom directives support in the graphql schema
137
184
*/
138
- exports . applySchemaCustomDirectives = function ( schema ) {
139
-
185
+ exports . applySchemaCustomDirectives = function ( schema ) {
140
186
if ( ! ( schema instanceof GraphQLSchema ) ) {
141
187
throw new Error ( 'Schema must be instanceof GraphQLSchema' ) ;
142
188
}
0 commit comments