Skip to content

Commit

Permalink
Merge pull request #40 from relay-tools/createFetch
Browse files Browse the repository at this point in the history
Expose createFetch
  • Loading branch information
taion authored Jun 3, 2017
2 parents 7150733 + 9850f75 commit d4ea6d3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Use [Relay](http://facebook.github.io/relay/) without a GraphQL server.
### Relay Modern

```js
import { Environment } from 'react-relay';
import { Network } from 'relay-local-schema';

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

For more control over the network layer, you can use `createFetch` to create just the fetch function.

```js
import { Environment, Network } from 'react-relay';
import { createFetch } from 'relay-local-schema';

import schema from './data/schema';

const environment = new Environment({
network: Network.create(createFetch({ schema })),
/* ... */
});
```

### Relay Classic

```js
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Network } from './modern';
export { createFetch, Network } from './modern';
25 changes: 5 additions & 20 deletions src/modern/Network.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { graphql } from 'graphql';
import { Network as NetworkBase } from 'relay-runtime';
import { Network } from 'relay-runtime';

export default {
create({ schema, rootValue, contextValue }) {
function fetchQuery(operation, variables) {
return graphql(
schema,
operation.text,
rootValue,
contextValue,
variables,
).then((payload) => {
if (payload.errors) {
throw new Error(payload.errors);
}

return payload;
});
}
import createFetch from './createFetch';

return NetworkBase.create(fetchQuery);
export default {
create(options) {
return Network.create(createFetch(options));
},
};
19 changes: 19 additions & 0 deletions src/modern/createFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { graphql } from 'graphql';

export default function createFetch({ schema, rootValue, contextValue }) {
return function fetchQuery(operation, variables) {
return graphql(
schema,
operation.text,
rootValue,
contextValue,
variables,
).then((payload) => {
if (payload.errors) {
throw new Error(payload.errors);
}

return payload;
});
};
}
1 change: 1 addition & 0 deletions src/modern/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export createFetch from './createFetch';
export Network from './Network';

0 comments on commit d4ea6d3

Please sign in to comment.