Skip to content

Commit d4ea6d3

Browse files
authored
Merge pull request #40 from relay-tools/createFetch
Expose createFetch
2 parents 7150733 + 9850f75 commit d4ea6d3

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Use [Relay](http://facebook.github.io/relay/) without a GraphQL server.
88
### Relay Modern
99

1010
```js
11+
import { Environment } from 'react-relay';
1112
import { Network } from 'relay-local-schema';
1213

1314
import schema from './data/schema';
@@ -33,6 +34,20 @@ const environment = new Environment({
3334
});
3435
```
3536

37+
For more control over the network layer, you can use `createFetch` to create just the fetch function.
38+
39+
```js
40+
import { Environment, Network } from 'react-relay';
41+
import { createFetch } from 'relay-local-schema';
42+
43+
import schema from './data/schema';
44+
45+
const environment = new Environment({
46+
network: Network.create(createFetch({ schema })),
47+
/* ... */
48+
});
49+
```
50+
3651
### Relay Classic
3752

3853
```js

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { Network } from './modern';
1+
export { createFetch, Network } from './modern';

src/modern/Network.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
import { graphql } from 'graphql';
2-
import { Network as NetworkBase } from 'relay-runtime';
1+
import { Network } from 'relay-runtime';
32

4-
export default {
5-
create({ schema, rootValue, contextValue }) {
6-
function fetchQuery(operation, variables) {
7-
return graphql(
8-
schema,
9-
operation.text,
10-
rootValue,
11-
contextValue,
12-
variables,
13-
).then((payload) => {
14-
if (payload.errors) {
15-
throw new Error(payload.errors);
16-
}
17-
18-
return payload;
19-
});
20-
}
3+
import createFetch from './createFetch';
214

22-
return NetworkBase.create(fetchQuery);
5+
export default {
6+
create(options) {
7+
return Network.create(createFetch(options));
238
},
249
};

src/modern/createFetch.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { graphql } from 'graphql';
2+
3+
export default function createFetch({ schema, rootValue, contextValue }) {
4+
return function fetchQuery(operation, variables) {
5+
return graphql(
6+
schema,
7+
operation.text,
8+
rootValue,
9+
contextValue,
10+
variables,
11+
).then((payload) => {
12+
if (payload.errors) {
13+
throw new Error(payload.errors);
14+
}
15+
16+
return payload;
17+
});
18+
};
19+
}

src/modern/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export createFetch from './createFetch';
12
export Network from './Network';

0 commit comments

Comments
 (0)