Skip to content

Commit a2f2536

Browse files
committed
Merge pull request #2 from relay-tools/add-onError
Support onError callback
2 parents 6d716b6 + 2e930ea commit a2f2536

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import schema from './data/schema';
1414
Relay.injectNetworkLayer(new RelayLocalSchema.NetworkLayer({schema}));
1515
```
1616

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:
1818

1919
```js
2020
Relay.injectNetworkLayer(
21-
new RelayLocalSchema.NetworkLayer({schema, rootValue})
21+
new RelayLocalSchema.NetworkLayer({
22+
schema,
23+
rootValue: "foo",
24+
onError: errors => console.log(errors)
25+
})
2226
);
2327
```

src/NetworkLayer.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {graphql} from 'graphql';
33
import formatRequestErrors from './__forks__/formatRequestErrors';
44

55
export default class NetworkLayer {
6-
constructor({schema, rootValue}) {
6+
constructor({schema, rootValue, onError}) {
77
this._schema = schema;
88
this._rootValue = rootValue;
9+
this._onError = onError;
910
}
1011

1112
sendMutation(mutationRequest) {
@@ -23,23 +24,26 @@ export default class NetworkLayer {
2324
}
2425

2526
async _executeRequest(requestType, request) {
26-
const result = await graphql(
27+
const {data, errors} = await graphql(
2728
this._schema,
2829
request.getQueryString(),
2930
this._rootValue,
3031
request.getVariables()
3132
);
3233

33-
if (result.errors) {
34+
if (errors) {
3435
request.reject(new Error(
3536
`Failed to execute ${requestType} \`${request.getDebugName()}\` for ` +
3637
'the following reasons:\n\n' +
37-
formatRequestErrors(request, result.errors)
38+
formatRequestErrors(request, errors)
3839
));
40+
if (this._onError) {
41+
this._onError(errors);
42+
}
3943
return;
4044
}
4145

42-
request.resolve({response: result.data});
46+
request.resolve({response: data});
4347
}
4448

4549
supports() {

0 commit comments

Comments
 (0)