File tree Expand file tree Collapse file tree 2 files changed +15
-7
lines changed Expand file tree Collapse file tree 2 files changed +15
-7
lines changed Original file line number Diff line number Diff line change @@ -14,10 +14,14 @@ import schema from './data/schema';
14
14
Relay .injectNetworkLayer (new RelayLocalSchema.NetworkLayer ({schema}));
15
15
```
16
16
17
- You can also supply a ` rootValue ` to the constructor:
17
+ You can also supply a GraphQL.js ` rootValue ` or an ` onError ` callback to the constructor:
18
18
19
19
``` js
20
20
Relay .injectNetworkLayer (
21
- new RelayLocalSchema.NetworkLayer ({schema, rootValue})
21
+ new RelayLocalSchema.NetworkLayer ({
22
+ schema,
23
+ rootValue: " foo" ,
24
+ onError : errors => console .log (errors)
25
+ })
22
26
);
23
27
```
Original file line number Diff line number Diff line change @@ -3,9 +3,10 @@ import {graphql} from 'graphql';
3
3
import formatRequestErrors from './__forks__/formatRequestErrors' ;
4
4
5
5
export default class NetworkLayer {
6
- constructor ( { schema, rootValue} ) {
6
+ constructor ( { schema, rootValue, onError } ) {
7
7
this . _schema = schema ;
8
8
this . _rootValue = rootValue ;
9
+ this . _onError = onError ;
9
10
}
10
11
11
12
sendMutation ( mutationRequest ) {
@@ -23,23 +24,26 @@ export default class NetworkLayer {
23
24
}
24
25
25
26
async _executeRequest ( requestType , request ) {
26
- const result = await graphql (
27
+ const { data , errors } = await graphql (
27
28
this . _schema ,
28
29
request . getQueryString ( ) ,
29
30
this . _rootValue ,
30
31
request . getVariables ( )
31
32
) ;
32
33
33
- if ( result . errors ) {
34
+ if ( errors ) {
34
35
request . reject ( new Error (
35
36
`Failed to execute ${ requestType } \`${ request . getDebugName ( ) } \` for ` +
36
37
'the following reasons:\n\n' +
37
- formatRequestErrors ( request , result . errors )
38
+ formatRequestErrors ( request , errors )
38
39
) ) ;
40
+ if ( this . _onError ) {
41
+ this . _onError ( errors ) ;
42
+ }
39
43
return ;
40
44
}
41
45
42
- request . resolve ( { response : result . data } ) ;
46
+ request . resolve ( { response : data } ) ;
43
47
}
44
48
45
49
supports ( ) {
You can’t perform that action at this time.
0 commit comments